|
| 1 | +use proc_macro2::{Span, TokenStream}; |
| 2 | +use quote::quote; |
| 3 | +use syn::{Data, DataEnum, DataStruct, DataUnion, Error, Type}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + repr::{EnumRepr, StructUnionRepr}, |
| 7 | + util::{ |
| 8 | + generate_tag_enum, Ctx, DataExt, FieldBounds, ImplBlockBuilder, PaddingCheck, Trait, |
| 9 | + TraitBound, |
| 10 | + }, |
| 11 | + SelfBounds, |
| 12 | +}; |
| 13 | + |
| 14 | +pub(crate) fn derive_initialize_into_bytes( |
| 15 | + ctx: &Ctx, |
| 16 | + top_level: Trait, |
| 17 | +) -> Result<TokenStream, Error> { |
| 18 | + try_gen_trivial_initialize_into_bytes(ctx, top_level).map(Ok).unwrap_or_else(|| { |
| 19 | + match &ctx.ast.data { |
| 20 | + Data::Struct(strct) => derive_initialize_into_bytes_struct(ctx, strct), |
| 21 | + Data::Enum(enm) => derive_initialize_into_bytes_enum(ctx, enm), |
| 22 | + Data::Union(unn) => derive_initialize_into_bytes_union(ctx, unn), |
| 23 | + } |
| 24 | + }) |
| 25 | +} |
| 26 | + |
| 27 | +fn try_gen_trivial_initialize_into_bytes( |
| 28 | + ctx: &Ctx, |
| 29 | + top_level: Trait, |
| 30 | +) -> Option<proc_macro2::TokenStream> { |
| 31 | + // If the top-level trait is `IntoBytes`, `IntoBytes` derive will fail |
| 32 | + // compilation if `Self` is not actually soundly `IntoBytes`, and so we can |
| 33 | + // rely on that for our `zeroize` impl. It's plausible that we could |
| 34 | + // make changes - or Rust could make changes (such as the "trivial bounds" |
| 35 | + // language feature) - that make this no longer true. To hedge against |
| 36 | + // these, we include an explicit `Self: IntoBytes` check in the generated |
| 37 | + // `is_bit_valid`, which is bulletproof. |
| 38 | + // |
| 39 | + // If `ctx.skip_on_error` is true, we can't rely on the `IntoBytes` derive |
| 40 | + // to fail compilation if `Self` is not actually soundly `IntoBytes`. |
| 41 | + if matches!(top_level, Trait::IntoBytes) |
| 42 | + && ctx.ast.generics.params.is_empty() |
| 43 | + && !ctx.skip_on_error |
| 44 | + { |
| 45 | + let zerocopy_crate = &ctx.zerocopy_crate; |
| 46 | + let core = ctx.core_path(); |
| 47 | + |
| 48 | + Some( |
| 49 | + ImplBlockBuilder::new( |
| 50 | + ctx, |
| 51 | + &ctx.ast.data, |
| 52 | + Trait::InitializeIntoBytes, |
| 53 | + FieldBounds::ALL_SELF, |
| 54 | + ) |
| 55 | + .self_type_trait_bounds(SelfBounds::All(&[Trait::IntoBytes])) |
| 56 | + .inner_extras(quote!( |
| 57 | + // SAFETY: See inline. |
| 58 | + #[inline(always)] |
| 59 | + fn initialize_padding(ptr: #zerocopy_crate::Ptr<'_, Self, ( |
| 60 | + #zerocopy_crate::invariant::Exclusive, |
| 61 | + #zerocopy_crate::invariant::Unaligned, |
| 62 | + #zerocopy_crate::invariant::Valid)> |
| 63 | + ) { |
| 64 | + if false { |
| 65 | + fn assert_is_into_bytes<T>() |
| 66 | + where |
| 67 | + T: #zerocopy_crate::IntoBytes, |
| 68 | + T: ?#core::marker::Sized, |
| 69 | + { |
| 70 | + } |
| 71 | + |
| 72 | + assert_is_into_bytes::<Self>(); |
| 73 | + } |
| 74 | + } |
| 75 | + )) |
| 76 | + .build(), |
| 77 | + ) |
| 78 | + } else { |
| 79 | + None |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +fn derive_initialize_into_bytes_struct( |
| 84 | + ctx: &Ctx, |
| 85 | + strct: &DataStruct, |
| 86 | +) -> Result<TokenStream, Error> { |
| 87 | + let zerocopy_crate = &ctx.zerocopy_crate; |
| 88 | + let core: TokenStream = ctx.core_path(); |
| 89 | + |
| 90 | + // TODO: This is just the default-repr sized case. We also need a |
| 91 | + |
| 92 | + let field_offsets_and_layouts = ctx.ast.data.fields().into_iter().map( |
| 93 | + |(_, name, ty)| quote!((#core::mem::offset_of!(Self, #name), #core::mem::size_of::<#ty>())), |
| 94 | + ); |
| 95 | + |
| 96 | + let subfield_zeroizations = ctx.ast.data.fields().into_iter().map(|(_, name, ty)| { |
| 97 | + quote! {{ |
| 98 | + // TODO: Need to also emit `AsInitialized`/`Valid` projection impls |
| 99 | + // either here or in `TryFromBytes`. |
| 100 | + let field = #zerocopy_crate::into_inner!(ptr.reborrow().project::< |
| 101 | + _, |
| 102 | + { #zerocopy_crate::STRUCT_VARIANT_ID }, |
| 103 | + { #zerocopy_crate::ident_id!(#name) } |
| 104 | + >()); |
| 105 | + <#ty as #zerocopy_crate::InitializeIntoBytes>::initialize_padding(field); |
| 106 | + }} |
| 107 | + }); |
| 108 | + |
| 109 | + Ok(ImplBlockBuilder::new(ctx, &ctx.ast.data, Trait::InitializeIntoBytes, FieldBounds::ALL_SELF) |
| 110 | + .self_type_trait_bounds(SelfBounds::All(&[Trait::Sized, Trait::TryFromBytes])) |
| 111 | + .inner_extras(quote!( |
| 112 | + // SAFETY: See inline. TODO |
| 113 | + #[inline(always)] |
| 114 | + fn initialize_padding(ptr: #zerocopy_crate::Ptr<'_, Self, ( |
| 115 | + #zerocopy_crate::invariant::Exclusive, |
| 116 | + #zerocopy_crate::invariant::Unaligned, |
| 117 | + #zerocopy_crate::invariant::Valid)> |
| 118 | + ) { |
| 119 | + let fields = &#zerocopy_crate::util::sort_fields([ |
| 120 | + #(#field_offsets_and_layouts,)* |
| 121 | + ])[..]; |
| 122 | + |
| 123 | + let mut start = 0; |
| 124 | + while let [(offset, size), rest @ ..] = fields { |
| 125 | + fields = rest; |
| 126 | + |
| 127 | + // Zero-out any padding between `start` and the field. |
| 128 | + { |
| 129 | + let ptr = self as *mut _ as *mut u8; |
| 130 | + let ptr = unsafe { ptr.add(start) }; |
| 131 | + unsafe { #core::ptr::write_bytes(ptr, 0, *offset) }; |
| 132 | + } |
| 133 | + |
| 134 | + // Advance `start`. |
| 135 | + start += size; |
| 136 | + } |
| 137 | + |
| 138 | + #(#subfield_zeroizations)* |
| 139 | + } |
| 140 | + )) |
| 141 | + .build()) |
| 142 | +} |
| 143 | + |
| 144 | +fn derive_initialize_into_bytes_enum(ctx: &Ctx, enm: &DataEnum) -> Result<TokenStream, Error> { |
| 145 | + todo!() |
| 146 | +} |
| 147 | + |
| 148 | +fn derive_initialize_into_bytes_union(ctx: &Ctx, unn: &DataUnion) -> Result<TokenStream, Error> { |
| 149 | + todo!() |
| 150 | +} |
0 commit comments