Skip to content

Instantly share code, notes, and snippets.

View bvssvni's full-sized avatar

Sven Nilsen bvssvni

View GitHub Profile
@bvssvni
bvssvni / gist:9327294
Last active August 29, 2015 13:56
Emulating dynamic types: How to implement Any for owned Trait pointers
//!
//! >rustc --version
//! rustc 0.10-pre (ee8f45e 2014-02-18 13:41:49 -0800)
//!
//! In this example we will implement Any for a trait pointer.
//! This is useful when we want to design an Entity/Component system
//! that is extensible beyond the library.
//!
//! As example we have two objects 'Player' and 'Enemy'.
//! Both implement the trait 'Entity' which is used 90% of the time.
@bvssvni
bvssvni / gist:9343353
Last active November 11, 2017 19:32
How To Organize Code in Rust per Object or Function
//! rustc 0.10-pre (ee8f45e 2014-02-18 13:41:49 -0800)
//!
//! Before you bite my head off, this is NOT a recommend way of organizing code!
//! It is only a demonstration what you can do in Rust! (wish I put this comment in earlier)
//! My intention with this example is to show new people the features of the module system.
//!
//! The module system in Rust gives you precise control
//! over the interface when writing libraries.
//! You can write the layout independent of how you organize the code!
//! In this example two different conventions are explored.
rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800)
/// A struct with borrowed pointer to a closure.
/// This is to be able to reuse the closure then struct runs out of scope.
pub struct Test<'a> {
f: &'a 'a ||
}
impl<'a> Test<'a> {
rustc 0.10-pre (0a5138c 2014-03-04 13:16:41 -0800)
pub trait Expr<'a, T> {
fn then(&'a self, f: &'a 'a ||) -> T;
}
impl<'a> Expr<'a, ()> for () {
fn then(&'a self, f: &'a 'a ||) -> () {
(*f)();
@bvssvni
bvssvni / gist:9408082
Last active August 29, 2015 13:57
A 'rusti' interpreter script (by mcpherrin)
#!/bin/sh
while true; do
echo -n " > "
read line
TMP=`mktemp`
rustc - -o $TMP <<EOF
#[feature(globs, macro_rules, struct_variant)];
// extern mod extra;
fn main() {
/**
* cairo_show_text:
* @cr: a cairo context
* @utf8: a NUL-terminated string of text encoded in UTF-8, or %NULL
*
* A drawing operator that generates the shape from a string of UTF-8
* characters, rendered according to the current font_face, font_size
* (font_matrix), and font_options.
*
@bvssvni
bvssvni / gist:9674632
Last active December 23, 2023 22:56
A Rust Chain Macro
//! Chain macro in Rust
//!
//! A typical usage is to avoid nested match expressions.
//!
//! Supports:
//! - Pattern matching
//! - Or expressions (A | B => ...)
//! - Guarded statements (x if <cond> => ...)
//! - Implicit else (requires all arms to return same type)
let x = Some(1);
let y = Some(2);
let z = monad!(
None {
_ => x,
Some(x) => y,
Some(y) => Some(x+y)
}
);
assert_eq!(z, Some(3));
enum Void {}
enum Foo<T, U> {
A(int) for T = (), U = ()
}
fn insert_text(text: &mut HashMap<&'static str, &'static str>) {
let text: Vec<&'static str> = vec!(include!("text.rs"));
for chunk in text.as_slice().chunks(2) {
let id: &'static str = *chunk.get(0).unwrap();
let txt: &'static str = *chunk.get(1).unwrap();
text.insert(id, txt);
}
}