diff --git a/src/offset_of.rs b/src/offset_of.rs index 1498cdf..d0a59ad 100644 --- a/src/offset_of.rs +++ b/src/offset_of.rs @@ -56,7 +56,7 @@ macro_rules! _memoffset_offset_from { // Compute offset, with unstable `offset_from` for const-compatibility. // (Requires the pointers to not dangle, but we already need that for `raw_field!` anyway.) unsafe { ($field as *const u8).offset_from($base as *const u8) as usize } - } + }; } #[cfg(not(feature = "unstable_const"))] #[macro_export] @@ -65,7 +65,7 @@ macro_rules! _memoffset_offset_from { ($field:expr, $base:expr) => { // Compute offset. ($field as usize) - ($base as usize) - } + }; } /// Calculates the offset of the specified field from the start of the struct. diff --git a/src/raw_field.rs b/src/raw_field.rs index d4c523a..51a4c41 100644 --- a/src/raw_field.rs +++ b/src/raw_field.rs @@ -38,6 +38,31 @@ macro_rules! _memoffset__raw_const { }}; } +/// Deref-coercion protection macro. +#[cfg(allow_clippy)] +#[macro_export] +#[doc(hidden)] +macro_rules! _memoffset__field_check { + ($type:path, $field:tt) => { + // Make sure the field actually exists. This line ensures that a + // compile-time error is generated if $field is accessed through a + // Deref impl. + #[allow(clippy::unneeded_field_pattern)] + let $type { $field: _, .. }; + }; +} +#[cfg(not(allow_clippy))] +#[macro_export] +#[doc(hidden)] +macro_rules! _memoffset__field_check { + ($type:path, $field:tt) => { + // Make sure the field actually exists. This line ensures that a + // compile-time error is generated if $field is accessed through a + // Deref impl. + let $type { $field: _, .. }; + }; +} + /// Computes a const raw pointer to the given field of the given base pointer /// to the given parent type. /// @@ -46,11 +71,7 @@ macro_rules! _memoffset__raw_const { #[macro_export(local_inner_macros)] macro_rules! raw_field { ($base:expr, $parent:path, $field:tt) => {{ - // Make sure the field actually exists. This line ensures that a - // compile-time error is generated if $field is accessed through a - // Deref impl. - #[cfg_attr(allow_clippy, allow(clippy::unneeded_field_pattern))] - let $parent { $field: _, .. }; + _memoffset__field_check!($parent, $field); // Get the field address. // Crucially, we know that this will not trigger a deref coercion because