diff --git a/Cargo.toml b/Cargo.toml index 842b754709..9ca19a10cc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ [package] edition = "2021" name = "zerocopy" -version = "0.7.6" +version = "0.7.7" authors = ["Joshua Liebow-Feeser "] description = "Utilities for zero-copy parsing and serialization" license = "BSD-2-Clause" @@ -41,7 +41,7 @@ simd-nightly = ["simd"] __internal_use_only_features_that_work_on_stable = ["alloc", "derive", "simd"] [dependencies] -zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive", optional = true } +zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive", optional = true } [dependencies.byteorder] version = "1.3" @@ -52,7 +52,7 @@ optional = true # zerocopy-derive remain equal, even if the 'derive' feature isn't used. # See: https://github.com/matklad/macro-dep-test [target.'cfg(any())'.dependencies] -zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive" } +zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive" } [dev-dependencies] assert_matches = "1.5" @@ -67,4 +67,4 @@ testutil = { path = "testutil" } # CI test failures. trybuild = "=1.0.80" # In tests, unlike in production, zerocopy-derive is not optional -zerocopy-derive = { version = "=0.7.6", path = "zerocopy-derive" } +zerocopy-derive = { version = "=0.7.7", path = "zerocopy-derive" } diff --git a/src/lib.rs b/src/lib.rs index 7a02c3a523..b76f10e1e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -170,9 +170,8 @@ mod macros; #[cfg(feature = "byteorder")] 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 +1627,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 +1661,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/Cargo.toml b/zerocopy-derive/Cargo.toml index 19185dae13..e9b06cee99 100644 --- a/zerocopy-derive/Cargo.toml +++ b/zerocopy-derive/Cargo.toml @@ -5,7 +5,7 @@ [package] edition = "2021" name = "zerocopy-derive" -version = "0.7.6" +version = "0.7.7" authors = ["Joshua Liebow-Feeser "] description = "Custom derive for traits from the zerocopy crate" license = "BSD-2-Clause" 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 ) });