summaryrefslogtreecommitdiff
path: root/rust/zerocopy-derive/derive/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'rust/zerocopy-derive/derive/mod.rs')
-rw-r--r--rust/zerocopy-derive/derive/mod.rs20
1 files changed, 12 insertions, 8 deletions
diff --git a/rust/zerocopy-derive/derive/mod.rs b/rust/zerocopy-derive/derive/mod.rs
index 665ba7da55a8..b3839fcf73c9 100644
--- a/rust/zerocopy-derive/derive/mod.rs
+++ b/rust/zerocopy-derive/derive/mod.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
pub mod from_bytes;
pub mod into_bytes;
pub mod known_layout;
@@ -15,8 +15,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()
}
@@ -26,7 +26,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> {
@@ -97,16 +97,20 @@ pub(crate) fn derive_split_at(ctx: &Ctx, _top_level: Trait) -> Result<TokenStrea
match &ctx.ast.data {
Data::Struct(_) => {}
Data::Enum(_) | Data::Union(_) => {
- return Err(Error::new(Span::call_site(), "can only be applied to structs"));
+ return ctx
+ .error_or_skip(Error::new(Span::call_site(), "can only be applied to structs"));
}
};
if repr.get_packed().is_some() {
- return Err(Error::new(Span::call_site(), "must not have #[repr(packed)] attribute"));
+ return ctx.error_or_skip(Error::new(
+ Span::call_site(),
+ "must not have #[repr(packed)] attribute",
+ ));
}
if !(repr.is_c() || repr.is_transparent()) {
- return Err(Error::new(
+ return ctx.error_or_skip(Error::new(
Span::call_site(),
"must have #[repr(C)] or #[repr(transparent)] in order to guarantee this type's layout is splitable",
));
@@ -116,7 +120,7 @@ pub(crate) fn derive_split_at(ctx: &Ctx, _top_level: Trait) -> Result<TokenStrea
let trailing_field = if let Some(((_, _, trailing_field), _)) = fields.split_last() {
trailing_field
} else {
- return Err(Error::new(Span::call_site(), "must at least one field"));
+ return ctx.error_or_skip(Error::new(Span::call_site(), "must at least one field"));
};
let zerocopy_crate = &ctx.zerocopy_crate;