From 19e7958ba96c872be86eafdb3bdb307928fa3a14 Mon Sep 17 00:00:00 2001 From: John Quigley Date: Mon, 21 Dec 2015 11:35:12 -0500 Subject: [PATCH] rustfmt --- src/lib.rs | 178 +++++++++++++++++++++++++++-------------------------- 1 file changed, 90 insertions(+), 88 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f4fe0d2..dd0ef92 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,108 +76,110 @@ impl Barfly { } } - // this code is pretty much a rip off of - // https://github.com/SSheldon/rust-objc-foundation/blob/master/examples/custom_class.rs - - // would be nice to use some mangled ident names here base on $name, - // (and avoid the need for $cbs_name) - // but concat_idents! doesn't work in the cases that I want. - enum Callback {} - unsafe impl Message for Callback { } - - // SO.. some explanation is in order here. We want to allow closure callbacks that - // can modify their environment. But we can't keep them on the $name object because - // that is really just a stateless proxy for the objc object. So we store them - // as numeric pointers values in "ivar" fields on that object. But, if we store a pointer to the - // closure object, we'll run into issues with thin/fat pointer conversions (because - // closure objects are trait objects and thus fat pointers). So we wrap the closure in - // another boxed object ($cbs_name), which, since it doesn't use traits, is actually a - // regular "thin" pointer, and store THAT pointer in the ivar. But...so...oy. - struct CallbackState { - cb: Box ()> - } +// this code is pretty much a rip off of +// https://github.com/SSheldon/rust-objc-foundation/blob/master/examples/custom_class.rs + +// would be nice to use some mangled ident names here base on $name, +// (and avoid the need for $cbs_name) +// but concat_idents! doesn't work in the cases that I want. +enum Callback {} +unsafe impl Message for Callback {} + +// SO.. some explanation is in order here. We want to allow closure callbacks that +// can modify their environment. But we can't keep them on the $name object because +// that is really just a stateless proxy for the objc object. So we store them +// as numeric pointers values in "ivar" fields on that object. But, if we store a pointer to the +// closure object, we'll run into issues with thin/fat pointer conversions (because +// closure objects are trait objects and thus fat pointers). So we wrap the closure in +// another boxed object ($cbs_name), which, since it doesn't use traits, is actually a +// regular "thin" pointer, and store THAT pointer in the ivar. But...so...oy. +struct CallbackState { + cb: Box ()>, +} - impl Callback { - fn from(cb:Box ()>) -> Id { - let cbs = CallbackState { - cb: cb - }; - let bcbs = Box::new(cbs); - - let ptr = Box::into_raw(bcbs); - let ptr = ptr as *mut c_void as u64; - println!("{}", ptr); - let mut oid = ::new(); - (*oid).setptr(ptr); - oid - } +impl Callback { + fn from(cb: Box ()>) -> Id { + let cbs = CallbackState { cb: cb }; + let bcbs = Box::new(cbs); + + let ptr = Box::into_raw(bcbs); + let ptr = ptr as *mut c_void as u64; + println!("{}", ptr); + let mut oid = ::new(); + (*oid).setptr(ptr); + oid + } - fn setptr(&mut self, uptr: u64) { - unsafe { - let obj = &mut *(self as *mut _ as *mut ::objc::runtime::Object); - println!("setting the ptr: {}", uptr); - obj.set_ivar("_cbptr", uptr); - } - } + fn setptr(&mut self, uptr: u64) { + unsafe { + let obj = &mut *(self as *mut _ as *mut ::objc::runtime::Object); + println!("setting the ptr: {}", uptr); + obj.set_ivar("_cbptr", uptr); } + } +} - // TODO: Drop for $name doesn't get called, probably because objc manages the memory and - // releases it for us. so we leak the boxed callback right now. - - impl INSObject for Callback { - fn class() -> &'static Class { - let cname = "Callback"; - - let mut klass = Class::get(cname); - if klass.is_none() { - println!("registering class for {}", cname); - let superclass = NSObject::class(); - let mut decl = ClassDecl::new(superclass, &cname).unwrap(); - decl.add_ivar::("_cbptr"); - - extern fn barfly_callback_call(this: &Object, _cmd: Sel) { - println!("callback, getting the pointer"); - unsafe { - let pval:u64 = *this.get_ivar("_cbptr"); - let ptr = pval as *mut c_void; - let ptr = ptr as *mut CallbackState; - let bcbs:Box = Box::from_raw(ptr); - { - println!("cb test from cb"); - (*bcbs.cb)(); - } - mem::forget(bcbs); - } - } +// TODO: Drop for $name doesn't get called, probably because objc manages the memory and +// releases it for us. so we leak the boxed callback right now. - unsafe { - decl.add_method(sel!(call), barfly_callback_call as extern fn(&Object, Sel)); - } +impl INSObject for Callback { + fn class() -> &'static Class { + let cname = "Callback"; - decl.register(); - klass = Class::get(cname); + let mut klass = Class::get(cname); + if klass.is_none() { + println!("registering class for {}", cname); + let superclass = NSObject::class(); + let mut decl = ClassDecl::new(superclass, &cname).unwrap(); + decl.add_ivar::("_cbptr"); + + extern "C" fn barfly_callback_call(this: &Object, _cmd: Sel) { + println!("callback, getting the pointer"); + unsafe { + let pval: u64 = *this.get_ivar("_cbptr"); + let ptr = pval as *mut c_void; + let ptr = ptr as *mut CallbackState; + let bcbs: Box = Box::from_raw(ptr); + { + println!("cb test from cb"); + (*bcbs.cb)(); + } + mem::forget(bcbs); } - klass.unwrap() } + + unsafe { + decl.add_method(sel!(call), + barfly_callback_call as extern "C" fn(&Object, Sel)); + } + + decl.register(); + klass = Class::get(cname); } + klass.unwrap() + } +} - pub fn add_fly_item(fly: &Barfly, menuItem: &str, cbs: Box ()>) { - unsafe { - let cb_obj = Callback::from(cbs); +pub fn add_fly_item(fly: &Barfly, menuItem: &str, cbs: Box ()>) { + unsafe { + let cb_obj = Callback::from(cbs); - let astring = NSString::alloc(nil); - let no_key = NSString::init_str(astring,""); // TODO want this eventually + let astring = NSString::alloc(nil); + let no_key = NSString::init_str(astring, ""); // TODO want this eventually - let astring = NSString::alloc(nil); - let itemtitle = NSString::init_str(astring,menuItem); - let action = sel!(call); - let aitem = NSMenuItem::alloc(nil); - let item = NSMenuItem::initWithTitle_action_keyEquivalent_(aitem, itemtitle, action, no_key); - let _: () = msg_send![item, setTarget:cb_obj]; + let astring = NSString::alloc(nil); + let itemtitle = NSString::init_str(astring, menuItem); + let action = sel!(call); + let aitem = NSMenuItem::alloc(nil); + let item = NSMenuItem::initWithTitle_action_keyEquivalent_(aitem, + itemtitle, + action, + no_key); + let _: () = msg_send![item, setTarget:cb_obj]; - NSMenu::addItem_(fly.menu, item); - } + NSMenu::addItem_(fly.menu, item); } +} #[test] -- 2.51.2