Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit29bfb06

Browse files
authored
feat: Add raw pointer casting methods for EBox (#213)
1 parent09abdf4 commit29bfb06

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)unsafefnfrom_raw_cast<U>(raw:*mutU) ->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
pubfninto_raw(b:EBox<T>) ->*mutT{
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)fninto_raw_cast<U>(b:EBox<T>) ->*mutU{
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::DebugforEBox<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
pubfnwith_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
letmut 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
pubfnnew_object(&self,arguments:implAsMut<[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
pubfninit_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
pubfnget_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(&muteg!(exception),null_mut());
893-
let obj =ZObject::from_raw(e.cast());
893+
let obj =ZObject::from_raw_cast(e);
894894
matchThrowObject::new(obj){
895895
Ok(e) =>returnErr(e.into()),
896896
Err(e) =>returnErr(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
pubfninto_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
fnto_ref_owned(&mutself) ->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
fnfrom(s:ZString) ->Self{
737737
unsafe{
738738
letmut 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
fnfrom(arr:ZArray) ->Self{
747747
unsafe{
748748
letmut 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)

[8]ページ先頭

©2009-2025 Movatter.jp