From e1eedcbe31247d1e23779b06d90b2317618989f0 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 17:44:09 +0200 Subject: [PATCH 1/6] Remove `const_transmute` feature flag `const_transmute` has been stabilized on current nightly, see https://github.com/rust-lang/rust/pull/72920 --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 86d7051..85f6630 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -62,7 +62,6 @@ feature( ptr_offset_from, const_ptr_offset_from, - const_transmute, const_raw_ptr_deref, ) )] From 7805359db7640cb08975fb4df02d8adea2c8ea99 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 17:47:19 +0200 Subject: [PATCH 2/6] Update readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e5a5fdb..3bb483e 100644 --- a/README.md +++ b/README.md @@ -68,7 +68,7 @@ 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)] ``` and then: From c3e910692019add68d3939a93e045a7920049cd2 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 19:04:42 +0200 Subject: [PATCH 3/6] Document `const_fn_transmute` requirement when using `offset_of!` inside a `const fn` --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index 3bb483e..7bc8a36 100644 --- a/README.md +++ b/README.md @@ -71,6 +71,11 @@ Your crate root: (`lib.rs`/`main.rs`) #![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_transmute, const_ptr_offset_from, const_raw_ptr_deref)] +``` + and then: ```rust,ignore From 4b6e893867e251a8de765005e965a2f2bc3f1f24 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 19:12:41 +0200 Subject: [PATCH 4/6] Add test for `offset_of!` inside a `const fn`. --- src/lib.rs | 2 ++ src/offset_of.rs | 17 +++++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 85f6630..bf2e507 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -61,6 +61,8 @@ feature = "unstable_const", feature( ptr_offset_from, + const_fn, + const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref, ) diff --git a/src/offset_of.rs b/src/offset_of.rs index d0a59ad..f1e0500 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); + } } From cc7a48c9205e1c999b059c4882548dc6d35a3db8 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 19:13:22 +0200 Subject: [PATCH 5/6] Update readme with `const fn` feature flag requirement when using `offset_of!` inside a `const fn` --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7bc8a36..70bbead 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ Your crate root: (`lib.rs`/`main.rs`) Or, if you intend to use `offset_of!` inside a `const fn`: ```rust,ignore -#![feature(ptr_offset_from, const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref)] +#![feature(ptr_offset_from, const_fn, const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref)] ``` and then: From f1c378097da2d5bbb807dddf48943e1c4c2c5bc6 Mon Sep 17 00:00:00 2001 From: RSSchermer Date: Thu, 30 Jul 2020 20:04:45 +0200 Subject: [PATCH 6/6] Cargo fmt --- src/offset_of.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/offset_of.rs b/src/offset_of.rs index f1e0500..4c46ed8 100644 --- a/src/offset_of.rs +++ b/src/offset_of.rs @@ -206,9 +206,9 @@ mod tests { c: i64, } - offset_of!(Foo, b) + offset_of!(Foo, b) } - assert_eq!([0;test_fn()].len(), 4); + assert_eq!([0; test_fn()].len(), 4); } }