Struct ext_php_rs::closure::Closure

source ·
pub struct Closure(/* private fields */);
Available on crate feature closure only.
Expand description

Wrapper around a Rust closure, which can be exported to PHP.

Closures can have up to 8 parameters, all must implement FromZval, and can return anything that implements IntoZval. Closures must have a static lifetime, and therefore cannot modify any self references.

Internally, closures are implemented as a PHP class. A class RustClosure is registered with an __invoke method:

<?php

class RustClosure {
    public function __invoke(...$args): mixed {
        // ...    
    }
}

The Rust closure is then double boxed, firstly as a Box<dyn Fn(...) -> ...> (depending on the signature of the closure) and then finally boxed as a Box<dyn PhpClosure>. This is a workaround, as PhpClosure is not generically implementable on types that implement Fn(T, ...) -> Ret. Make a suggestion issue if you have a better idea of implementing this!.

When the __invoke method is called from PHP, the invoke method is called on the dyn PhpClosure\ trait object, and from there everything is basically the same as a regular PHP function.

Implementations§

source§

impl Closure

source

pub fn wrap<T>(func: T) -> Self
where T: PhpClosure + 'static,

Wraps a Fn or FnMut Rust closure into a type which can be returned to PHP.

The closure can accept up to 8 arguments which implement IntoZval, and can return any type which implements FromZval. The closure must have a static lifetime, so cannot reference self.

§Parameters
  • func - The closure to wrap. Should be boxed in the form Box<dyn Fn[Mut](...) -> ...>.
§Example
use ext_php_rs::closure::Closure;

let closure = Closure::wrap(Box::new(|name| {
    format!("Hello {}", name)
}) as Box<dyn Fn(String) -> String>);
source

pub fn wrap_once<T>(func: T) -> Self
where T: PhpOnceClosure + 'static,

Wraps a FnOnce Rust closure into a type which can be returned to PHP. If the closure is called more than once from PHP, an exception is thrown.

The closure can accept up to 8 arguments which implement IntoZval, and can return any type which implements FromZval. The closure must have a static lifetime, so cannot reference self.

§Parameters
  • func - The closure to wrap. Should be boxed in the form Box<dyn FnOnce(...) -> ...>.
§Example
use ext_php_rs::closure::Closure;

let name: String = "Hello world".into();
let closure = Closure::wrap_once(Box::new(|| {
    name
}) as Box<dyn FnOnce() -> String>);
source

pub fn build()

Builds the class entry for Closure, registering it with PHP. This function should only be called once inside your module startup function.

§Panics

Panics if the function is called more than once.

Trait Implementations§

source§

impl<'a> FromZendObject<'a> for &'a Closure

source§

fn from_zend_object(obj: &'a ZendObject) -> Result<Self>

Extracts Self from the source ZendObject.
source§

impl<'a> FromZendObjectMut<'a> for &'a mut Closure

source§

fn from_zend_object_mut(obj: &'a mut ZendObject) -> Result<Self>

Extracts Self from the source ZendObject.
source§

impl<'a> FromZval<'a> for &'a Closure

source§

const TYPE: DataType = _

The corresponding type of the implemented value in PHP.
source§

fn from_zval(zval: &'a Zval) -> Option<Self>

Attempts to retrieve an instance of Self from a reference to a Zval. Read more
source§

impl<'a> FromZvalMut<'a> for &'a mut Closure

source§

const TYPE: DataType = _

The corresponding type of the implemented value in PHP.
source§

fn from_zval_mut(zval: &'a mut Zval) -> Option<Self>

Attempts to retrieve an instance of Self from a mutable reference to a Zval. Read more
source§

impl IntoZendObject for Closure

source§

fn into_zend_object(self) -> Result<ZBox<ZendObject>>

Attempts to convert self into a Zend object.
source§

impl IntoZval for Closure

source§

const TYPE: DataType = _

The corresponding type of the implemented value in PHP.
source§

fn set_zval(self, zv: &mut Zval, persistent: bool) -> Result<()>

Sets the content of a pre-existing zval. Returns a result containing nothing if setting the content was successful. Read more
source§

fn into_zval(self, persistent: bool) -> Result<Zval>

Converts a Rust primitive type into a Zval. Returns a result containing the Zval if successful. Read more
source§

impl RegisteredClass for Closure

source§

const CLASS_NAME: &'static str = "RustClosure"

PHP class name of the registered class.
source§

fn get_metadata() -> &'static ClassMetadata<Self>

Returns a reference to the class metadata, which stores the class entry and handlers. Read more
source§

fn get_properties<'a>() -> HashMap<&'static str, Property<'a, Self>>

Returns a hash table containing the properties of the class. Read more
source§

const CONSTRUCTOR: Option<ConstructorMeta<Self>> = None

Optional class constructor.
source§

impl Send for Closure

source§

impl Sync for Closure

Auto Trait Implementations§

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.