Skip to content
Open
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
6 changes: 3 additions & 3 deletions zerocopy-derive/src/derive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use crate::{
util::{Ctx, DataExt, FieldBounds, ImplBlockBuilder, Trait},
};

pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> TokenStream {
match &ctx.ast.data {
pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Error> {
Ok(match &ctx.ast.data {
Data::Struct(strct) => {
ImplBlockBuilder::new(ctx, strct, Trait::Immutable, FieldBounds::ALL_SELF).build()
}
Expand All @@ -24,7 +24,7 @@ pub(crate) fn derive_immutable(ctx: &Ctx, _top_level: Trait) -> TokenStream {
Data::Union(unn) => {
ImplBlockBuilder::new(ctx, unn, Trait::Immutable, FieldBounds::ALL_SELF).build()
}
}
})
}

pub(crate) fn derive_hash(ctx: &Ctx, _top_level: Trait) -> Result<TokenStream, Error> {
Expand Down
32 changes: 32 additions & 0 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,38 @@ derive!(ByteHash => derive_hash => crate::derive::derive_hash);
derive!(ByteEq => derive_eq => crate::derive::derive_eq);
derive!(SplitAt => derive_split_at => crate::derive::derive_split_at);

#[cfg(not(zerocopy_unstable_derive_on_error))]
#[proc_macro_derive(most_traits, attributes(zerocopy))]
pub fn most_traits(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse_macro_input!(ts as DeriveInput);
let ctx = match Ctx::try_from_derive_input(ast) {
Ok(ctx) => ctx,
Err(e) => return e.into_compile_error().into(),
}
.skip_on_error();

// top-level traits for which to attempt a derive
let derives: [(fn(&Ctx, Trait) -> _, _); 4] = [
(crate::derive::known_layout::derive, Trait::KnownLayout),
(crate::derive::derive_immutable, Trait::Immutable),
(crate::derive::try_from_bytes::derive_try_from_bytes, Trait::FromBytes),
(crate::derive::into_bytes::derive_into_bytes, Trait::IntoBytes),
];

let mut tokens = proc_macro2::TokenStream::new();
for (derive, t) in derives {
tokens.extend(derive(&ctx, t))
}

// We wrap in `const_block` as a backstop in case any derive fails
// to wrap its output in `const_block` (and thus fails to annotate)
// with the full set of `#[allow(...)]` attributes).
let ts = const_block([Some(tokens)]);
#[cfg(test)]
crate::util::testutil::check_hygiene(ts.clone());
ts.into()
}

/// Deprecated: prefer [`FromZeros`] instead.
#[deprecated(since = "0.8.0", note = "`FromZeroes` was renamed to `FromZeros`")]
#[doc(hidden)]
Expand Down
5 changes: 5 additions & 0 deletions zerocopy-derive/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ impl Ctx {
}
}

pub(crate) fn skip_on_error(mut self) -> Self {
self.skip_on_error = true;
self
}

pub(crate) fn core_path(&self) -> TokenStream {
let zerocopy_crate = &self.zerocopy_crate;
quote!(#zerocopy_crate::util::macro_util::core_reexport)
Expand Down
Loading