Skip to content

Commit 47bc243

Browse files
authored
[derive] Support deriving deprecated AsBytes (#938)
This makes the transition from `AsBytes` to `IntoBytes` (its new name) easier. Makes progress on #695
1 parent 461bc15 commit 47bc243

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

zerocopy-derive/src/lib.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ derive!(NoCell => derive_no_cell => derive_no_cell_inner);
9393
derive!(TryFromBytes => derive_try_from_bytes => derive_try_from_bytes_inner);
9494
derive!(FromZeros => derive_from_zeros => derive_from_zeros_inner);
9595
derive!(FromBytes => derive_from_bytes => derive_from_bytes_inner);
96-
derive!(IntoBytes => derive_as_bytes => derive_as_bytes_inner);
96+
derive!(IntoBytes => derive_into_bytes => derive_into_bytes_inner);
9797
derive!(Unaligned => derive_unaligned => derive_unaligned_inner);
9898

9999
/// Deprecated: prefer [`FromZeros`] instead.
@@ -104,6 +104,14 @@ pub fn derive_from_zeroes(ts: proc_macro::TokenStream) -> proc_macro::TokenStrea
104104
derive_from_zeros(ts)
105105
}
106106

107+
/// Deprecated: prefer [`IntoBytes`] instead.
108+
#[deprecated(since = "0.8.0", note = "`AsBytes` was renamed to `IntoBytes`")]
109+
#[doc(hidden)]
110+
#[proc_macro_derive(AsBytes)]
111+
pub fn derive_as_bytes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
112+
derive_into_bytes(ts)
113+
}
114+
107115
fn derive_known_layout_inner(ast: &DeriveInput) -> proc_macro2::TokenStream {
108116
let is_repr_c_struct = match &ast.data {
109117
Data::Struct(..) => {
@@ -324,11 +332,11 @@ fn derive_from_bytes_inner(ast: &DeriveInput) -> proc_macro2::TokenStream {
324332
IntoIterator::into_iter([from_zeros, from_bytes]).collect()
325333
}
326334

327-
fn derive_as_bytes_inner(ast: &DeriveInput) -> proc_macro2::TokenStream {
335+
fn derive_into_bytes_inner(ast: &DeriveInput) -> proc_macro2::TokenStream {
328336
match &ast.data {
329-
Data::Struct(strct) => derive_as_bytes_struct(ast, strct),
330-
Data::Enum(enm) => derive_as_bytes_enum(ast, enm),
331-
Data::Union(unn) => derive_as_bytes_union(ast, unn),
337+
Data::Struct(strct) => derive_into_bytes_struct(ast, strct),
338+
Data::Enum(enm) => derive_into_bytes_enum(ast, enm),
339+
Data::Union(unn) => derive_into_bytes_union(ast, unn),
332340
}
333341
}
334342

@@ -568,7 +576,7 @@ fn derive_from_zeros_enum(ast: &DeriveInput, enm: &DataEnum) -> proc_macro2::Tok
568576

569577
// We don't actually care what the repr is; we just care that it's one of
570578
// the allowed ones.
571-
try_or_print!(ENUM_FROM_ZEROS_AS_BYTES_CFG.validate_reprs(ast));
579+
try_or_print!(ENUM_FROM_ZEROS_INTO_BYTES_CFG.validate_reprs(ast));
572580

573581
let has_explicit_zero_discriminant =
574582
enm.variants.iter().filter_map(|v| v.discriminant.as_ref()).any(|(_, e)| {
@@ -687,8 +695,8 @@ fn derive_from_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro2::T
687695
impl_block(ast, unn, Trait::FromBytes, FieldBounds::ALL_SELF, SelfBounds::None, None, None)
688696
}
689697

690-
fn derive_as_bytes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_macro2::TokenStream {
691-
let reprs = try_or_print!(STRUCT_UNION_AS_BYTES_CFG.validate_reprs(ast));
698+
fn derive_into_bytes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_macro2::TokenStream {
699+
let reprs = try_or_print!(STRUCT_UNION_INTO_BYTES_CFG.validate_reprs(ast));
692700
let is_transparent = reprs.contains(&StructRepr::Transparent);
693701
let is_packed = reprs.contains(&StructRepr::Packed);
694702
let num_fields = strct.fields().len();
@@ -737,7 +745,7 @@ fn derive_as_bytes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_macro2:
737745
impl_block(ast, strct, Trait::IntoBytes, field_bounds, SelfBounds::None, padding_check, None)
738746
}
739747

740-
const STRUCT_UNION_AS_BYTES_CFG: Config<StructRepr> = Config {
748+
const STRUCT_UNION_INTO_BYTES_CFG: Config<StructRepr> = Config {
741749
// Since `disallowed_but_legal_combinations` is empty, this message will
742750
// never actually be emitted.
743751
allowed_combinations_message: r#"IntoBytes requires either a) repr "C" or "transparent" with all fields implementing IntoBytes or, b) repr "packed""#,
@@ -748,20 +756,20 @@ const STRUCT_UNION_AS_BYTES_CFG: Config<StructRepr> = Config {
748756

749757
// An enum is `IntoBytes` if it is field-less and has a defined repr.
750758

751-
fn derive_as_bytes_enum(ast: &DeriveInput, enm: &DataEnum) -> proc_macro2::TokenStream {
759+
fn derive_into_bytes_enum(ast: &DeriveInput, enm: &DataEnum) -> proc_macro2::TokenStream {
752760
if !enm.is_fieldless() {
753761
return Error::new_spanned(ast, "only field-less enums can implement IntoBytes")
754762
.to_compile_error();
755763
}
756764

757765
// We don't care what the repr is; we only care that it is one of the
758766
// allowed ones.
759-
try_or_print!(ENUM_FROM_ZEROS_AS_BYTES_CFG.validate_reprs(ast));
767+
try_or_print!(ENUM_FROM_ZEROS_INTO_BYTES_CFG.validate_reprs(ast));
760768
impl_block(ast, enm, Trait::IntoBytes, FieldBounds::None, SelfBounds::None, None, None)
761769
}
762770

763771
#[rustfmt::skip]
764-
const ENUM_FROM_ZEROS_AS_BYTES_CFG: Config<EnumRepr> = {
772+
const ENUM_FROM_ZEROS_INTO_BYTES_CFG: Config<EnumRepr> = {
765773
use EnumRepr::*;
766774
Config {
767775
// Since `disallowed_but_legal_combinations` is empty, this message will
@@ -790,14 +798,14 @@ const ENUM_FROM_ZEROS_AS_BYTES_CFG: Config<EnumRepr> = {
790798
// - `repr(C)`, `repr(transparent)`, or `repr(packed)`
791799
// - no padding (size of union equals size of each field type)
792800

793-
fn derive_as_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro2::TokenStream {
801+
fn derive_into_bytes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro2::TokenStream {
794802
// TODO(#10): Support type parameters.
795803
if !ast.generics.params.is_empty() {
796804
return Error::new(Span::call_site(), "unsupported on types with type parameters")
797805
.to_compile_error();
798806
}
799807

800-
try_or_print!(STRUCT_UNION_AS_BYTES_CFG.validate_reprs(ast));
808+
try_or_print!(STRUCT_UNION_INTO_BYTES_CFG.validate_reprs(ast));
801809

802810
impl_block(
803811
ast,

0 commit comments

Comments
 (0)