Skip to content

Commit

Permalink
[macro_util] Rename from derive_util, include more
Browse files Browse the repository at this point in the history
Include macro utilities - namely, re-exports of core symbols used by
macro-generated code.

Release 0.7.7 to work around a cargo-semver-checks false positive; see
#455 (comment).
  • Loading branch information
joshlf committed Oct 4, 2023
1 parent 78062fc commit 8233d75
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 28 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
[package]
edition = "2021"
name = "zerocopy"
version = "0.7.6"
version = "0.7.7"
authors = ["Joshua Liebow-Feeser <joshlf@google.com>"]
description = "Utilities for zero-copy parsing and serialization"
license = "BSD-2-Clause"
Expand Down Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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" }
24 changes: 10 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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) }
}
}}
}
Expand Down
20 changes: 13 additions & 7 deletions src/derive_util.rs → src/macro_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]

Expand Down Expand Up @@ -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::*;
Expand Down
2 changes: 1 addition & 1 deletion zerocopy-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[package]
edition = "2021"
name = "zerocopy-derive"
version = "0.7.6"
version = "0.7.7"
authors = ["Joshua Liebow-Feeser <joshlf@google.com>"]
description = "Custom derive for traits from the zerocopy crate"
license = "BSD-2-Clause"
Expand Down
4 changes: 2 additions & 2 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,8 +530,8 @@ fn impl_block<D: DataExt>(
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<false>
zerocopy::macro_util::HasPadding<#type_ident, {zerocopy::#validator_macro!(#type_ident, #(#fields),*)}>:
zerocopy::macro_util::ShouldBe<false>
)
});

Expand Down

0 comments on commit 8233d75

Please sign in to comment.