Skip to content

Commit 105e258

Browse files
authored
Adjust module hooks api. (#100)
1 parent cbf1b62 commit 105e258

File tree

4 files changed

+28
-27
lines changed

4 files changed

+28
-27
lines changed

examples/complex/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ pub fn get_module() -> Module {
5050
);
5151

5252
// register hook functions
53-
module.on_module_init(|| true);
54-
module.on_module_shutdown(|| true);
55-
module.on_request_init(|| true);
56-
module.on_request_shutdown(|| true);
53+
module.on_module_init(|| {});
54+
module.on_module_shutdown(|| {});
55+
module.on_request_init(|| {});
56+
module.on_request_shutdown(|| {});
5757

5858
// register functions
5959
module

examples/http-server/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,9 @@ pub fn get_module() -> Module {
4040
module.on_module_init(move || {
4141
let guard = rt_.enter();
4242
forget(guard);
43-
true
4443
});
4544
module.on_module_shutdown(move || {
4645
drop(rt);
47-
true
4846
});
4947

5048
module.add_class(make_exception_class());

phper-doc/doc/_06_module/_01_hooks/index.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ pub fn get_module() -> Module {
3737
3838
module.on_module_init(|| {
3939
// Do somethings in `MINIT` stage.
40-
true
4140
});
4241
4342
module

phper/src/modules.rs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,39 +49,43 @@ unsafe extern "C" fn module_startup(_type: c_int, module_number: c_int) -> c_int
4949
class_entity.declare_properties(ce);
5050
}
5151

52-
match take(&mut module.module_init) {
53-
Some(f) => f() as c_int,
54-
None => 1,
52+
if let Some(f) = take(&mut module.module_init) {
53+
f();
5554
}
55+
56+
ZEND_RESULT_CODE_SUCCESS
5657
}
5758

5859
unsafe extern "C" fn module_shutdown(_type: c_int, module_number: c_int) -> c_int {
5960
let module = GLOBAL_MODULE.as_mut().unwrap();
6061

6162
ini::unregister(module_number);
6263

63-
match take(&mut module.module_shutdown) {
64-
Some(f) => f() as c_int,
65-
None => 1,
64+
if let Some(f) = take(&mut module.module_shutdown) {
65+
f();
6666
}
67+
68+
ZEND_RESULT_CODE_SUCCESS
6769
}
6870

6971
unsafe extern "C" fn request_startup(_type: c_int, _module_number: c_int) -> c_int {
7072
let module = GLOBAL_MODULE.as_ref().unwrap();
7173

72-
match &module.request_init {
73-
Some(f) => f() as c_int,
74-
None => 1,
74+
if let Some(f) = &module.request_init {
75+
f();
7576
}
77+
78+
ZEND_RESULT_CODE_SUCCESS
7679
}
7780

7881
unsafe extern "C" fn request_shutdown(_type: c_int, _module_number: c_int) -> c_int {
7982
let module = GLOBAL_MODULE.as_ref().unwrap();
8083

81-
match &module.request_shutdown {
82-
Some(f) => f() as c_int,
83-
None => 1,
84+
if let Some(f) = &module.request_shutdown {
85+
f();
8486
}
87+
88+
ZEND_RESULT_CODE_SUCCESS
8589
}
8690

8791
unsafe extern "C" fn module_info(zend_module: *mut zend_module_entry) {
@@ -108,10 +112,10 @@ pub struct Module {
108112
name: CString,
109113
version: CString,
110114
author: CString,
111-
module_init: Option<Box<dyn FnOnce() -> bool + Send + Sync>>,
112-
module_shutdown: Option<Box<dyn FnOnce() -> bool + Send + Sync>>,
113-
request_init: Option<Box<dyn Fn() -> bool + Send + Sync>>,
114-
request_shutdown: Option<Box<dyn Fn() -> bool + Send + Sync>>,
115+
module_init: Option<Box<dyn FnOnce()>>,
116+
module_shutdown: Option<Box<dyn FnOnce()>>,
117+
request_init: Option<Box<dyn Fn()>>,
118+
request_shutdown: Option<Box<dyn Fn()>>,
115119
function_entities: Vec<FunctionEntity>,
116120
class_entities: Vec<ClassEntity<()>>,
117121
constants: Vec<Constant>,
@@ -141,22 +145,22 @@ impl Module {
141145
}
142146

143147
/// Register `MINIT` hook.
144-
pub fn on_module_init(&mut self, func: impl FnOnce() -> bool + Send + Sync + 'static) {
148+
pub fn on_module_init(&mut self, func: impl FnOnce() + 'static) {
145149
self.module_init = Some(Box::new(func));
146150
}
147151

148152
/// Register `MSHUTDOWN` hook.
149-
pub fn on_module_shutdown(&mut self, func: impl FnOnce() -> bool + Send + Sync + 'static) {
153+
pub fn on_module_shutdown(&mut self, func: impl FnOnce() + 'static) {
150154
self.module_shutdown = Some(Box::new(func));
151155
}
152156

153157
/// Register `RINIT` hook.
154-
pub fn on_request_init(&mut self, func: impl Fn() -> bool + Send + Sync + 'static) {
158+
pub fn on_request_init(&mut self, func: impl Fn() + 'static) {
155159
self.request_init = Some(Box::new(func));
156160
}
157161

158162
/// Register `RSHUTDOWN` hook.
159-
pub fn on_request_shutdown(&mut self, func: impl Fn() -> bool + Send + Sync + 'static) {
163+
pub fn on_request_shutdown(&mut self, func: impl Fn() + 'static) {
160164
self.request_shutdown = Some(Box::new(func));
161165
}
162166

0 commit comments

Comments
 (0)