Skip to content

Commit edf7f35

Browse files
committed
extract unsafe ZeroedSizeArray trait
1 parent 4eccd3c commit edf7f35

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/codegen/mod.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ impl CodeGenerator for Module {
353353
if saw_union && !ctx.options().unstable_rust {
354354
utils::prepend_union_types(ctx, &mut *result);
355355
}
356-
let saw_incomplete_array = result.saw_incomplete_array;
357-
if saw_incomplete_array && !ctx.options().unstable_rust {
356+
if result.saw_incomplete_array {
358357
utils::prepend_incomplete_array_types(ctx, &mut *result);
359358
}
360359
if ctx.need_bindegen_complex_type() {
@@ -2360,8 +2359,6 @@ mod utils {
23602359
result: &mut Vec<P<ast::Item>>) {
23612360
let prefix = ctx.trait_prefix();
23622361

2363-
// TODO(emilio): The fmt::Debug impl could be way nicer with
2364-
// std::intrinsics::type_name, but...
23652362
let incomplete_array_decl = quote_item!(ctx.ext_cx(),
23662363
#[repr(C)]
23672364
pub struct __IncompleteArrayField<T>(
@@ -2376,14 +2373,24 @@ mod utils {
23762373
__IncompleteArrayField(::$prefix::marker::PhantomData)
23772374
}
23782375

2376+
#[inline]
2377+
pub unsafe fn as_ptr(&self) -> *const T {
2378+
::$prefix::mem::transmute(self)
2379+
}
2380+
2381+
#[inline]
2382+
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
2383+
::$prefix::mem::transmute(self)
2384+
}
2385+
23792386
#[inline]
23802387
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
2381-
::std::slice::from_raw_parts(::std::mem::transmute(self), len)
2388+
::std::slice::from_raw_parts(self.as_ptr(), len)
23822389
}
23832390

23842391
#[inline]
23852392
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
2386-
::std::slice::from_raw_parts_mut(::std::mem::transmute(self), len)
2393+
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
23872394
}
23882395
}
23892396
)

tests/expectations/tests/class.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,18 @@ impl <T> __IncompleteArrayField<T> {
1212
__IncompleteArrayField(::std::marker::PhantomData)
1313
}
1414
#[inline]
15+
pub unsafe fn as_ptr(&self) -> *const T { ::std::mem::transmute(self) }
16+
#[inline]
17+
pub unsafe fn as_mut_ptr(&mut self) -> *mut T {
18+
::std::mem::transmute(self)
19+
}
20+
#[inline]
1521
pub unsafe fn as_slice(&self, len: usize) -> &[T] {
16-
::std::slice::from_raw_parts(::std::mem::transmute(self), len)
22+
::std::slice::from_raw_parts(self.as_ptr(), len)
1723
}
1824
#[inline]
1925
pub unsafe fn as_mut_slice(&mut self, len: usize) -> &mut [T] {
20-
::std::slice::from_raw_parts_mut(::std::mem::transmute(self), len)
26+
::std::slice::from_raw_parts_mut(self.as_mut_ptr(), len)
2127
}
2228
}
2329
impl <T> ::std::fmt::Debug for __IncompleteArrayField<T> {

0 commit comments

Comments
 (0)