Skip to content

Instantly share code, notes, and snippets.

View brendanzab's full-sized avatar
😵‍💫
writing elaborators

Brendan Zabarauskas brendanzab

😵‍💫
writing elaborators
View GitHub Profile
/**
* Because I have a large number of foriegn callback functions to wrap it'd be
* great I you could use a generic handle for local data setting and getting.
* Unfortunately when I try this I get:
*
* generic_tls.rs:23:64: 12:65 error: unexpected token: `,`
* generic_tls.rs:23 unsafe { task::local_data::local_data_set(tls_handle<FooFun>, cbfun); }
*
*/
fn main() {
let stuff = ~[~"one", ~"two", ~"three"];
stuff
.map(|s| str::append(~"lol", *s))
.map(|s| io::println(*s));
}
@brendanzab
brendanzab / ffi_ptrs.md
Created October 23, 2012 17:04
Rust FFI function parameter conversions

  C                            |  Rust
===========================================================
  T value                      |  value: T
  T *value                     |  value: *T
  T* *value                    |  value: **T
  const T *value               |  value: *T
  const T* const *value        |  value: **T
@brendanzab
brendanzab / proposal.md
Created October 25, 2012 01:48
A proposal for how to enable the use constants with pattern matching

At the moment one of the limitations of pattern matching is the inability to use variables in the scope of the match.

pcwalton suggests using const:

const origin: Point = Point { x: 0, y: 0 };

let foo = Point { x: 12, y: 42 }

match p {
@brendanzab
brendanzab / map_cast.rs
Created October 26, 2012 01:13 — forked from erickt/gist:3956374
map_cast Rust macro
use to_str::ToStr;
macro_rules! map_cast(
(~[$($elems:expr),+] -> $T:ty) => (~[$($elems as $T),+]);
(@[$($elems:expr),+] -> $T:ty) => (@[$($elems as $T),+]);
($arr:expr -> $T:ty) => ($arr.map(|a| *a as $T));
)
fn main() {
let arr1 = map_cast!(~[1, 2.5] -> ToStr);
@brendanzab
brendanzab / check_gl.rs
Created October 26, 2012 07:35
check_gl Rust macro
// example usage:
//
// check_gl!(glVertexAttribPointer(attrib, 4, GL_FLOAT, GL_FALSE as GLboolean, 0, null()));
//
// if there is an error it outputs: `OpenGL <<opengl error code>>: <<function identifier>>(<<function arguments>>)`
//
#[cfg(check_gl)]
macro_rules! check_gl (
@brendanzab
brendanzab / text.md
Created October 26, 2012 14:20
Beautiful code

Linenoise: I particularly love the 'completeLine' function, with its cute little 'beep' and graceful cascade of conditionals.

//
// let glfns = gl::init();
//
// glfns.glViewport(0, 0, width as GLsizei, height as GLsizei);
//
struct GLProc {
glCullFace: extern fn(++mode: GLenum),
glFrontFace: extern fn(++mode: GLenum),
@brendanzab
brendanzab / index.rs
Created October 28, 2012 03:50
Vector struct index operator performance
extern mod std;
use std::time::precise_time_ns;
use cast::{reinterpret_cast, transmute};
use ptr::to_unsafe_ptr;
pub struct Vec4 { x: float, y: float, z: float, w: float }
pub struct VecMatch { x: float, y: float, z: float, w: float }
pub impl VecMatch: Index<uint, float> {
pure fn abs<T:Copy Num Ord>(x: &T) -> T {
if x >= from_int(0) { *x } else {-x }
}