Skip to content

Commit 7f4d4c4

Browse files
committed
simplify with macro
1 parent ac93e6b commit 7f4d4c4

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

phper/src/macros.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,15 @@ macro_rules! sg {
109109
$crate::sys::sapi_globals.$x
110110
};
111111
}
112+
113+
/// Define a class stub
114+
#[macro_export]
115+
macro_rules! define_class_stub {
116+
($name:expr, $cls_var:ident) => {{
117+
use std::{cell::RefCell, rc::Rc};
118+
119+
let $cls_var: Rc<RefCell<Option<_>>> = Rc::new(RefCell::new(None));
120+
let entity = ::phper::classes::ClassEntity::new($name);
121+
(entity, $cls_var)
122+
}};
123+
}

tests/integration/src/classes.rs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ use phper::{
1616
types::{ArgumentTypeHint, ReturnTypeHint},
1717
values::ZVal,
1818
};
19-
use std::{collections::HashMap, convert::Infallible};
20-
use std::rc::Rc;
19+
use std::{collections::HashMap, convert::Infallible, rc::Rc};
2120

2221
pub fn integrate(module: &mut Module) {
2322
integrate_a(module);
@@ -235,50 +234,39 @@ fn integrate_bar_extends_foo(module: &mut Module, foo_class: StateClass<Foo>) {
235234
fn integrate_dependent_classes(module: &mut Module) {
236235
const A_CLS: &str = r"IntegrationTest\Dependency\A";
237236
const B_CLS: &str = r"IntegrationTest\Dependency\B";
238-
// Build both class entities
239-
let mut a_cls = ClassEntity::new(A_CLS);
240-
let mut b_cls = ClassEntity::new(B_CLS);
241237

242-
// Placeholder, will clone StateClass into closures after registration
243-
let a_cls_ref = std::rc::Rc::new(std::cell::RefCell::new(None));
244-
let b_cls_ref = std::rc::Rc::new(std::cell::RefCell::new(None));
238+
let (mut a_cls, a_ref) = define_class_stub!(A_CLS, a_ref);
239+
let (mut b_cls, b_ref) = define_class_stub!(B_CLS, b_ref);
245240

246241
{
247-
let b_cls_ref = Rc::clone(&b_cls_ref);
248-
a_cls.add_static_method("createB", Visibility::Public, move |_| {
249-
let borrow = b_cls_ref.borrow();
250-
let b_state: &StateClass<()> = borrow
251-
.as_ref()
252-
.expect("B class not registered");
253-
let obj = b_state.init_object()?;
254-
Ok::<_, phper::Error>(obj)
255-
})
256-
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(String::from(B_CLS))));
242+
let b_ref = Rc::clone(&b_ref);
243+
a_cls
244+
.add_static_method("createB", Visibility::Public, move |_| {
245+
let borrow = b_ref.borrow();
246+
let b_state: &StateClass<()> = borrow.as_ref().unwrap();
247+
Ok::<_, phper::Error>(b_state.init_object()?)
248+
})
249+
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(B_CLS.into())));
257250
}
258251

259252
{
260-
let a_cls_ref = Rc::clone(&a_cls_ref);
261-
b_cls.add_static_method("createA", Visibility::Public, move |_| {
262-
let borrow = a_cls_ref.borrow();
263-
let a_state: &StateClass<()> = borrow
264-
.as_ref()
265-
.expect("A class not registered");
266-
let obj = a_state.init_object()?;
267-
Ok::<_, phper::Error>(obj)
268-
})
269-
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(String::from(A_CLS))));
253+
let a_ref = Rc::clone(&a_ref);
254+
b_cls
255+
.add_static_method("createA", Visibility::Public, move |_| {
256+
let borrow = a_ref.borrow();
257+
let a_state: &StateClass<()> = borrow.as_ref().unwrap();
258+
Ok::<_, phper::Error>(a_state.init_object()?)
259+
})
260+
.return_type(ReturnType::new(ReturnTypeHint::ClassEntry(A_CLS.into())));
270261
}
271262

272-
273-
// Register both classes and save the StateClass into shared RefCells
263+
// Register both classes
274264
let a_state = module.add_class(a_cls);
275265
let b_state = module.add_class(b_cls);
276-
277-
*a_cls_ref.borrow_mut() = Some(a_state);
278-
*b_cls_ref.borrow_mut() = Some(b_state);
266+
*a_ref.borrow_mut() = Some(a_state.clone());
267+
*b_ref.borrow_mut() = Some(b_state.clone());
279268
}
280269

281-
282270
#[cfg(phper_major_version = "8")]
283271
fn integrate_stringable(module: &mut Module) {
284272
use phper::{functions::ReturnType, types::ReturnTypeHint};

tests/integration/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
// See the Mulan PSL v2 for more details.
1010

1111
#![warn(rust_2018_idioms, clippy::dbg_macro, clippy::print_stdout)]
12+
#[macro_use]
13+
extern crate phper;
1214

1315
mod arguments;
1416
mod arrays;

0 commit comments

Comments
 (0)