diff --git a/README.md b/README.md index e5a5fdb..70bbead 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,12 @@ features = ["unstable_const"] Your crate root: (`lib.rs`/`main.rs`) ```rust,ignore -#![feature(ptr_offset_from, const_ptr_offset_from, const_transmute, const_raw_ptr_deref)] +#![feature(ptr_offset_from, const_ptr_offset_from, 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: diff --git a/src/lib.rs b/src/lib.rs index 86d7051..bf2e507 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,8 +61,9 @@ feature = "unstable_const", feature( ptr_offset_from, + const_fn, + const_fn_transmute, const_ptr_offset_from, - const_transmute, const_raw_ptr_deref, ) )] diff --git a/src/offset_of.rs b/src/offset_of.rs index d0a59ad..4c46ed8 100644 --- a/src/offset_of.rs +++ b/src/offset_of.rs @@ -194,4 +194,21 @@ mod tests { assert_eq!([0; offset_of!(Foo, b)].len(), 4); } + + #[cfg(feature = "unstable_const")] + #[test] + fn const_fn_offset() { + const fn test_fn() -> usize { + #[repr(C)] + struct Foo { + a: u32, + b: [u8; 2], + c: i64, + } + + offset_of!(Foo, b) + } + + assert_eq!([0; test_fn()].len(), 4); + } }