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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
i686-unknown-linux-gnu | |
i686-unknown-linux-gnu/rt | |
i686-unknown-linux-gnu/rt/stage1 | |
i686-unknown-linux-gnu/rt/stage1/sundown | |
i686-unknown-linux-gnu/rt/stage1/sundown/src | |
i686-unknown-linux-gnu/rt/stage1/sundown/html | |
i686-unknown-linux-gnu/rt/stage1/sync | |
i686-unknown-linux-gnu/rt/stage1/isaac | |
i686-unknown-linux-gnu/rt/stage1/test | |
i686-unknown-linux-gnu/rt/stage1/arch |
Why is it that there is anOption.expect(M)
, but noResult.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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[inline] | |
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[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."; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import | |
rectangle | |
type | |
MaxRectPacker* [T] = object | |
initialWidth, initialHeight: T | |
freeGeometry: seq[Rectangle[T]] | |
proc TryGet* [T](self: var MaxRectPacker[T]; |
OlderNewer