Skip to content

Commit 0031df1

Browse files
committed
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`.
1 parent e946066 commit 0031df1

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/php/class.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ impl<'a> ClassBuilder<'a> {
6666
/// # Parameters
6767
///
6868
/// * `parent` - The parent class to extend.
69-
pub fn extends(mut self, parent: &ClassEntry) -> Self {
69+
pub fn extends(mut self, parent: &'static ClassEntry) -> Self {
7070
self.extends = (parent as *const _) as *mut _;
7171
self
7272
}
@@ -156,17 +156,17 @@ impl<'a> ClassBuilder<'a> {
156156
self
157157
}
158158

159-
/// Builds the class, returning a pointer to the class entry.
160-
pub fn build(mut self) -> *mut ClassEntry {
159+
/// Builds the class, returning a reference to the class entry.
160+
///
161+
/// # Errors
162+
///
163+
/// Returns `None` if the class could not be built.
164+
pub fn build(mut self) -> Option<&'static mut ClassEntry> {
161165
self.methods.push(FunctionEntry::end());
162166
let func = Box::into_raw(self.methods.into_boxed_slice()) as *const FunctionEntry;
163167
self.ptr.info.internal.builtin_functions = func;
164168

165-
let class = unsafe {
166-
zend_register_internal_class_ex(self.ptr, self.extends)
167-
.as_mut()
168-
.unwrap()
169-
};
169+
let class = unsafe { zend_register_internal_class_ex(self.ptr, self.extends).as_mut()? };
170170

171171
unsafe { libc::free((self.ptr as *mut ClassEntry) as *mut libc::c_void) };
172172

@@ -191,6 +191,6 @@ impl<'a> ClassBuilder<'a> {
191191
class.__bindgen_anon_2.create_object = Some(object_override);
192192
}
193193

194-
class
194+
Some(class)
195195
}
196196
}

0 commit comments

Comments
 (0)