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

add raw_field macro to compute raw ptr to field #40

Merged
merged 3 commits into from
Mar 16, 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
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ features = ["unstable_const"]

Your crate root: (`lib.rs`/`main.rs`)
```rust,ignore
#![feature(const_transmute)]
#![feature(const_ptr_offset_from)]
#![feature(ptr_offset_from)]
#![feature(ptr_offset_from, const_ptr_offset_from, const_transmute, const_raw_ptr_deref)]
```

and then:
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,15 @@
//! ```

#![no_std]
#![cfg_attr(feature = "unstable_const", feature(const_ptr_offset_from))]
#![cfg_attr(feature = "unstable_const", feature(const_transmute))]
#![cfg_attr(feature = "unstable_const", feature(ptr_offset_from))]
#![cfg_attr(
feature = "unstable_const",
feature(
ptr_offset_from,
const_ptr_offset_from,
const_transmute,
const_raw_ptr_deref
)
)]

#[macro_use]
#[cfg(doctests)]
Expand Down
74 changes: 53 additions & 21 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,29 @@ macro_rules! _memoffset__field_check {
};
}

/// Computes a const raw pointer to the given field of the given base pointer
/// to the given parent type.
///
/// The `base` pointer *must not* be dangling, but it *may* point to
/// uninitialized memory.
#[macro_export(local_inner_macros)]
macro_rules! raw_field {
($base:expr, $parent:path, $field:tt) => {{
_memoffset__field_check!($parent, $field);
let base_ptr: *const $parent = $base;

// Get the field address. This is UB because we are creating a reference to
// the uninitialized field. Will be updated to use `&raw` before rustc
// starts exploiting such UB.
// Crucially, we know that this will not trigger a deref coercion because
// of the `field_check!` we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
&(*base_ptr).$field as *const _
}
}};
}

/// Calculates the offset of the specified field from the start of the struct.
///
/// ## Examples
Expand All @@ -81,43 +104,32 @@ macro_rules! _memoffset__field_check {
#[macro_export(local_inner_macros)]
macro_rules! offset_of {
($parent:path, $field:tt) => {{
_memoffset__field_check!($parent, $field);

// Get a base pointer.
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get the field address. This is UB because we are creating a reference to
// the uninitialized field.
// Crucially, we know that this will not trigger a deref coercion because
// of the `field_check!` we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
let field_ptr = unsafe { &(*base_ptr).$field as *const _ };
let offset = (field_ptr as usize) - (base_ptr as usize);
offset
// Get field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
(field_ptr as usize) - (base_ptr as usize)
}};
}

#[cfg(feature = "unstable_const")]
#[macro_export(local_inner_macros)]
macro_rules! offset_of {
($parent:path, $field:tt) => {{
_memoffset__field_check!($parent, $field);

// Get a base pointer.
// No UB here, and the pointer does not dangle, either.
let uninit = $crate::mem::MaybeUninit::<$parent>::uninit();
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
// This, on the other hand, *is* UB, since we're creating a reference
// to uninitialized data.
// Unfortunately it's the best we can do at the moment.
let base_ref = $crate::mem::transmute::<_, &$parent>(&uninit);
let base_u8_ptr = base_ref as *const _ as *const u8;

// This is another reference to uninitialized data.
// Crucially, we know that this will not trigger a deref coercion because
// of the `field_check!` we did above.
let field_u8_ptr = &base_ref.$field as *const _ as *const u8;
let offset = field_u8_ptr.offset_from(base_u8_ptr) as usize;
offset
let base_ptr = $crate::mem::transmute::<_, &$parent>(&uninit) as *const $parent;
// Get a field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
(field_ptr as *const u8).offset_from(base_ptr as *const u8) as usize
}
}};
}
Expand Down Expand Up @@ -185,6 +197,26 @@ mod tests {
assert_eq!(foo(Pair(0, 0)), 4);
}

#[test]
fn test_raw_field() {
#[repr(C)]
struct Foo {
a: u32,
b: [u8; 2],
c: i64,
}

let f: Foo = Foo {
a: 0,
b: [0, 0],
c: 0,
};
let f_ptr = &f as *const _;
assert_eq!(f_ptr as usize + 0, raw_field!(f_ptr, Foo, a) as usize);
assert_eq!(f_ptr as usize + 4, raw_field!(f_ptr, Foo, b) as usize);
assert_eq!(f_ptr as usize + 8, raw_field!(f_ptr, Foo, c) as usize);
}

#[cfg(feature = "unstable_const")]
#[test]
fn const_offset() {
Expand Down