pub enum Property<'a, T> {
    Field(Box<dyn Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync>),
    Method {
        get: PropertyGetter<'a, T>,
        set: PropertySetter<'a, T>,
    },
}
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

§get: PropertyGetter<'a, T>
§set: PropertySetter<'a, T>

Implementations§

source§

impl<'a, T: 'a> Property<'a, T>

source

pub fn field<F>(f: F) -> Self
where F: Fn(&mut T) -> &mut dyn Prop<'_> + Send + Sync + 'static,

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);
source

pub fn method<V>( get: Option<fn(_: &T) -> V>, set: Option<fn(_: &mut T, _: V)> ) -> Self
where for<'b> V: IntoZval + FromZval<'b> + '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 an Option.
  • set - Function used to set the value of the property, in an Option.
§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));
source

pub fn get(&self, self_: &'a mut T, retval: &mut Zval) -> PhpResult

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 - The Zval 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));
source

pub fn set(&self, self_: &'a mut T, value: &Zval) -> PhpResult

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 - The Zval 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);

Auto Trait Implementations§

§

impl<'a, T> !RefUnwindSafe for Property<'a, T>

§

impl<'a, T> Send for Property<'a, T>

§

impl<'a, T> Sync for Property<'a, T>

§

impl<'a, T> Unpin for Property<'a, T>

§

impl<'a, T> !UnwindSafe for Property<'a, T>

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>,

§

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>,

§

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.