summaryrefslogtreecommitdiff
path: root/rust/zerocopy-derive/derive
diff options
context:
space:
mode:
Diffstat (limited to 'rust/zerocopy-derive/derive')
-rw-r--r--rust/zerocopy-derive/derive/from_bytes.rs2
-rw-r--r--rust/zerocopy-derive/derive/into_bytes.rs2
-rw-r--r--rust/zerocopy-derive/derive/known_layout.rs3
-rw-r--r--rust/zerocopy-derive/derive/mod.rs20
-rw-r--r--rust/zerocopy-derive/derive/try_from_bytes.rs6
-rw-r--r--rust/zerocopy-derive/derive/unaligned.rs2
6 files changed, 19 insertions, 16 deletions
diff --git a/rust/zerocopy-derive/derive/from_bytes.rs b/rust/zerocopy-derive/derive/from_bytes.rs
index d693a63b7645..66d820f6ad4c 100644
--- a/rust/zerocopy-derive/derive/from_bytes.rs
+++ b/rust/zerocopy-derive/derive/from_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use syn::{
parse_quote, Data, DataEnum, DataStruct, DataUnion, Error, Expr, ExprLit, ExprUnary, Lit, UnOp,
diff --git a/rust/zerocopy-derive/derive/into_bytes.rs b/rust/zerocopy-derive/derive/into_bytes.rs
index ad52a6b45d28..0103a78d087f 100644
--- a/rust/zerocopy-derive/derive/into_bytes.rs
+++ b/rust/zerocopy-derive/derive/into_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{Data, DataEnum, DataStruct, DataUnion, Error, Type};
diff --git a/rust/zerocopy-derive/derive/known_layout.rs b/rust/zerocopy-derive/derive/known_layout.rs
index fddffd167c82..d0c4cecfff15 100644
--- a/rust/zerocopy-derive/derive/known_layout.rs
+++ b/rust/zerocopy-derive/derive/known_layout.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::TokenStream;
use quote::quote;
use syn::{parse_quote, Data, Error, Type};
@@ -87,7 +87,6 @@ fn derive_known_layout_for_repr_c_struct<'a>(
};
let inner_extras = {
- let leading_fields_tys = leading_fields_tys.clone();
let methods = make_methods(*trailing_field_ty);
let (_, ty_generics, _) = ctx.ast.generics.split_for_impl();
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;
diff --git a/rust/zerocopy-derive/derive/try_from_bytes.rs b/rust/zerocopy-derive/derive/try_from_bytes.rs
index a3e4a75631a5..44f083328786 100644
--- a/rust/zerocopy-derive/derive/try_from_bytes.rs
+++ b/rust/zerocopy-derive/derive/try_from_bytes.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::TokenStream;
use quote::quote;
use syn::{
@@ -531,8 +531,8 @@ fn derive_has_field_struct_union(ctx: &Ctx, data: &dyn DataExt) -> TokenStream {
data,
Trait::ProjectField {
variant_id: variant_id.clone(),
- field: field.clone(),
- field_id: field_id.clone(),
+ field,
+ field_id,
invariants: parse_quote!((Aliasing, Alignment, #zerocopy_crate::invariant::Initialized)),
},
FieldBounds::None,
diff --git a/rust/zerocopy-derive/derive/unaligned.rs b/rust/zerocopy-derive/derive/unaligned.rs
index d6dea0a11f1e..7c97d62e2dcb 100644
--- a/rust/zerocopy-derive/derive/unaligned.rs
+++ b/rust/zerocopy-derive/derive/unaligned.rs
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: (BSD-2-Clause OR Apache-2.0) OR MIT
-
+//
use proc_macro2::{Span, TokenStream};
use syn::{Data, DataEnum, DataStruct, DataUnion, Error};