diff --git a/src/lib.rs b/src/lib.rs index 7a02c3a523..ac8292ebb1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,7 +172,7 @@ mod macros; pub mod byteorder; #[cfg(any(feature = "derive", test))] #[doc(hidden)] -pub mod derive_util; +pub mod macro_util; mod util; // TODO(#252): If we make this pub, come up with a better name. mod wrappers; @@ -1628,10 +1628,6 @@ mod simd { simd_arch_mod!(arm, int8x4_t, uint8x4_t); } -// Used in `transmute!` below. -#[doc(hidden)] -pub use core::mem::transmute as __real_transmute; - /// Safely transmutes a value of one type to a value of another type of the same /// size. /// @@ -1666,14 +1662,15 @@ macro_rules! transmute { // We know this transmute is safe thanks to the `AsBytes` and // `FromBytes` bounds enforced by the `false` branch. // - // We use `$crate::__real_transmute` because we know it will always - // be available for crates which are using the 2015 edition of Rust. - // By contrast, if we were to use `std::mem::transmute`, this macro - // would not work for such crates in `no_std` contexts, and if we - // were to use `core::mem::transmute`, this macro would not work in - // `std` contexts in which `core` was not manually imported. This is - // not a problem for 2018 edition crates. - unsafe { $crate::__real_transmute(e) } + // We use this reexport of `core::mem::transmute` because we know it + // will always be available for crates which are using the 2015 + // edition of Rust. By contrast, if we were to use + // `std::mem::transmute`, this macro would not work for such crates + // in `no_std` contexts, and if we were to use + // `core::mem::transmute`, this macro would not work in `std` + // contexts in which `core` was not manually imported. This is not a + // problem for 2018 edition crates. + unsafe { $crate::macro_util::core_reexport::mem::transmute(e) } } }} } diff --git a/src/derive_util.rs b/src/macro_util.rs similarity index 90% rename from src/derive_util.rs rename to src/macro_util.rs index edf88e3bd7..9e5c39a650 100644 --- a/src/derive_util.rs +++ b/src/macro_util.rs @@ -2,14 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -//! Utilities used by `zerocopy-derive`. +//! Utilities used by macros and by `zerocopy-derive`. //! -//! These are defined in `zerocopy` rather than in code generated by -//! `zerocopy-derive` so that they can be compiled once rather than recompiled -//! for every pair of type and trait (in other words, if they were defined in -//! generated code, then deriving `AsBytes` and `FromBytes` on three different -//! types would result in the code in question being emitted and compiled six -//! different times). +//! These are defined here `zerocopy` rather than in code generated by macros or +//! by `zerocopy-derive` so that they can be compiled once rather than +//! recompiled for every invocation (e.g., if they were defined in generated +//! code, then deriving `AsBytes` and `FromBytes` on three different types would +//! result in the code in question being emitted and compiled six different +//! times). #![allow(missing_debug_implementations)] @@ -63,6 +63,12 @@ macro_rules! union_has_padding { }; } +pub mod core_reexport { + pub mod mem { + pub use core::mem::transmute; + } +} + #[cfg(test)] mod tests { use crate::util::testutil::*; diff --git a/zerocopy-derive/src/lib.rs b/zerocopy-derive/src/lib.rs index 6b9e3a40f2..728e66c7fb 100644 --- a/zerocopy-derive/src/lib.rs +++ b/zerocopy-derive/src/lib.rs @@ -530,8 +530,8 @@ fn impl_block( let fields = field_types.iter(); let validator_macro = check.validator_macro_ident(); parse_quote!( - zerocopy::derive_util::HasPadding<#type_ident, {zerocopy::#validator_macro!(#type_ident, #(#fields),*)}>: - zerocopy::derive_util::ShouldBe + zerocopy::macro_util::HasPadding<#type_ident, {zerocopy::#validator_macro!(#type_ident, #(#fields),*)}>: + zerocopy::macro_util::ShouldBe ) });