Skip to content

Commit

Permalink
Merge pull request #83 from coolreader18/stable_offset_of
Browse files Browse the repository at this point in the history
Just use core::mem::offset_of!() on rustc >= 1.77
  • Loading branch information
RalfJung authored Mar 26, 2024
2 parents 804aaab + 638ee9c commit d553d8c
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 29 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## Unreleased
- Clarify documentation about macro indirection
- Added changelog
- Turn the crate into a thin stdlib wrapper on rustc>=1.77
- Remove `unstable_offset_of`

## v0.9.0 (18/05/2023)
### Added
Expand Down
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,3 @@ doc-comment = "0.3"
[features]
default = []
unstable_const = []
unstable_offset_of = []
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ Introduces the following macros:

`memoffset` works under `no_std` environments.

If you're using a rustc version greater or equal to 1.77, this crate's `offset_of!()` macro simply forwards to `core::mem::offset_of!()`.

## Usage ##
Add the following dependency to your `Cargo.toml`:

Expand Down Expand Up @@ -53,13 +55,11 @@ fn main() {
Constant evaluation is automatically enabled and available on stable compilers starting with rustc 1.65.

This is an incomplete implementation with one caveat:
Due to dependence on [`#![feature(const_refs_to_cell)]`](https://github.com/rust-lang/rust/issues/80384), you cannot get the offset of a `Cell` field in a const-context.

This means that if need to get the offset of a cell, you'll have to remain on nightly for now.
Due to dependence on [`#![feature(const_refs_to_cell)]`](https://github.com/rust-lang/rust/issues/80384), you cannot get the offset of a `Cell` field in a const-context on a rustc version less than 1.77.

### Usage on recent nightlies ###
### Usage on somewhat recent nightlies ###

If you're using a new-enough nightly and you require the ability to get the offset of a `Cell`,
If you're using a nightly that does not yet have `core::mem::offset_of!()` and you require the ability to get the offset of a `Cell`,
you'll have to enable the `unstable_const` cargo feature, as well as enabling `const_refs_to_cell` in your crate root.

Do note that `unstable_const` is an unstable feature that is set to be removed in a future version of `memoffset`.
Expand Down
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ fn main() {
if ac.probe_rustc_version(1, 65) {
println!("cargo:rustc-cfg=stable_const");
}
if ac.probe_rustc_version(1, 77) {
println!("cargo:rustc-cfg=stable_offset_of");
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
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
56 changes: 34 additions & 22 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ macro_rules! _memoffset_offset_from_unsafe {
($field as usize) - ($base as usize)
};
}
#[cfg(not(feature = "unstable_offset_of"))]
#[cfg(not(stable_offset_of))]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_impl {
Expand All @@ -80,10 +80,9 @@ macro_rules! _memoffset__offset_of_impl {
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}
#[cfg(feature = "unstable_offset_of")]
#[cfg(stable_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)
Expand Down Expand Up @@ -125,6 +124,31 @@ macro_rules! offset_of {
};
}

#[cfg(tuple_ty)]
#[cfg(not(stable_offset_of))]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_tuple_impl {
($parent:ty, $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_tuple!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
}};
}

#[cfg(tuple_ty)]
#[cfg(stable_offset_of)]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_tuple_impl {
($parent:ty, $field:tt) => {{
$crate::__priv::mem::offset_of!($parent, $field)
}};
}

/// Calculates the offset of the specified field from the start of the tuple.
///
/// ## Examples
Expand All @@ -137,16 +161,13 @@ macro_rules! offset_of {
#[macro_export(local_inner_macros)]
macro_rules! offset_of_tuple {
($parent:ty, $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_tuple!(base_ptr, $parent, $field);
// Compute offset.
_memoffset_offset_from_unsafe!(field_ptr, base_ptr)
// Macro implementation is delegated to another macro to have a
// single top-level macro to attach documentation to.
_memoffset__offset_of_tuple_impl!($parent, $field)
}};
}

#[cfg(not(feature = "unstable_offset_of"))]
#[cfg(not(stable_offset_of))]
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! _memoffset__offset_of_union_impl {
Expand All @@ -160,10 +181,9 @@ macro_rules! _memoffset__offset_of_union_impl {
}};
}

#[cfg(feature = "unstable_offset_of")]
#[cfg(stable_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)
Expand Down Expand Up @@ -348,11 +368,7 @@ mod tests {
assert_eq!(f_ptr as usize + 0, raw_field_union!(f_ptr, Foo, c) as usize);
}

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

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

0 comments on commit d553d8c

Please sign in to comment.