Last active
August 29, 2015 14:00
-
-
Save fjolnir/11325074 to your computer and use it in GitHub Desktop.
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
use std::libc; | |
use std::cast::transmute; | |
type Id = libc::uintptr_t; | |
type Sel = libc::uintptr_t; | |
#[link(name = "objc")] | |
extern { | |
fn sel_registerName(sel: &str) -> Sel; | |
fn objc_msgSend(object: Id, sel: Sel, ...) -> libc::uintptr_t; | |
fn objc_lookUpClass(name: &str) -> Id; | |
} | |
trait ObjcObject { | |
fn send<T>(&self, sel: &str) -> T; | |
} | |
impl ObjcObject for Id { | |
fn send<T>(&self, sel: &str) -> T { | |
let sel_ = unsafe { sel_registerName(sel) }; | |
let imp: extern "C" fn(Id, Sel, ...) -> T = unsafe { std::cast::transmute(objc_msgSend) }; | |
imp(*self, sel_) | |
} | |
} | |
fn objc_class(name: &str) -> Id { | |
println!("Looking up {}", name); | |
unsafe { objc_lookUpClass(name) } | |
} | |
fn main() { | |
let kls = objc_class("NSObject"); | |
let hash: uint = kls.send("hash"); | |
let instance: Id = kls.send("alloc"); | |
println!("NSObject={} +[NSObject hash]={} +[NSObject alloc]={}", kls, hash, instance); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment