Struct Property

Source
pub struct Property<M: MethodType<D>, D: DataType> { /* private fields */ }
Expand description

A D-Bus Property.

Implementations§

Source§

impl<M: MethodType<D>, D: DataType> Property<M, D>

Source

pub fn emits_changed(self, e: EmitsChangedSignal) -> Self

Builder method that allows setting the Property’s signal behavior when changed.

Note: if e is set to const, the property will be read only.

Examples found in repository?
examples/adv_server.rs (line 83)
57fn create_iface(check_complete_s: mpsc::Sender<i32>) -> (Interface<MTFn<TData>, TData>, Arc<Signal<TData>>) {
58    let f = tree::Factory::new_fn();
59
60    let check_complete = Arc::new(f.signal("CheckComplete", ()));
61
62    (f.interface("com.example.dbus.rs.device", ())
63        // The online property can be both set and get
64        .add_p(f.property::<bool,_>("online", ())
65            .access(Access::ReadWrite)
66            .on_get(|i, m| {
67                let dev: &Arc<Device> = m.path.get_data();
68                i.append(dev.online.get());
69                Ok(())
70            })
71            .on_set(|i, m| {
72                let dev: &Arc<Device> = m.path.get_data();
73                let b: bool = i.read()?;
74                if b && dev.checking.get() {
75                    return Err(MethodErr::failed(&"Device currently under check, cannot bring online"))
76                }
77                dev.online.set(b);
78                Ok(())
79            })
80        )
81        // The "checking" property is read only
82        .add_p(f.property::<bool,_>("checking", ())
83            .emits_changed(EmitsChangedSignal::False)
84            .on_get(|i, m| {
85                let dev: &Arc<Device> = m.path.get_data();
86                i.append(dev.checking.get());
87                Ok(())
88            })
89        )
90        // ...and so is the "description" property
91        .add_p(f.property::<&str,_>("description", ())
92            .emits_changed(EmitsChangedSignal::Const)
93            .on_get(|i, m| {
94                let dev: &Arc<Device> = m.path.get_data();
95                i.append(&dev.description);
96                Ok(())
97            })
98        )
99        // ...add a method for starting a device check...
100        .add_m(f.method("check", (), move |m| {
101            let dev: &Arc<Device> = m.path.get_data();
102            if dev.checking.get() {
103                return Err(MethodErr::failed(&"Device currently under check, cannot start another check"))
104            }
105            if dev.online.get() {
106                return Err(MethodErr::failed(&"Device is currently online, cannot start check"))
107            }
108            dev.checking.set(true);
109
110            // Start some lengthy processing in a separate thread...
111            let devindex = dev.index;
112            let ch = check_complete_s.clone();
113            thread::spawn(move || {
114
115                // Bogus check of device
116                use std::time::Duration;
117                thread::sleep(Duration::from_secs(15));
118
119                // Tell main thread that we finished
120                ch.send(devindex).unwrap();
121            });
122            Ok(vec!(m.msg.method_return()))
123        }))
124        // Indicate that we send a special signal once checking has completed.
125        .add_s(check_complete.clone())
126    , check_complete)
127}
Source

pub fn auto_emit_on_set(self, b: bool) -> Self

Builder method that determines whether or not setting this property will result in an PropertiesChanged signal. Defaults to true.

When set to true (the default), the behaviour is determined by “emits_changed”. When set to false, no PropertiesChanged signal will be emitted (but the signal still shows up in introspection data). You can still emit the signal manually by, e g, calling add_propertieschanged and send the resulting message(s).

Source

pub fn access(self, e: Access) -> Self

Builder method that allows setting the Property as readable, writable, or both.

Note: might modify emits_changed as well, if property is changed to non-readonly and emit is set to “Const”.

Examples found in repository?
examples/adv_server.rs (line 65)
57fn create_iface(check_complete_s: mpsc::Sender<i32>) -> (Interface<MTFn<TData>, TData>, Arc<Signal<TData>>) {
58    let f = tree::Factory::new_fn();
59
60    let check_complete = Arc::new(f.signal("CheckComplete", ()));
61
62    (f.interface("com.example.dbus.rs.device", ())
63        // The online property can be both set and get
64        .add_p(f.property::<bool,_>("online", ())
65            .access(Access::ReadWrite)
66            .on_get(|i, m| {
67                let dev: &Arc<Device> = m.path.get_data();
68                i.append(dev.online.get());
69                Ok(())
70            })
71            .on_set(|i, m| {
72                let dev: &Arc<Device> = m.path.get_data();
73                let b: bool = i.read()?;
74                if b && dev.checking.get() {
75                    return Err(MethodErr::failed(&"Device currently under check, cannot bring online"))
76                }
77                dev.online.set(b);
78                Ok(())
79            })
80        )
81        // The "checking" property is read only
82        .add_p(f.property::<bool,_>("checking", ())
83            .emits_changed(EmitsChangedSignal::False)
84            .on_get(|i, m| {
85                let dev: &Arc<Device> = m.path.get_data();
86                i.append(dev.checking.get());
87                Ok(())
88            })
89        )
90        // ...and so is the "description" property
91        .add_p(f.property::<&str,_>("description", ())
92            .emits_changed(EmitsChangedSignal::Const)
93            .on_get(|i, m| {
94                let dev: &Arc<Device> = m.path.get_data();
95                i.append(&dev.description);
96                Ok(())
97            })
98        )
99        // ...add a method for starting a device check...
100        .add_m(f.method("check", (), move |m| {
101            let dev: &Arc<Device> = m.path.get_data();
102            if dev.checking.get() {
103                return Err(MethodErr::failed(&"Device currently under check, cannot start another check"))
104            }
105            if dev.online.get() {
106                return Err(MethodErr::failed(&"Device is currently online, cannot start check"))
107            }
108            dev.checking.set(true);
109
110            // Start some lengthy processing in a separate thread...
111            let devindex = dev.index;
112            let ch = check_complete_s.clone();
113            thread::spawn(move || {
114
115                // Bogus check of device
116                use std::time::Duration;
117                thread::sleep(Duration::from_secs(15));
118
119                // Tell main thread that we finished
120                ch.send(devindex).unwrap();
121            });
122            Ok(vec!(m.msg.method_return()))
123        }))
124        // Indicate that we send a special signal once checking has completed.
125        .add_s(check_complete.clone())
126    , check_complete)
127}
Source

pub fn annotate<N: Into<String>, V: Into<String>>( self, name: N, value: V, ) -> Self

Builder method that adds an annotation to the method.

Source

pub fn deprecated(self) -> Self

Builder method that adds an annotation that this entity is deprecated.

Source

pub fn get_name(&self) -> &str

Get property name

Source

pub fn get_data(&self) -> &D::Property

Get associated data

Source

pub fn can_get(&self) -> Result<(), MethodErr>

Returns Ok if the property is gettable

Source

pub fn get_as_variant( &self, i: &mut IterAppend<'_>, pinfo: &PropInfo<'_, M, D>, ) -> Result<(), MethodErr>

Calls the on_get function and appends the result as a variant.

Note: Will panic if get_cb is not set.

Source

pub fn can_set(&self, i: Option<Iter<'_>>) -> Result<(), MethodErr>

Returns Ok if the property is settable.

Will verify signature in case iter is not None; iter is supposed to point at the Variant with the item inside.

Source

pub fn set_as_variant( &self, i: &mut Iter<'_>, pinfo: &PropInfo<'_, M, D>, ) -> Result<Option<Message>, MethodErr>

Calls the on_set function, which reads from i.

The return value might contain an extra message containing the EmitsChanged signal. Note: Will panic if set_cb is not set.

Source

pub fn add_propertieschanged<F: FnOnce() -> Box<dyn RefArg>>( &self, v: &mut Vec<PropertiesPropertiesChanged>, iface: &IfaceName<'_>, new_value: F, )

Adds this property to a list of PropertiesChanged signals.

“v” is updated with the signal for this property. “new_value” is only called if self.emits is “true”, it should return the value of the property. If no PropertiesChanged signal should be emitted for this property, “v” is left unchanged.

Source§

impl<'a, D: DataType> Property<MTFn<D>, D>

Source

pub fn on_get<H>(self, handler: H) -> Property<MTFn<D>, D>
where H: 'static + Fn(&mut IterAppend<'_>, &PropInfo<'_, MTFn<D>, D>) -> Result<(), MethodErr>,

Sets the callback for getting a property.

For single-thread use.

Examples found in repository?
examples/adv_server.rs (lines 66-70)
57fn create_iface(check_complete_s: mpsc::Sender<i32>) -> (Interface<MTFn<TData>, TData>, Arc<Signal<TData>>) {
58    let f = tree::Factory::new_fn();
59
60    let check_complete = Arc::new(f.signal("CheckComplete", ()));
61
62    (f.interface("com.example.dbus.rs.device", ())
63        // The online property can be both set and get
64        .add_p(f.property::<bool,_>("online", ())
65            .access(Access::ReadWrite)
66            .on_get(|i, m| {
67                let dev: &Arc<Device> = m.path.get_data();
68                i.append(dev.online.get());
69                Ok(())
70            })
71            .on_set(|i, m| {
72                let dev: &Arc<Device> = m.path.get_data();
73                let b: bool = i.read()?;
74                if b && dev.checking.get() {
75                    return Err(MethodErr::failed(&"Device currently under check, cannot bring online"))
76                }
77                dev.online.set(b);
78                Ok(())
79            })
80        )
81        // The "checking" property is read only
82        .add_p(f.property::<bool,_>("checking", ())
83            .emits_changed(EmitsChangedSignal::False)
84            .on_get(|i, m| {
85                let dev: &Arc<Device> = m.path.get_data();
86                i.append(dev.checking.get());
87                Ok(())
88            })
89        )
90        // ...and so is the "description" property
91        .add_p(f.property::<&str,_>("description", ())
92            .emits_changed(EmitsChangedSignal::Const)
93            .on_get(|i, m| {
94                let dev: &Arc<Device> = m.path.get_data();
95                i.append(&dev.description);
96                Ok(())
97            })
98        )
99        // ...add a method for starting a device check...
100        .add_m(f.method("check", (), move |m| {
101            let dev: &Arc<Device> = m.path.get_data();
102            if dev.checking.get() {
103                return Err(MethodErr::failed(&"Device currently under check, cannot start another check"))
104            }
105            if dev.online.get() {
106                return Err(MethodErr::failed(&"Device is currently online, cannot start check"))
107            }
108            dev.checking.set(true);
109
110            // Start some lengthy processing in a separate thread...
111            let devindex = dev.index;
112            let ch = check_complete_s.clone();
113            thread::spawn(move || {
114
115                // Bogus check of device
116                use std::time::Duration;
117                thread::sleep(Duration::from_secs(15));
118
119                // Tell main thread that we finished
120                ch.send(devindex).unwrap();
121            });
122            Ok(vec!(m.msg.method_return()))
123        }))
124        // Indicate that we send a special signal once checking has completed.
125        .add_s(check_complete.clone())
126    , check_complete)
127}
Source

pub fn on_set<H>(self, handler: H) -> Property<MTFn<D>, D>
where H: 'static + Fn(&mut Iter<'_>, &PropInfo<'_, MTFn<D>, D>) -> Result<(), MethodErr>,

Sets the callback for setting a property.

For single-thread use.

Examples found in repository?
examples/adv_server.rs (lines 71-79)
57fn create_iface(check_complete_s: mpsc::Sender<i32>) -> (Interface<MTFn<TData>, TData>, Arc<Signal<TData>>) {
58    let f = tree::Factory::new_fn();
59
60    let check_complete = Arc::new(f.signal("CheckComplete", ()));
61
62    (f.interface("com.example.dbus.rs.device", ())
63        // The online property can be both set and get
64        .add_p(f.property::<bool,_>("online", ())
65            .access(Access::ReadWrite)
66            .on_get(|i, m| {
67                let dev: &Arc<Device> = m.path.get_data();
68                i.append(dev.online.get());
69                Ok(())
70            })
71            .on_set(|i, m| {
72                let dev: &Arc<Device> = m.path.get_data();
73                let b: bool = i.read()?;
74                if b && dev.checking.get() {
75                    return Err(MethodErr::failed(&"Device currently under check, cannot bring online"))
76                }
77                dev.online.set(b);
78                Ok(())
79            })
80        )
81        // The "checking" property is read only
82        .add_p(f.property::<bool,_>("checking", ())
83            .emits_changed(EmitsChangedSignal::False)
84            .on_get(|i, m| {
85                let dev: &Arc<Device> = m.path.get_data();
86                i.append(dev.checking.get());
87                Ok(())
88            })
89        )
90        // ...and so is the "description" property
91        .add_p(f.property::<&str,_>("description", ())
92            .emits_changed(EmitsChangedSignal::Const)
93            .on_get(|i, m| {
94                let dev: &Arc<Device> = m.path.get_data();
95                i.append(&dev.description);
96                Ok(())
97            })
98        )
99        // ...add a method for starting a device check...
100        .add_m(f.method("check", (), move |m| {
101            let dev: &Arc<Device> = m.path.get_data();
102            if dev.checking.get() {
103                return Err(MethodErr::failed(&"Device currently under check, cannot start another check"))
104            }
105            if dev.online.get() {
106                return Err(MethodErr::failed(&"Device is currently online, cannot start check"))
107            }
108            dev.checking.set(true);
109
110            // Start some lengthy processing in a separate thread...
111            let devindex = dev.index;
112            let ch = check_complete_s.clone();
113            thread::spawn(move || {
114
115                // Bogus check of device
116                use std::time::Duration;
117                thread::sleep(Duration::from_secs(15));
118
119                // Tell main thread that we finished
120                ch.send(devindex).unwrap();
121            });
122            Ok(vec!(m.msg.method_return()))
123        }))
124        // Indicate that we send a special signal once checking has completed.
125        .add_s(check_complete.clone())
126    , check_complete)
127}
Source§

impl<'a, D: DataType> Property<MTFnMut<D>, D>

Source

pub fn on_get<H>(self, handler: H) -> Property<MTFnMut<D>, D>
where H: 'static + Fn(&mut IterAppend<'_>, &PropInfo<'_, MTFnMut<D>, D>) -> Result<(), MethodErr>,

Sets the callback for getting a property.

For single-thread use.

Source

pub fn on_set<H>(self, handler: H) -> Property<MTFnMut<D>, D>
where H: 'static + Fn(&mut Iter<'_>, &PropInfo<'_, MTFnMut<D>, D>) -> Result<(), MethodErr>,

Sets the callback for setting a property.

For single-thread use.

Source§

impl<D: DataType> Property<MTSync<D>, D>

Source

pub fn on_get<H>(self, handler: H) -> Property<MTSync<D>, D>
where H: Fn(&mut IterAppend<'_>, &PropInfo<'_, MTSync<D>, D>) -> Result<(), MethodErr> + Send + Sync + 'static,

Sets the callback for getting a property.

For multi-thread use.

Source

pub fn on_set<H>(self, handler: H) -> Property<MTSync<D>, D>
where H: Fn(&mut Iter<'_>, &PropInfo<'_, MTSync<D>, D>) -> Result<(), MethodErr> + Send + Sync + 'static,

Sets the callback for setting a property.

For single-thread use.

Source§

impl<M: MethodType<D>, D: DataType> Property<M, D>
where D::Property: Append + Clone,

Source

pub fn default_get(self) -> Self

Adds a “standard” get handler.

Source§

impl<M: MethodType<D>, D: DataType> Property<M, D>
where D::Property: RefArg,

Source

pub fn default_get_refarg(self) -> Self

Adds a “standard” get handler (for RefArgs).

Trait Implementations§

Source§

impl<M: Debug + MethodType<D>, D: Debug + DataType> Debug for Property<M, D>
where D::Property: Debug,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M, D> Freeze for Property<M, D>
where <D as DataType>::Property: Freeze,

§

impl<M, D> RefUnwindSafe for Property<M, D>

§

impl<M, D> Send for Property<M, D>
where <D as DataType>::Property: Send, <M as MethodType<D>>::GetProp: Send, <M as MethodType<D>>::SetProp: Send,

§

impl<M, D> Sync for Property<M, D>
where <D as DataType>::Property: Sync, <M as MethodType<D>>::GetProp: Sync, <M as MethodType<D>>::SetProp: Sync,

§

impl<M, D> Unpin for Property<M, D>
where <D as DataType>::Property: Unpin,

§

impl<M, D> UnwindSafe for Property<M, D>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.