Skip to content

Commit 9520967

Browse files
authored
Remove const_transmute feature dependency (#45)
Remove `const_transmute` feature flag `const_transmute` has been stabilized on current nightly, see rust-lang/rust#72920. Document `const_fn_transmute` requirement when using `offset_of!` inside a `const fn`. Add test for `offset_of!` inside a `const fn`.
1 parent 8ec2862 commit 9520967

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,12 @@ features = ["unstable_const"]
6868

6969
Your crate root: (`lib.rs`/`main.rs`)
7070
```rust,ignore
71-
#![feature(ptr_offset_from, const_ptr_offset_from, const_transmute, const_raw_ptr_deref)]
71+
#![feature(ptr_offset_from, const_ptr_offset_from, const_raw_ptr_deref)]
72+
```
73+
74+
Or, if you intend to use `offset_of!` inside a `const fn`:
75+
```rust,ignore
76+
#![feature(ptr_offset_from, const_fn, const_fn_transmute, const_ptr_offset_from, const_raw_ptr_deref)]
7277
```
7378

7479
and then:

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@
6161
feature = "unstable_const",
6262
feature(
6363
ptr_offset_from,
64+
const_fn,
65+
const_fn_transmute,
6466
const_ptr_offset_from,
65-
const_transmute,
6667
const_raw_ptr_deref,
6768
)
6869
)]

src/offset_of.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,21 @@ mod tests {
194194

195195
assert_eq!([0; offset_of!(Foo, b)].len(), 4);
196196
}
197+
198+
#[cfg(feature = "unstable_const")]
199+
#[test]
200+
fn const_fn_offset() {
201+
const fn test_fn() -> usize {
202+
#[repr(C)]
203+
struct Foo {
204+
a: u32,
205+
b: [u8; 2],
206+
c: i64,
207+
}
208+
209+
offset_of!(Foo, b)
210+
}
211+
212+
assert_eq!([0; test_fn()].len(), 4);
213+
}
197214
}

0 commit comments

Comments
 (0)