mirror of
https://github.com/openharmony/third_party_rust_memoffset.git
synced 2026-07-18 12:25:36 -04:00
Merge pull request #46 from RalfJung/no-transmute
use MaybeUninit::as_ptr instead of transmute
This commit is contained in:
@@ -68,24 +68,10 @@ features = ["unstable_const"]
|
||||
|
||||
Your crate root: (`lib.rs`/`main.rs`)
|
||||
```rust,ignore
|
||||
#![feature(ptr_offset_from, const_ptr_offset_from, const_raw_ptr_deref)]
|
||||
#![feature(ptr_offset_from, const_ptr_offset_from, const_maybe_uninit_as_ptr, const_raw_ptr_deref)]
|
||||
```
|
||||
|
||||
Or, if you intend to use `offset_of!` inside a `const fn`:
|
||||
```rust,ignore
|
||||
#![feature(ptr_offset_from, const_fn, const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref)]
|
||||
```
|
||||
|
||||
and then:
|
||||
|
||||
```rust,ignore
|
||||
struct Foo {
|
||||
a: u32,
|
||||
b: u32,
|
||||
}
|
||||
|
||||
let foo = [0; offset_of!(Foo, b)]
|
||||
```
|
||||
If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature.
|
||||
|
||||
### Raw references ###
|
||||
Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references.
|
||||
|
||||
+1
-1
@@ -62,8 +62,8 @@
|
||||
feature(
|
||||
ptr_offset_from,
|
||||
const_fn,
|
||||
const_fn_transmute,
|
||||
const_ptr_offset_from,
|
||||
const_maybe_uninit_as_ptr,
|
||||
const_raw_ptr_deref,
|
||||
)
|
||||
)]
|
||||
|
||||
+1
-3
@@ -31,9 +31,7 @@ macro_rules! _memoffset__let_base_ptr {
|
||||
// `let_base_ptr` declares a variable (several, actually)
|
||||
// instead of returning one.
|
||||
let uninit = $crate::mem::MaybeUninit::<$type>::uninit();
|
||||
// Transmuting for const-compatibility.
|
||||
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
|
||||
let $name: *const $type = unsafe { $crate::mem::transmute(&uninit) };
|
||||
let $name: *const $type = uninit.as_ptr();
|
||||
};
|
||||
}
|
||||
#[cfg(not(maybe_uninit))]
|
||||
|
||||
Reference in New Issue
Block a user