Skip to content

Commit 29bfb06

Browse files
authored
feat: Add raw pointer casting methods for EBox (#213)
1 parent 09abdf4 commit 29bfb06

File tree

7 files changed

+38
-18
lines changed

7 files changed

+38
-18
lines changed

phper/src/alloc.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,34 @@ impl<T> EBox<T> {
3737
Self { ptr: raw }
3838
}
3939

40+
/// Constructs from a raw pointer with cast.
41+
///
42+
/// # Safety
43+
///
44+
/// Make sure the pointer is from `into_raw`, or created from `emalloc`.
45+
pub(crate) unsafe fn from_raw_cast<U>(raw: *mut U) -> Self {
46+
const {
47+
assert!(size_of::<U>() == size_of::<T>());
48+
}
49+
Self { ptr: raw.cast() }
50+
}
51+
4052
/// Consumes and returning a wrapped raw pointer.
4153
///
4254
/// Will leak memory.
4355
pub fn into_raw(b: EBox<T>) -> *mut T {
4456
ManuallyDrop::new(b).ptr
4557
}
58+
59+
/// Consumes and returning a wrapped raw pointer with cast.
60+
///
61+
/// Will leak memory.
62+
pub(crate) fn into_raw_cast<U>(b: EBox<T>) -> *mut U {
63+
const {
64+
assert!(size_of::<U>() == size_of::<T>());
65+
}
66+
ManuallyDrop::new(b).ptr.cast()
67+
}
4668
}
4769

4870
impl<T: fmt::Debug> fmt::Debug for EBox<T> {

phper/src/arrays.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ impl ToOwned for ZArr {
356356
unsafe {
357357
// TODO The source really immutable?
358358
let dest = phper_zend_array_dup(self.as_ptr() as *mut _);
359-
ZArray::from_raw(dest.cast())
359+
ZArray::from_raw_cast(dest)
360360
}
361361
}
362362
}
@@ -369,7 +369,7 @@ impl ToRefOwned for ZArr {
369369
unsafe {
370370
phper_zval_arr(val.as_mut_ptr(), self.as_mut_ptr());
371371
phper_z_addref_p(val.as_mut_ptr());
372-
ZArray::from_raw(val.as_mut_z_arr().unwrap().as_mut_ptr().cast())
372+
ZArray::from_raw_cast(val.as_mut_z_arr().unwrap().as_mut_ptr())
373373
}
374374
}
375375
}
@@ -395,7 +395,7 @@ impl ZArray {
395395
pub fn with_capacity(n: usize) -> Self {
396396
unsafe {
397397
let ptr = phper_zend_new_array(n.try_into().unwrap());
398-
Self::from_raw(ptr.cast())
398+
Self::from_raw_cast(ptr)
399399
}
400400
}
401401
}

phper/src/classes.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl ClassEntry {
147147
// day of debugging time here).
148148
let mut val = ManuallyDrop::new(val);
149149
let ptr = phper_z_obj_p(val.as_mut_ptr());
150-
Ok(ZObject::from_raw(ptr.cast()))
150+
Ok(ZObject::from_raw_cast(ptr))
151151
}
152152
}
153153
}
@@ -334,8 +334,7 @@ impl<T: 'static> StateClass<T> {
334334
pub fn new_object(&self, arguments: impl AsMut<[ZVal]>) -> crate::Result<StateObject<T>> {
335335
self.as_class_entry()
336336
.new_object(arguments)
337-
.map(ZObject::into_raw)
338-
.map(|ptr| ptr.cast())
337+
.map(ZObject::into_raw_cast)
339338
.map(StateObject::<T>::from_raw_object)
340339
}
341340

@@ -345,8 +344,7 @@ impl<T: 'static> StateClass<T> {
345344
pub fn init_object(&self) -> crate::Result<StateObject<T>> {
346345
self.as_class_entry()
347346
.init_object()
348-
.map(ZObject::into_raw)
349-
.map(|ptr| ptr.cast())
347+
.map(ZObject::into_raw_cast)
350348
.map(StateObject::<T>::from_raw_object)
351349
}
352350
}

phper/src/functions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ impl ZFunc {
651651
pub fn get_function_or_method_name(&self) -> ZString {
652652
unsafe {
653653
let s = phper_get_function_or_method_name(self.as_ptr());
654-
ZString::from_raw(s.cast())
654+
ZString::from_raw_cast(s)
655655
}
656656
}
657657

@@ -890,7 +890,7 @@ pub(crate) fn call_raw_common(call_fn: impl FnOnce(&mut ZVal)) -> crate::Result<
890890
if !eg!(exception).is_null() {
891891
#[allow(static_mut_refs)]
892892
let e = ptr::replace(&mut eg!(exception), null_mut());
893-
let obj = ZObject::from_raw(e.cast());
893+
let obj = ZObject::from_raw_cast(e);
894894
match ThrowObject::new(obj) {
895895
Ok(e) => return Err(e.into()),
896896
Err(e) => return Err(e.into()),

phper/src/objects.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ impl ToRefOwned for ZObj {
282282
unsafe {
283283
phper_zval_obj(val.as_mut_ptr(), self.as_mut_ptr());
284284
phper_z_addref_p(val.as_mut_ptr());
285-
ZObject::from_raw(val.as_mut_z_obj().unwrap().as_mut_ptr().cast())
285+
ZObject::from_raw_cast(val.as_mut_z_obj().unwrap().as_mut_ptr())
286286
}
287287
}
288288
}
@@ -447,7 +447,7 @@ impl<T> StateObject<T> {
447447

448448
/// Converts into [ZObject].
449449
pub fn into_z_object(self) -> ZObject {
450-
unsafe { ZObject::from_raw(self.into_raw_object().cast()) }
450+
unsafe { ZObject::from_raw_cast(self.into_raw_object()) }
451451
}
452452
}
453453

phper/src/strings.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ impl ToRefOwned for ZStr {
175175
fn to_ref_owned(&mut self) -> Self::Owned {
176176
unsafe {
177177
let ptr = phper_zend_string_copy(self.as_mut_ptr());
178-
ZString::from_raw(ptr.cast())
178+
ZString::from_raw_cast(ptr)
179179
}
180180
}
181181
}
@@ -198,7 +198,7 @@ impl ZString {
198198
s.len().try_into().unwrap(),
199199
false.into(),
200200
);
201-
Self::from_raw(ptr.cast())
201+
Self::from_raw_cast(ptr)
202202
}
203203
}
204204

@@ -209,7 +209,7 @@ impl ZString {
209209
let s = s.as_ref();
210210
let ptr =
211211
phper_zend_string_init(s.as_ptr().cast(), s.len().try_into().unwrap(), true.into());
212-
Self::from_raw(ptr.cast())
212+
Self::from_raw_cast(ptr)
213213
}
214214
}
215215
}
@@ -222,7 +222,7 @@ impl Clone for ZString {
222222
phper_zstr_len(self.as_ptr()).try_into().unwrap(),
223223
false.into(),
224224
);
225-
Self::from_raw(ZStr::from_mut_ptr(ptr.cast()))
225+
Self::from_raw_cast(ZStr::from_mut_ptr(ptr))
226226
}
227227
}
228228
}

phper/src/values.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ impl From<ZString> for ZVal {
736736
fn from(s: ZString) -> Self {
737737
unsafe {
738738
let mut val = MaybeUninit::<ZVal>::uninit();
739-
phper_zval_str(val.as_mut_ptr().cast(), ZString::into_raw(s).cast());
739+
phper_zval_str(val.as_mut_ptr().cast(), ZString::into_raw_cast(s));
740740
val.assume_init()
741741
}
742742
}
@@ -746,7 +746,7 @@ impl From<ZArray> for ZVal {
746746
fn from(arr: ZArray) -> Self {
747747
unsafe {
748748
let mut val = MaybeUninit::<ZVal>::uninit();
749-
phper_zval_arr(val.as_mut_ptr().cast(), ZArray::into_raw(arr).cast());
749+
phper_zval_arr(val.as_mut_ptr().cast(), ZArray::into_raw_cast(arr));
750750
val.assume_init()
751751
}
752752
}

0 commit comments

Comments
 (0)