Skip to content

Instantly share code, notes, and snippets.

@SiegeLord
SiegeLord / test.c
Created February 23, 2012 23:39
D shared libs from C
void test_func();
int main()
{
test_func();
return 0;
}
struct A { a : int }
trait TA { }
impl TA for A { }
fn test_struct(a : &A)
{
}
fn test_trait(a : &TA)
@SiegeLord
SiegeLord / gist:5207215
Created March 20, 2013 18:30
Implementing defer in Rust
struct Deferrer
{
df : ~fn()
}
impl Drop for Deferrer
{
fn finalize(&self)
{
(self.df)();
@SiegeLord
SiegeLord / bin.rs
Created April 11, 2013 18:49
"Safe" C functions
extern mod crate;
fn main()
{
println(fmt!("%?", crate::rand()));
}
use core::libc::c_int;
pub extern "C"
{
fn c_func(atexit_ptr : extern "C" fn(cb : extern "C" fn()) -> c_int) -> bool;
fn atexit(cb : extern "C" fn()) -> c_int;
}
fn main()
{
@SiegeLord
SiegeLord / test.rs
Created April 13, 2013 04:59
Inherited mutability
struct c_struct;
unsafe fn c_function(a : *mut c_struct)
{
}
struct wrapper
{
payload : *c_struct
@SiegeLord
SiegeLord / cb.rs
Created April 14, 2013 04:02
C callbacks
use core::libc::*;
extern "C"
{
fn c_func(cb : extern "C" fn(data : *mut c_void), data : *mut c_void);
}
fn rust_func<T>(cb : &fn(data : &T), data : &T)
{
unsafe
@SiegeLord
SiegeLord / pool.rs
Last active December 16, 2015 05:29
Resource manager
struct Resource;
struct Manager<'self>
{
Resources : ~[~Resource],
Count : &'self mut i32
}
struct Object<'self>
{
@SiegeLord
SiegeLord / bin.rs
Last active December 16, 2015 09:59
Tree
struct Owned
{
Name : ~str
}
struct Node
{
Resource : Owned,
Children : ~[Node]
}
@SiegeLord
SiegeLord / destructor.rs
Last active December 16, 2015 10:39
Destructor order
struct Node
{
Name : ~str,
Children : ~[Node]
}
impl Drop for Node
{
fn finalize(&self)
{