Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
)]
Expand Down
17 changes: 17 additions & 0 deletions src/offset_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}