Skip to content

Commit

Permalink
Tree: Closures can no longer borrow their environment :-(
Browse files Browse the repository at this point in the history
This is due to a soundness fix in rustc:
rust-lang/rust#35143

Signed-off-by: David Henningsson <diwic@ubuntu.com>
  • Loading branch information
diwic committed Aug 2, 2016
1 parent 9f9af79 commit 138424e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
5 changes: 3 additions & 2 deletions examples/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn main() {
// We create the signal first, since we'll need it in both inside the method callback
// and when creating the tree.
let signal = Arc::new(f.signal("HelloHappened", ()).sarg::<&str,_>("sender"));
let signal2 = signal.clone();

// We create a tree with one object path inside and make that path introspectable.
let tree = f.tree().add(f.object_path("/hello", ()).introspectable().add(
Expand All @@ -34,7 +35,7 @@ fn main() {
f.interface("com.example.dbustest", ()).add_m(

// ...and a method inside the interface.
f.method("Hello", (), |m| {
f.method("Hello", (), move |m| {

// This is the callback that will be called when another peer on the bus calls our method.
// the callback receives "MethodInfo" struct and can return either an error, or a list of
Expand All @@ -55,7 +56,7 @@ fn main() {
}).outarg::<&str,_>("reply")

// We also add the signal to the interface. This is mainly for introspection.
).add_s(signal.clone())
).add_s(signal2)
));

// We register all object paths in the tree.
Expand Down
9 changes: 5 additions & 4 deletions src/tree/factory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,16 @@ impl Factory<MTFn<()>, ()> {
impl<D: DataType> Factory<MTFn<D>, D> {
/// Creates a new method for single-thread use.
pub fn method<H, T>(&self, t: T, data: D::Method, handler: H) -> Method<MTFn<D>, D>
where H: Fn(&MethodInfo<MTFn<D>, D>) -> MethodResult, T: Into<Member<'static>> {
super::leaves::new_method(t.into(), data, Box::new(handler) as Box<<MTFn<D> as MethodType<D>>::Method>)
where H: 'static + Fn(&MethodInfo<MTFn<D>, D>) -> MethodResult, T: Into<Member<'static>> {
super::leaves::new_method(t.into(), data, Box::new(handler) as Box<_>)
}
}

impl<D: DataType> Factory<MTSync<D>, D> {
/// Creates a new method for multi-thread use.
pub fn method<H, T>(&self, t: T, data: D::Method, handler: H) -> Method<MTSync<D>, D>
where H: Fn(&MethodInfo<MTSync<D>, D>) -> MethodResult + Send + Sync + 'static, T: Into<Member<'static>> {
super::leaves::new_method(t.into(), data, Box::new(handler) as Box<<MTSync<D> as MethodType<D>>::Method>)
super::leaves::new_method(t.into(), data, Box::new(handler) as Box<_>)
}
}

Expand Down Expand Up @@ -79,14 +79,15 @@ impl<M: MethodType<D>, D: DataType> Factory<M, D> {

}

/*
#[test]
fn create_fn() {
let f = Factory::new_fn::<()>();
let borrow_me = 5u32;
let m = f.method("test", (), |m| Ok(vec!(m.msg.method_return().append1(&borrow_me))));
assert_eq!(&**m.get_name(), "test");
}

*/

#[test]
fn fn_customdata() {
Expand Down
21 changes: 12 additions & 9 deletions src/tree/leaves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl<'a, D: DataType> Property<MTFn<D>, D> {
///
/// For single-thread use.
pub fn on_get<H>(mut self, handler: H) -> Property<MTFn<D>, D>
where H: Fn(&mut arg::IterAppend, &PropInfo<MTFn<D>, D>) -> Result<(), MethodErr> + 'a {
where H: 'static + Fn(&mut arg::IterAppend, &PropInfo<MTFn<D>, D>) -> Result<(), MethodErr> {
self.get_cb = Some(DebugGetProp(Box::new(handler) as Box<_>));
self
}
Expand All @@ -345,7 +345,7 @@ impl<'a, D: DataType> Property<MTFn<D>, D> {
///
/// For single-thread use.
pub fn on_set<H>(mut self, handler: H) -> Property<MTFn<D>, D>
where H: Fn(&mut arg::Iter, &PropInfo<MTFn<D>, D>) -> Result<(), MethodErr> + 'a {
where H: 'static + Fn(&mut arg::Iter, &PropInfo<MTFn<D>, D>) -> Result<(), MethodErr> {
self.set_cb = Some(DebugSetProp(Box::new(handler) as Box<_>));
self
}
Expand Down Expand Up @@ -460,21 +460,24 @@ fn test_set_prop() {
use tree::{Factory, Access};
use std::cell::{Cell, RefCell};
use std::collections::BTreeMap;
use std::rc::Rc;

let changes = Cell::new(0i32);
let setme = RefCell::new("I have not been set yet!".to_owned());
let changes = Rc::new(Cell::new(0i32));
let (changes1, changes2) = (changes.clone(), changes.clone());
let setme = Rc::new(RefCell::new("I have not been set yet!".to_owned()));
let (setme1, setme2) = (setme.clone(), setme.clone());

let f = Factory::new_fn::<()>();
let tree = f.tree().add(f.object_path("/example", ()).introspectable()
.add(f.interface("com.example.dbus.rs", ())
.add_p(f.property::<i32,_>("changes", ())
.on_get(|i, _| { i.append(changes.get()); Ok(()) }))
.on_get(move |i, _| { i.append(changes1.get()); Ok(()) }))
.add_p(f.property::<String,_>("setme", ())
.access(Access::ReadWrite)
.on_get(|i, _| { i.append(&*setme.borrow()); Ok(()) })
.on_set(|i, _| {
*setme.borrow_mut() = i.get().unwrap();
changes.set(changes.get() + 1);
.on_get(move |i, _| { i.append(&*setme1.borrow()); Ok(()) })
.on_set(move |i, _| {
*setme2.borrow_mut() = i.get().unwrap();
changes2.set(changes2.get() + 1);
Ok(())
}))
)
Expand Down
8 changes: 4 additions & 4 deletions src/tree/methodtype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ pub trait MethodType<D: DataType>: Sized + Default {
#[derive(Default, Debug, Copy, Clone)]
pub struct MTFn<D=()>(PhantomData<*const D>);

impl<'a, D: DataType> MethodType<D> for MTFn<D> {
type GetProp = Fn(&mut IterAppend, &PropInfo<Self, D>) -> Result<(), MethodErr> + 'a;
type SetProp = Fn(&mut Iter, &PropInfo<Self, D>) -> Result<(), MethodErr> + 'a;
type Method = Fn(&MethodInfo<Self, D>) -> MethodResult + 'a;
impl<D: DataType> MethodType<D> for MTFn<D> {
type GetProp = Fn(&mut IterAppend, &PropInfo<Self, D>) -> Result<(), MethodErr>;
type SetProp = Fn(&mut Iter, &PropInfo<Self, D>) -> Result<(), MethodErr>;
type Method = Fn(&MethodInfo<Self, D>) -> MethodResult;

fn call_getprop(p: &Self::GetProp, i: &mut IterAppend, pinfo: &PropInfo<Self, D>)
-> Result<(), MethodErr> { p(i, pinfo) }
Expand Down

0 comments on commit 138424e

Please sign in to comment.