Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
[package]
edition = "2021"
name = "zerocopy"
version = "0.8.32"
version = "0.8.33"
authors = [
"Joshua Liebow-Feeser <[email protected]>",
"Jack Wrenn <[email protected]>",
Expand Down Expand Up @@ -112,13 +112,13 @@ __internal_use_only_features_that_work_on_stable = [
]

[dependencies]
zerocopy-derive = { version = "=0.8.32", path = "zerocopy-derive", optional = true }
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive", optional = true }

# The "associated proc macro pattern" ensures that the versions of zerocopy and
# 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.8.32", path = "zerocopy-derive" }
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive" }

[dev-dependencies]
# More recent versions of `either` have an MSRV higher than ours.
Expand All @@ -142,4 +142,4 @@ testutil = { path = "testutil" }
# CI test failures.
trybuild = { version = "=1.0.89", features = ["diff"] }
# In tests, unlike in production, zerocopy-derive is not optional
zerocopy-derive = { version = "=0.8.32", path = "zerocopy-derive" }
zerocopy-derive = { version = "=0.8.33", path = "zerocopy-derive" }
2 changes: 1 addition & 1 deletion zerocopy-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[package]
edition = "2021"
name = "zerocopy-derive"
version = "0.8.32"
version = "0.8.33"
authors = ["Joshua Liebow-Feeser <[email protected]>", "Jack Wrenn <[email protected]>"]
description = "Custom derive for traits from the zerocopy crate"
license = "BSD-2-Clause OR Apache-2.0 OR MIT"
Expand Down
9 changes: 3 additions & 6 deletions zerocopy-derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ pub(crate) fn generate_tag_enum(repr: &EnumRepr, data: &DataEnum) -> TokenStream
}

fn tag_ident(variant_ident: &Ident) -> Ident {
let variant_ident_str = crate::ext::to_ident_str(variant_ident);
Ident::new(&format!("___ZEROCOPY_TAG_{}", variant_ident_str), variant_ident.span())
ident!(("___ZEROCOPY_TAG_{}", variant_ident), variant_ident.span())
}

/// Generates a constant for the tag associated with each variant of the enum.
Expand Down Expand Up @@ -103,8 +102,7 @@ fn generate_tag_consts(data: &DataEnum) -> TokenStream {
}

fn variant_struct_ident(variant_ident: &Ident) -> Ident {
let variant_ident_str = crate::ext::to_ident_str(variant_ident);
Ident::new(&format!("___ZerocopyVariantStruct_{}", variant_ident_str), variant_ident.span())
ident!(("___ZerocopyVariantStruct_{}", variant_ident), variant_ident.span())
}

/// Generates variant structs for the given enum variant.
Expand Down Expand Up @@ -171,10 +169,9 @@ fn generate_variant_structs(
}

fn variants_union_field_ident(ident: &Ident) -> Ident {
let variant_ident_str = crate::ext::to_ident_str(ident);
// Field names are prefixed with `__field_` to prevent name collision
// with the `__nonempty` field.
Ident::new(&format!("__field_{}", variant_ident_str), ident.span())
ident!(("__field_{}", ident), ident.span())
}

fn generate_variants_union(
Expand Down
16 changes: 10 additions & 6 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@
)]
#![recursion_limit = "128"]

macro_rules! ident {
(($fmt:literal $(, $arg:expr)*), $span:expr) => {
syn::Ident::new(&format!($fmt $(, crate::ext::to_ident_str($arg))*), $span)
};
}

mod r#enum;
mod ext;
#[cfg(test)]
Expand Down Expand Up @@ -322,10 +328,8 @@ fn derive_known_layout_inner(

// Generate a valid ident for a type-level handle to a field of a
// given `name`.
let field_index = |name: &TokenStream| {
let name = to_ident_str(name);
Ident::new(&format!("__Zerocopy_Field_{}", name), ident.span())
};
let field_index =
|name: &TokenStream| ident!(("__Zerocopy_Field_{}", name), ident.span());

let field_indices: Vec<_> =
fields.iter().map(|(_vis, name, _ty)| field_index(name)).collect();
Expand Down Expand Up @@ -764,7 +768,7 @@ fn derive_has_field_struct_union(
}

let field_tokens = fields.iter().map(|(vis, ident, _)| {
let ident = Ident::new(&format!("ẕ{}", ident), ident.span());
let ident = ident!(("ẕ{}", ident), ident.span());
quote!(
#vis enum #ident {}
)
Expand All @@ -783,7 +787,7 @@ fn derive_has_field_struct_union(
Data::Enum(..) | Data::Struct(..) => false,
};
let has_fields = fields.iter().map(move |(_, ident, ty)| {
let field_token = Ident::new(&format!("ẕ{}", ident), ident.span());
let field_token = ident!(("ẕ{}", ident), ident.span());
let field: Box<Type> = parse_quote!(#field_token);
let field_id: Box<Expr> = parse_quote!({ #zerocopy_crate::ident_id!(#ident) });
ImplBlockBuilder::new(
Expand Down
8 changes: 8 additions & 0 deletions zerocopy-derive/tests/struct_try_from_bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,11 @@ struct A;
struct B {
a: A,
}

#[derive(imp::TryFromBytes)]
#[repr(C)]
struct RawIdent {
r#type: u8,
}

util_assert_impl_all!(RawIdent: imp::TryFromBytes);
Loading