Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use MaybeUninit::as_ptr instead of transmute #46

Merged
merged 1 commit into from
Aug 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)]
Expand Down
4 changes: 1 addition & 3 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))]
Expand Down