Skip to content

Instantly share code, notes, and snippets.

Skrylar / gist:8110262
Created December 24, 2013 08:15
Output of `find i686-unknown-linux-gnu` in a clean Rust directory.

Binding Rust to C

This document is a work in progress, and is primarily concerned with getting Rust code able to talk with C code. What we cover here is writing 'bindings', we will cover 'wrapping' up the bindings and providing an elegant interface in the next document.

Extern blocks

The most basic building block of interfacing Rust with C is the extern block, which is necessary for telling Rust that the C functions you wish to use actually exist.


Skrylar / result.adoc
Last active March 29, 2021 12:54
Tips for Rust

Tips for Rust: Result Types

Expecting certain outcomes from a Result

Why is it that there is an Option.expect(M), but no Result.expect(M)?

I opened a pull request [pr] which put this in, and it turns out the reason there is no expect function is because you are intended to use composition:

Skrylar /
Created January 2, 2014 11:21
Finally getting a zero-copy event dispatch working with SDL.
extern mod sdl;
use std::rc::Rc;
struct Foof;
impl sdl::MouseDelegate for Foof {
fn mouse_did_move(_info: &sdl::MouseMotionEventInfo) {
println!("Mouse position: {}x{}", _info.x(), _info.y())
Skrylar /
Created January 2, 2014 12:07
Heinous hackery to transmit zero-copy events in Rust.
unsafe fn with_window_event_db(_in: &CommonEventInfo, block: |&mut DelegateBoard|) {
let event : &MouseMotionEventInfo = cast::transmute(_in);
EventPump::with_window_db(event.window_id(), block)
unsafe fn with_window_db(id: u32, block: |&mut DelegateBoard|) {
let win = ffi::SDL_GetWindowFromID(id);
if win.is_not_null() {
let dbp = ffi::SDL_GetWindowData(win, bytes!("rust_cb", 0).as_ptr() as *i8);
fn main() {
let mut sentry = sdl::init(sdl::init::Video).unwrap();
let win = sentry.new_window("Foof wob", 32, 32, 800, 600, 0).unwrap();
let mut x = win.borrow_mut();
/* does this work for putting the trait types in? */
sentry.set_application_delegate(&Foof as &sdl::ApplicationDelegate);
x.get().set_mouse_delegate(&Foof as &sdl::MouseDelegate);
pub trait ApplicationDelegate {
fn application_did_quit(&mut self, _timestamp: u32) {}
pub trait MouseDelegate {
fn mouse_did_move(&mut self, _info: &MouseMotionEventInfo) {}
fn mouse_did_click(&mut self, _info: &MouseButtonEventInfo) {}
fn mouse_did_scroll(&mut self, _info: &MouseWheelEventInfo) {}
Skrylar /
Created February 3, 2014 06:47
Just some research in to doing Morphic-like GUIs in Rust.
#[crate_id = "mu1"];
#[crate_type = "bin"];
use std::cast::transmute;
use std::cell::RefCell;
use std::libc::size_t;
use std::rc::{Rc, Weak};
static ErrAlreadyParented: &'static str = "Morph already has a parent.";
Skrylar / test.nim
Created February 27, 2014 19:50
Using 'check' prevents propagation of changes to variable parameters.
import unittest
proc doThings(spuds: var int): int =
spuds = 24
return 99
suite "broken":
test "count the spuds":
var spuds = 0
check doThings(spuds) == 99
Skrylar / maxrectpack.nim
Created March 16, 2014 10:18
Code to demonstrate a crash in the Nimrod compiler. Its important that both files are separate, combining the two makes the crash go away.
MaxRectPacker* [T] = object
initialWidth, initialHeight: T
freeGeometry: seq[Rectangle[T]]
proc TryGet* [T](self: var MaxRectPacker[T];