pub struct Closure(/* private fields */);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
impl Closure
Sourcepub fn wrap<T>(func: T) -> Selfwhere
T: PhpClosure + 'static,
pub fn wrap<T>(func: T) -> Selfwhere
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 formBox<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>);Sourcepub fn wrap_once<T>(func: T) -> Selfwhere
T: PhpOnceClosure + 'static,
pub fn wrap_once<T>(func: T) -> Selfwhere
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 formBox<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>);Sourcepub fn build()
pub fn build()
Builds the class entry for Closure, registering it with PHP. The
generated module startup function calls it once per MINIT, including
when the SAPI starts the module again in the same process (e.g. a
FrankenPHP worker restart), whose engine no longer has the class.
§Panics
Panics if the RustClosure PHP class cannot be registered.
Trait Implementations§
Source§impl IntoZendObject for Closure
impl IntoZendObject for Closure
Source§fn into_zend_object(self) -> Result<ZBox<ZendObject>>
fn into_zend_object(self) -> Result<ZBox<ZendObject>>
self into a Zend object. Read moreSource§impl IntoZval for Closure
impl IntoZval for Closure
impl PhpClass for Closure
Source§impl RegisteredClass for Closure
impl RegisteredClass for Closure
Source§const CLASS_NAME: &'static str = "RustClosure"
const CLASS_NAME: &'static str = "RustClosure"
Source§const BUILDER_MODIFIER: Option<fn(ClassBuilder) -> ClassBuilder> = None
const BUILDER_MODIFIER: Option<fn(ClassBuilder) -> ClassBuilder> = None
Source§const EXTENDS: Option<ClassEntryInfo> = None
const EXTENDS: Option<ClassEntryInfo> = None
Source§const IMPLEMENTS: &'static [ClassEntryInfo]
const IMPLEMENTS: &'static [ClassEntryInfo]
Source§fn get_metadata() -> &'static ClassMetadata<Self>
fn get_metadata() -> &'static ClassMetadata<Self>
Source§fn method_builders() -> Vec<(FunctionBuilder<'static>, MethodFlags)>
fn method_builders() -> Vec<(FunctionBuilder<'static>, MethodFlags)>
Source§fn constructor() -> Option<ConstructorMeta<Self>>
fn constructor() -> Option<ConstructorMeta<Self>>
Source§fn constants() -> &'static [(&'static str, &'static dyn IntoZvalDyn, DocComments, ConstantFlags)]
fn constants() -> &'static [(&'static str, &'static dyn IntoZvalDyn, DocComments, ConstantFlags)]
Source§const FLAGS: ClassFlags
const FLAGS: ClassFlags
Source§const NO_CONSTRUCTOR: bool = false
const NO_CONSTRUCTOR: bool = false
#[php(no_constructor)]: new then creates an object
initialized with Default::default(), as for a PHP class without a
constructor. Otherwise a class without a constructor gets one that
throws.Source§const DOC_COMMENTS: DocComments
const DOC_COMMENTS: DocComments
Source§fn method_properties() -> &'static [PropertyDescriptor<Self>]
fn method_properties() -> &'static [PropertyDescriptor<Self>]
#[php(getter)]/#[php(setter)]).
Resolved via autoref specialization in the macro-generated code.Source§fn static_properties() -> &'static [(&'static str, PropertyFlags, Option<&'static (dyn IntoZvalDyn + Sync)>, DocComments)]
fn static_properties() -> &'static [(&'static str, PropertyFlags, Option<&'static (dyn IntoZvalDyn + Sync)>, DocComments)]
Source§fn interface_implementations() -> Vec<ClassEntryInfo>
fn interface_implementations() -> Vec<ClassEntryInfo>
#[php_impl_interface] trait implementations. Read moreSource§fn interface_method_implementations() -> Vec<(FunctionBuilder<'static>, MethodFlags)>
fn interface_method_implementations() -> Vec<(FunctionBuilder<'static>, MethodFlags)>
#[php_impl_interface] trait implementations. Read more