Skip to content

Commit

Permalink
Add unstable_offset_of feature to use the unstable offset_of macro
Browse files Browse the repository at this point in the history
  • Loading branch information
est31 committed May 17, 2023
1 parent 0fac3ac commit 274dacc
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 16 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ doc-comment = "0.3"
[features]
default = []
unstable_const = []
unstable_offset_of = []
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
feature(const_ptr_offset_from)
)]
#![cfg_attr(feature = "unstable_const", feature(const_refs_to_cell))]
#![cfg_attr(feature = "unstable_offset_of", feature(allow_internal_unstable))]

#[macro_use]
#[cfg(doctests)]
Expand Down
76 changes: 60 additions & 16 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,28 @@ macro_rules! _memoffset_offset_from_unsafe {
($field as usize) - ($base as usize)
};
}
#[cfg(not(feature = "unstable_offset_of"))]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_impl {
($parent:path, $field:tt) => {{
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}
#[cfg(feature = "unstable_offset_of")]
#[macro_export]
#[doc(hidden)]
#[allow_internal_unstable(offset_of)]
macro_rules! _memoffset__offset_of_impl {
($parent:path, $field:tt) => {{
$crate::__priv::mem::offset_of!($parent, $field)
}};
}

/// Calculates the offset of the specified field from the start of the named struct.
///
Expand Down Expand Up @@ -98,14 +120,9 @@ macro_rules! _memoffset_offset_from_unsafe {
/// As a result, the value should not be retained and used between different compilations.
#[macro_export(local_inner_macros)]
macro_rules! offset_of {
($parent:path, $field:tt) => {{
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get field pointer.
let field_ptr = raw_field!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
($parent:path, $field:tt) => {
_memoffset__offset_of_impl!($parent, $field)
};
}

/// Calculates the offset of the specified field from the start of the tuple.
Expand All @@ -131,6 +148,30 @@ macro_rules! offset_of_tuple {
}};
}

#[cfg(not(feature = "unstable_offset_of"))]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_union_impl {
($parent:path, $field:tt) => {{
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get field pointer.
let field_ptr = raw_field_union!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}

#[cfg(feature = "unstable_offset_of")]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
#[allow_internal_unstable(offset_of)]
macro_rules! _memoffset__offset_of_union_impl {
($parent:path, $field:tt) => {{
$crate::__priv::mem::offset_of!($parent, $field)
}};
}

/// Calculates the offset of the specified union member from the start of the union.
///
/// ## Examples
Expand All @@ -155,12 +196,7 @@ macro_rules! offset_of_tuple {
#[macro_export(local_inner_macros)]
macro_rules! offset_of_union {
($parent:path, $field:tt) => {{
// Get a base pointer (non-dangling if rustc supports `MaybeUninit`).
_memoffset__let_base_ptr!(base_ptr, $parent);
// Get field pointer.
let field_ptr = raw_field_union!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
_memoffset__offset_of_union_impl!($parent, $field)
}};
}

Expand Down Expand Up @@ -312,7 +348,11 @@ mod tests {
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize);
}

#[cfg(any(feature = "unstable_const", stable_const))]
#[cfg(any(
feature = "unstable_const",
feature = "unstable_offset_of",
stable_const
))]
#[test]
fn const_offset() {
#[repr(C)]
Expand All @@ -337,7 +377,11 @@ mod tests {
assert_eq!([0; offset_of!(Foo, b)].len(), 4);
}

#[cfg(any(feature = "unstable_const", stable_const))]
#[cfg(any(
feature = "unstable_const",
feature = "unstable_offset_of",
stable_const
))]
#[test]
fn const_fn_offset() {
const fn test_fn() -> usize {
Expand Down

0 comments on commit 274dacc

Please sign in to comment.