Skip to content

Commit 5230fb1

Browse files
committed
refactor: replace EBox with ZArray and ZString in multiple files
1 parent d36f785 commit 5230fb1

File tree

7 files changed

+31
-48
lines changed

7 files changed

+31
-48
lines changed

examples/complex/src/lib.rs

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

1111
use phper::{
12-
alloc::EBox,
13-
arrays::ZArr,
12+
arrays::ZArray,
1413
classes::{ClassEntity, Visibility},
1514
functions::Argument,
1615
ini::{Policy, ini_get},
@@ -62,7 +61,7 @@ pub fn get_module() -> Module {
6261
.argument(Argument::new("name"));
6362
module.add_function("complex_throw_exception", throw_exception);
6463
module.add_function("complex_get_all_ini", |_: &mut [ZVal]| {
65-
let mut arr = EBox::<ZArr>::new();
64+
let mut arr = ZArray::new();
6665

6766
let complex_enable = ZVal::from(ini_get::<bool>("complex.enable"));
6867
arr.insert("complex.enable", complex_enable);

examples/http-client/src/response.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
use crate::errors::HttpClientError;
1212
use phper::{
13-
alloc::EBox,
14-
arrays::{InsertKey, ZArr},
13+
arrays::{InsertKey, ZArray},
1514
classes::{ClassEntity, StateClass, Visibility},
1615
values::ZVal,
1716
};
@@ -51,19 +50,16 @@ pub fn make_response_class() -> ClassEntity<Option<Response>> {
5150
.ok_or_else(|| HttpClientError::ResponseAfterRead {
5251
method_name: "headers".to_owned(),
5352
})?;
54-
let headers_map =
55-
response
56-
.headers()
57-
.iter()
58-
.fold(EBox::<ZArr>::new(), |mut acc, (key, value)| {
59-
let arr = acc
60-
.entry(key.as_str())
61-
.or_insert(ZVal::from(EBox::<ZArr>::new()));
62-
arr.as_mut_z_arr()
63-
.unwrap()
64-
.insert(InsertKey::NextIndex, ZVal::from(value.as_bytes()));
65-
acc
66-
});
53+
let headers_map = response
54+
.headers()
55+
.iter()
56+
.fold(ZArray::new(), |mut acc, (key, value)| {
57+
let arr = acc.entry(key.as_str()).or_insert(ZVal::from(ZArray::new()));
58+
arr.as_mut_z_arr()
59+
.unwrap()
60+
.insert(InsertKey::NextIndex, ZVal::from(value.as_bytes()));
61+
acc
62+
});
6763
Ok::<_, HttpClientError>(headers_map)
6864
});
6965

examples/http-server/src/request.rs

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

1111
use phper::{
12-
alloc::EBox,
13-
arrays::ZArr,
12+
arrays::ZArray,
1413
classes::{ClassEntity, StateClass, Visibility},
1514
};
1615
use std::convert::Infallible;
@@ -32,7 +31,7 @@ pub fn make_request_class() -> ClassEntity<()> {
3231
// Register the constructor method with public visibility, initialize the
3332
// headers with empty array.
3433
class.add_method("__construct", Visibility::Public, |this, _arguments| {
35-
this.set_property("headers", EBox::<ZArr>::new());
34+
this.set_property("headers", ZArray::new());
3635
Ok::<_, Infallible>(())
3736
});
3837

phper/src/arrays.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,6 @@ impl Debug for ZArr {
347347
}
348348
}
349349

350-
#[allow(deprecated)]
351350
impl ToOwned for ZArr {
352351
type Owned = ZArray;
353352

@@ -399,7 +398,6 @@ impl ZArray {
399398
}
400399
}
401400

402-
#[allow(deprecated)]
403401
impl Default for ZArray {
404402
fn default() -> Self {
405403
Self::new()

phper/src/classes.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,6 @@ impl ClassEntry {
127127
///
128128
/// If the `__construct` is private, or protected and the called scope isn't
129129
/// parent class, it will throw PHP Error.
130-
#[allow(deprecated)]
131130
pub fn new_object(&self, arguments: impl AsMut<[ZVal]>) -> crate::Result<ZObject> {
132131
let mut object = self.init_object()?;
133132
object.call_construct(arguments)?;
@@ -137,7 +136,6 @@ impl ClassEntry {
137136
/// Create the object from class, without calling `__construct`.
138137
///
139138
/// **Be careful when `__construct` is necessary.**
140-
#[allow(deprecated)]
141139
pub fn init_object(&self) -> crate::Result<ZObject> {
142140
unsafe {
143141
let ptr = self.as_ptr() as *mut _;
@@ -333,7 +331,6 @@ impl<T: 'static> StateClass<T> {
333331
///
334332
/// If the `__construct` is private, or protected and the called scope isn't
335333
/// parent class, it will throw PHP Error.
336-
#[allow(deprecated)]
337334
pub fn new_object(&self, arguments: impl AsMut<[ZVal]>) -> crate::Result<StateObject<T>> {
338335
self.as_class_entry()
339336
.new_object(arguments)
@@ -345,7 +342,6 @@ impl<T: 'static> StateClass<T> {
345342
/// Create the object from class, without calling `__construct`.
346343
///
347344
/// **Be careful when `__construct` is necessary.**
348-
#[allow(deprecated)]
349345
pub fn init_object(&self) -> crate::Result<StateObject<T>> {
350346
self.as_class_entry()
351347
.init_object()

phper/src/enums.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@
2020
#![cfg(phper_enum_supported)]
2121

2222
use crate::{
23-
alloc::EBox,
2423
classes::{
2524
ClassEntry, ConstantEntity, InnerClassEntry, Interface, Visibility, add_class_constant,
2625
},
2726
errors::Throwable,
2827
functions::{Function, FunctionEntry, HandlerMap, MethodEntity},
2928
objects::ZObj,
30-
strings::ZStr,
29+
strings::ZString,
3130
sys::*,
3231
types::Scalar,
3332
utils::ensure_end_with_zero,
@@ -261,7 +260,7 @@ impl Enum {
261260
unsafe {
262261
let ce = self.as_class_entry().as_ptr() as *mut _;
263262
let case_name_str = case_name.as_ref();
264-
let mut name_zstr = EBox::<ZStr>::new(case_name_str);
263+
let mut name_zstr = ZString::new(case_name_str);
265264

266265
// Get the enum case
267266
let case_obj = zend_enum_get_case(ce, name_zstr.as_mut_ptr());
@@ -297,7 +296,7 @@ impl Enum {
297296
unsafe {
298297
let ce = self.as_class_entry().as_ptr() as *mut _;
299298
let case_name_str = case_name.as_ref();
300-
let mut name_zstr = EBox::<ZStr>::new(case_name_str);
299+
let mut name_zstr = ZString::new(case_name_str);
301300

302301
// Get the enum case
303302
let case_obj = zend_enum_get_case(ce, name_zstr.as_mut_ptr());
@@ -529,7 +528,7 @@ unsafe fn register_enum_case(
529528
);
530529
}
531530
Scalar::String(value) => {
532-
let value = EBox::<ZStr>::new_persistent(value);
531+
let value = ZString::new_persistent(value);
533532
let mut value = ManuallyDrop::new(ZVal::from(value));
534533
zend_enum_add_case_cstr(class_ce, case_name.as_ptr(), value.as_mut_ptr());
535534
}

phper/src/errors.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010

1111
//! The errors for crate and php.
1212
13-
use crate::{
14-
alloc::EBox, classes::ClassEntry, objects::ZObj, sys::*, types::TypeInfo, values::ZVal,
15-
};
13+
use crate::{classes::ClassEntry, objects::ZObject, sys::*, types::TypeInfo, values::ZVal};
1614
use derive_more::Constructor;
1715
use phper_alloc::ToRefOwned;
1816
use std::{
@@ -107,9 +105,9 @@ pub trait Throwable: error::Error {
107105
///
108106
/// By default, the Exception is instance of `get_class()`, the code is
109107
/// `get_code` and message is `get_message`;
110-
fn to_object(&mut self) -> result::Result<EBox<ZObj>, Box<dyn Throwable>> {
111-
let mut object = EBox::<ZObj>::new(self.get_class(), [])
112-
.map_err(|e| Box::new(e) as Box<dyn Throwable>)?;
108+
fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
109+
let mut object =
110+
ZObject::new(self.get_class(), []).map_err(|e| Box::new(e) as Box<dyn Throwable>)?;
113111
if let Some(code) = self.get_code() {
114112
object.set_property("code", code);
115113
}
@@ -133,8 +131,7 @@ impl<T: Throwable> Throwable for Box<T> {
133131
Throwable::get_message(self.deref())
134132
}
135133

136-
#[allow(deprecated)]
137-
fn to_object(&mut self) -> result::Result<EBox<ZObj>, Box<dyn Throwable>> {
134+
fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
138135
Throwable::to_object(self.deref_mut())
139136
}
140137
}
@@ -158,8 +155,7 @@ impl Throwable for Infallible {
158155
match *self {}
159156
}
160157

161-
#[allow(deprecated)]
162-
fn to_object(&mut self) -> result::Result<EBox<ZObj>, Box<dyn Throwable>> {
158+
fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
163159
match *self {}
164160
}
165161
}
@@ -278,7 +274,7 @@ impl Throwable for Error {
278274
}
279275
}
280276

281-
fn to_object(&mut self) -> result::Result<EBox<ZObj>, Box<dyn Throwable>> {
277+
fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
282278
match self {
283279
Error::Io(e) => Throwable::to_object(e as &mut dyn error::Error),
284280
Error::Utf8(e) => Throwable::to_object(e as &mut dyn error::Error),
@@ -296,13 +292,13 @@ impl Throwable for Error {
296292

297293
/// Wrapper of Throwable object.
298294
#[derive(Debug)]
299-
pub struct ThrowObject(EBox<ZObj>);
295+
pub struct ThrowObject(ZObject);
300296

301297
impl ThrowObject {
302298
/// Construct from Throwable object.
303299
///
304300
/// Failed if the object is not instance of php `Throwable`.
305-
pub fn new(obj: EBox<ZObj>) -> result::Result<Self, NotImplementThrowableError> {
301+
pub fn new(obj: ZObject) -> result::Result<Self, NotImplementThrowableError> {
306302
if !obj.get_class().is_instance_of(throwable_class()) {
307303
return Err(NotImplementThrowableError);
308304
}
@@ -322,7 +318,7 @@ impl ThrowObject {
322318
Self::from_result(Throwable::to_object(e))
323319
}
324320

325-
fn from_result(mut result: result::Result<EBox<ZObj>, Box<dyn Throwable>>) -> Self {
321+
fn from_result(mut result: result::Result<ZObject, Box<dyn Throwable>>) -> Self {
326322
let mut i = 0;
327323

328324
let obj = loop {
@@ -343,7 +339,7 @@ impl ThrowObject {
343339

344340
/// Consumes the `ThrowObject`, returning the wrapped object.
345341
#[inline]
346-
pub fn into_inner(self) -> EBox<ZObj> {
342+
pub fn into_inner(self) -> ZObject {
347343
self.0
348344
}
349345

@@ -390,7 +386,7 @@ impl Throwable for ThrowObject {
390386
}
391387

392388
#[inline]
393-
fn to_object(&mut self) -> result::Result<EBox<ZObj>, Box<dyn Throwable>> {
389+
fn to_object(&mut self) -> result::Result<ZObject, Box<dyn Throwable>> {
394390
Ok(self.0.to_ref_owned())
395391
}
396392
}

0 commit comments

Comments
 (0)