Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use stablilized addr_of macro #50

Merged
merged 1 commit into from
Mar 8, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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_raw = []
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,3 @@ Your crate root: (`lib.rs`/`main.rs`)
```

If you intend to use `offset_of!` inside a `const fn`, also add the `const_fn` compiler feature.

### Raw references ###
Recent nightlies support [a way to create raw pointers](https://github.com/rust-lang/rust/issues/73394) that avoids creating intermediate safe references.
`memoffset` can make use of that feature to avoid what is technically Undefined Behavior.
Use the `unstable_raw` feature to enable this.
3 changes: 3 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ fn main() {
if ac.probe_rustc_version(1, 40) {
println!("cargo:rustc-cfg=doctests");
}
if ac.probe_rustc_version(1, 51) {
println!("cargo:rustc-cfg=raw_ref_macros");
}
}
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
const_raw_ptr_deref,
)
)]
#![cfg_attr(feature = "unstable_raw", feature(raw_ref_macros))]

#[macro_use]
#[cfg(doctests)]
Expand Down
18 changes: 9 additions & 9 deletions src/raw_field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

/// `raw_const!`, or just ref-then-cast when that is not available.
#[cfg(feature = "unstable_raw")]
/// `addr_of!`, or just ref-then-cast when that is not available.
#[cfg(raw_ref_macros)]
#[macro_export]
#[doc(hidden)]
macro_rules! _memoffset__raw_const {
macro_rules! _memoffset__addr_of {
($path:expr) => {{
$crate::ptr::raw_const!($path)
$crate::ptr::addr_of!($path)
}};
}
#[cfg(not(feature = "unstable_raw"))]
#[cfg(not(raw_ref_macros))]
#[macro_export]
#[doc(hidden)]
macro_rules! _memoffset__raw_const {
macro_rules! _memoffset__addr_of {
($path:expr) => {{
// This is UB because we create an intermediate reference to uninitialized memory.
// Nothing we can do about that without `raw_const!` though.
// Nothing we can do about that without `addr_of!` though.
&$path as *const _
}};
}
Expand Down Expand Up @@ -88,7 +88,7 @@ macro_rules! raw_field {
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
_memoffset__raw_const!((*($base as *const $parent)).$field)
_memoffset__addr_of!((*($base as *const $parent)).$field)
}
}};
}
Expand All @@ -109,7 +109,7 @@ macro_rules! raw_field_tuple {
// of the field check we did above.
#[allow(unused_unsafe)] // for when the macro is used in an unsafe block
unsafe {
_memoffset__raw_const!((*($base as *const $parent)).$field)
_memoffset__addr_of!((*($base as *const $parent)).$field)
}
}};
}