Enum ext_php_rs::props::Property [−][src]
pub enum Property<'a, T> {
Field(Box<dyn Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync>),
Method {
get: Option<Box<dyn Fn(&T, &mut Zval) -> PhpResult + Send + Sync + 'a>>,
set: Option<Box<dyn Fn(&mut T, &Zval) -> PhpResult + Send + Sync + 'a>>,
},
}
Expand description
Represents a property added to a PHP class.
There are two types of properties:
- Field properties, where the data is stored inside a struct field.
- Method properties, where getter and/or setter functions are provided, which are used to get and set the value of the property.
Variants
Field(Box<dyn Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync>)
Method
Fields
Implementations
Creates a field property.
Parameters
f
- The function used to get a mutable reference to the property.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|test: &mut Test| &mut test.a);
Creates a method property with getters and setters.
If either the getter or setter is not given, an exception will be thrown when attempting to retrieve/set the property.
Parameters
get
- Function used to get the value of the property, in anOption
.set
- Function used to set the value of the property, in anOption
.
Examples
struct Test;
impl Test {
pub fn get_prop(&self) -> String {
"Hello".into()
}
pub fn set_prop(&mut self, val: String) {
println!("{}", val);
}
}
let prop: Property<Test> = Property::method(Some(Test::get_prop), Some(Test::set_prop));
Attempts to retrieve the value of the property from the given object
self_
.
The value of the property, if successfully retrieved, is loaded into the
given Zval
retval
. If unsuccessful, a PhpException
is
returned inside the error variant of a result.
Parameters
self_
- The object to retrieve the property from.retval
- TheZval
to load the value of the property into.
Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be retrieved.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let mut zv = Zval::new();
prop.get(&mut test, &mut zv).unwrap();
assert_eq!(zv.long(), Some(500));
Attempts to set the value of the property inside the given object
self_
.
The new value of the property is supplied inside the given Zval
value
. If unsuccessful, a PhpException
is returned inside the
error variant of a result.
Parameters
self_
- The object to set the property in.value
- TheZval
containing the new content for the property.
Returns
Nothing upon success, a PhpException
inside an error variant when
the property could not be set.
Examples
struct Test {
pub a: i32,
}
let prop: Property<Test> = Property::field(|obj: &mut Test| &mut obj.a);
let mut test = Test { a: 500 };
let zv = 100.into_zval(false).unwrap();
prop.set(&mut test, &zv).unwrap();
assert_eq!(test.a, 100);