From 0031df1604b5e93f7a0befe675eb57f0a35b5105 Mon Sep 17 00:00:00 2001 From: David Cole Date: Sat, 31 Jul 2021 20:44:46 +1200 Subject: [PATCH] `ClassEntry::build` now returns a reference Useful for when you want to throw a custom exception. Store the reference to the class (which should extend one of the base exceptions) in a static mutable variable, and then use `throw`. --- src/php/class.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/php/class.rs b/src/php/class.rs index 8122094..03e6837 100644 --- a/src/php/class.rs +++ b/src/php/class.rs @@ -66,7 +66,7 @@ impl<'a> ClassBuilder<'a> { /// # Parameters /// /// * `parent` - The parent class to extend. - pub fn extends(mut self, parent: &ClassEntry) -> Self { + pub fn extends(mut self, parent: &'static ClassEntry) -> Self { self.extends = (parent as *const _) as *mut _; self } @@ -156,17 +156,17 @@ impl<'a> ClassBuilder<'a> { self } - /// Builds the class, returning a pointer to the class entry. - pub fn build(mut self) -> *mut ClassEntry { + /// Builds the class, returning a reference to the class entry. + /// + /// # Errors + /// + /// Returns `None` if the class could not be built. + pub fn build(mut self) -> Option<&'static mut ClassEntry> { self.methods.push(FunctionEntry::end()); let func = Box::into_raw(self.methods.into_boxed_slice()) as *const FunctionEntry; self.ptr.info.internal.builtin_functions = func; - let class = unsafe { - zend_register_internal_class_ex(self.ptr, self.extends) - .as_mut() - .unwrap() - }; + let class = unsafe { zend_register_internal_class_ex(self.ptr, self.extends).as_mut()? }; unsafe { libc::free((self.ptr as *mut ClassEntry) as *mut libc::c_void) }; @@ -191,6 +191,6 @@ impl<'a> ClassBuilder<'a> { class.__bindgen_anon_2.create_object = Some(object_override); } - class + Some(class) } }