Skip to content

Commit

Permalink
rename FromZeroes to FromZeros
Browse files Browse the repository at this point in the history
`FromZeroes` is retained as a deprecated, `doc(hidden)` re-export.
  • Loading branch information
jswrenn committed Sep 29, 2023
1 parent cc7a0eb commit 1f18eaf
Show file tree
Hide file tree
Showing 33 changed files with 397 additions and 392 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ so you don't have to.
## Overview

Zerocopy provides four core marker traits, each of which can be derived
(e.g., `#[derive(FromZeroes)]`):
- `FromZeroes` indicates that a sequence of zero bytes represents a valid
(e.g., `#[derive(FromZeros)]`):
- `FromZeros` indicates that a sequence of zero bytes represents a valid
instance of a type
- `FromBytes` indicates that a type may safely be converted from an
arbitrary byte sequence
Expand Down Expand Up @@ -72,7 +72,7 @@ for network parsing.
```

- **`simd`**
When the `simd` feature is enabled, `FromZeroes`, `FromBytes`, and
When the `simd` feature is enabled, `FromZeros`, `FromBytes`, and
`AsBytes` impls are emitted for all stable SIMD types which exist on the
target platform. Note that the layout of SIMD types is not yet stabilized,
so these impls may be removed in the future if layout changes make them
Expand Down
10 changes: 5 additions & 5 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
//!
//! ```rust,edition2021
//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeroes, Ref, Unaligned};
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeros, Ref, Unaligned};
//! use zerocopy::byteorder::network_endian::U16;
//!
//! #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
//! #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
//! #[repr(C)]
//! struct UdpHeader {
//! src_port: U16,
Expand Down Expand Up @@ -176,7 +176,7 @@ example of how it can be used for parsing UDP packets.
[`AsBytes`]: crate::AsBytes
[`Unaligned`]: crate::Unaligned"),
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(any(feature = "derive", test), derive(FromZeroes, FromBytes, AsBytes, Unaligned))]
#[cfg_attr(any(feature = "derive", test), derive(FromZeros, FromBytes, AsBytes, Unaligned))]
#[repr(transparent)]
pub struct $name<O>([u8; $bytes], PhantomData<O>);
}
Expand All @@ -185,8 +185,8 @@ example of how it can be used for parsing UDP packets.
/// SAFETY:
/// `$name<O>` is `repr(transparent)`, and so it has the same layout
/// as its only non-zero field, which is a `u8` array. `u8` arrays
/// are `FromZeroes`, `FromBytes`, `AsBytes`, and `Unaligned`.
impl_or_verify!(O => FromZeroes for $name<O>);
/// are `FromZeros`, `FromBytes`, `AsBytes`, and `Unaligned`.
impl_or_verify!(O => FromZeros for $name<O>);
impl_or_verify!(O => FromBytes for $name<O>);
impl_or_verify!(O => AsBytes for $name<O>);
impl_or_verify!(O => Unaligned for $name<O>);
Expand Down
329 changes: 167 additions & 162 deletions src/lib.rs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,15 @@ macro_rules! unsafe_impl {
///
/// ```rust,ignore
/// // Note that these derives are gated by `feature = "derive"`
/// #[cfg_attr(any(feature = "derive", test), derive(FromZeroes, FromBytes, AsBytes, Unaligned))]
/// #[cfg_attr(any(feature = "derive", test), derive(FromZeros, FromBytes, AsBytes, Unaligned))]
/// #[repr(transparent)]
/// struct Wrapper<T>(T);
///
/// safety_comment! {
/// /// SAFETY:
/// /// `Wrapper<T>` is `repr(transparent)`, so it is sound to implement any
/// /// zerocopy trait if `T` implements that trait.
/// impl_or_verify!(T: FromZeroes => FromZeroes for Wrapper<T>);
/// impl_or_verify!(T: FromZeros => FromZeros for Wrapper<T>);
/// impl_or_verify!(T: FromBytes => FromBytes for Wrapper<T>);
/// impl_or_verify!(T: AsBytes => AsBytes for Wrapper<T>);
/// impl_or_verify!(T: Unaligned => Unaligned for Wrapper<T>);
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ pub(crate) mod testutil {
// Though `u64` has alignment 8 on some platforms, it's not guaranteed.
// By contrast, `AU64` is guaranteed to have alignment 8.
#[derive(
FromZeroes, FromBytes, AsBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone,
FromZeros, FromBytes, AsBytes, Eq, PartialEq, Ord, PartialOrd, Default, Debug, Copy, Clone,
)]
#[repr(C, align(8))]
pub(crate) struct AU64(pub(crate) u64);
Expand Down
6 changes: 3 additions & 3 deletions src/wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ use super::*;
// [3] https://github.com/google/zerocopy/issues/209
#[allow(missing_debug_implementations)]
#[derive(Default, Copy)]
#[cfg_attr(any(feature = "derive", test), derive(FromZeroes, FromBytes, AsBytes, Unaligned))]
#[cfg_attr(any(feature = "derive", test), derive(FromZeros, FromBytes, AsBytes, Unaligned))]
#[repr(C, packed)]
pub struct Unalign<T>(T);

Expand All @@ -63,9 +63,9 @@ safety_comment! {
/// - `Unalign<T>` is `repr(packed)`, so it is unaligned regardless of the
/// alignment of `T`, and so we don't require that `T: Unaligned`
/// - `Unalign<T>` has the same bit validity as `T`, and so it is
/// `FromZeroes`, `FromBytes`, or `AsBytes` exactly when `T` is as well.
/// `FromZeros`, `FromBytes`, or `AsBytes` exactly when `T` is as well.
impl_or_verify!(T => Unaligned for Unalign<T>);
impl_or_verify!(T: FromZeroes => FromZeroes for Unalign<T>);
impl_or_verify!(T: FromZeros => FromZeros for Unalign<T>);
impl_or_verify!(T: FromBytes => FromBytes for Unalign<T>);
impl_or_verify!(T: AsBytes => AsBytes for Unalign<T>);
}
Expand Down
20 changes: 10 additions & 10 deletions tests/ui-msrv/invalid-impls/invalid-impls.stderr
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
error[E0277]: the trait bound `T: zerocopy::FromZeroes` is not satisfied
error[E0277]: the trait bound `T: zerocopy::FromZeros` is not satisfied
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
|
| impl<$($tyvar $(: $(? $optbound +)* $($bound +)*)?),*> Subtrait for $ty {}
| ^^^^^^^^ the trait `zerocopy::FromZeroes` is not implemented for `T`
| ^^^^^^^^ the trait `zerocopy::FromZeros` is not implemented for `T`
|
::: tests/ui-msrv/invalid-impls/invalid-impls.rs:22:1
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ------------------------------------------- in this macro invocation
|
note: required because of the requirements on the impl of `zerocopy::FromZeroes` for `Foo<T>`
note: required because of the requirements on the impl of `zerocopy::FromZeros` for `Foo<T>`
--> tests/ui-msrv/invalid-impls/invalid-impls.rs:18:10
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
Expand All @@ -22,12 +22,12 @@ note: required by a bound in `_::Subtrait`
|
::: tests/ui-msrv/invalid-impls/invalid-impls.rs:22:1
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ------------------------------------------- in this macro invocation
= note: this error originates in the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
|
22 | impl_or_verify!(T: zerocopy::FromZeroes => FromZeroes for Foo<T>);
22 | impl_or_verify!(T: zerocopy::FromZeros => FromZeros for Foo<T>);
| ++++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
Expand All @@ -44,7 +44,7 @@ error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
note: required because of the requirements on the impl of `zerocopy::FromBytes` for `Foo<T>`
--> tests/ui-msrv/invalid-impls/invalid-impls.rs:18:22
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
Expand Down Expand Up @@ -76,7 +76,7 @@ error[E0277]: the trait bound `T: zerocopy::AsBytes` is not satisfied
note: required because of the requirements on the impl of `zerocopy::AsBytes` for `Foo<T>`
--> tests/ui-msrv/invalid-impls/invalid-impls.rs:18:33
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
Expand Down Expand Up @@ -108,7 +108,7 @@ error[E0277]: the trait bound `T: zerocopy::Unaligned` is not satisfied
note: required because of the requirements on the impl of `zerocopy::Unaligned` for `Foo<T>`
--> tests/ui-msrv/invalid-impls/invalid-impls.rs:18:42
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^
note: required by a bound in `_::Subtrait`
--> tests/ui-msrv/invalid-impls/../../../src/macros.rs
Expand Down
4 changes: 2 additions & 2 deletions tests/ui-nightly/invalid-impls/invalid-impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use zerocopy_derive::*;

fn main() {}

#[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
#[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
#[repr(transparent)]
struct Foo<T>(T);

impl_or_verify!(T => FromZeroes for Foo<T>);
impl_or_verify!(T => FromZeros for Foo<T>);
impl_or_verify!(T => FromBytes for Foo<T>);
impl_or_verify!(T => AsBytes for Foo<T>);
impl_or_verify!(T => Unaligned for Foo<T>);
22 changes: 11 additions & 11 deletions tests/ui-nightly/invalid-impls/invalid-impls.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0277]: the trait bound `T: zerocopy::FromZeroes` is not satisfied
error[E0277]: the trait bound `T: zerocopy::FromZeros` is not satisfied
--> tests/ui-nightly/invalid-impls/invalid-impls.rs:22:37
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
| ^^^^^^ the trait `zerocopy::FromZeroes` is not implemented for `T`
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ^^^^^^ the trait `zerocopy::FromZeros` is not implemented for `T`
|
note: required for `Foo<T>` to implement `zerocopy::FromZeroes`
note: required for `Foo<T>` to implement `zerocopy::FromZeros`
--> tests/ui-nightly/invalid-impls/invalid-impls.rs:18:10
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-nightly/invalid-impls/../../../src/macros.rs
Expand All @@ -17,12 +17,12 @@ note: required by a bound in `_::Subtrait`
|
::: tests/ui-nightly/invalid-impls/invalid-impls.rs:22:1
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ------------------------------------------- in this macro invocation
= note: this error originates in the derive macro `FromZeroes` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the derive macro `FromZeros` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
|
22 | impl_or_verify!(T: zerocopy::FromZeroes => FromZeroes for Foo<T>);
22 | impl_or_verify!(T: zerocopy::FromZeros => FromZeros for Foo<T>);
| ++++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
Expand All @@ -34,7 +34,7 @@ error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::FromBytes`
--> tests/ui-nightly/invalid-impls/invalid-impls.rs:18:22
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-nightly/invalid-impls/../../../src/macros.rs
Expand All @@ -61,7 +61,7 @@ error[E0277]: the trait bound `T: zerocopy::AsBytes` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::AsBytes`
--> tests/ui-nightly/invalid-impls/invalid-impls.rs:18:33
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-nightly/invalid-impls/../../../src/macros.rs
Expand All @@ -88,7 +88,7 @@ error[E0277]: the trait bound `T: zerocopy::Unaligned` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::Unaligned`
--> tests/ui-nightly/invalid-impls/invalid-impls.rs:18:42
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-nightly/invalid-impls/../../../src/macros.rs
Expand Down
22 changes: 11 additions & 11 deletions tests/ui-stable/invalid-impls/invalid-impls.stderr
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
error[E0277]: the trait bound `T: zerocopy::FromZeroes` is not satisfied
error[E0277]: the trait bound `T: zerocopy::FromZeros` is not satisfied
--> tests/ui-stable/invalid-impls/invalid-impls.rs:22:37
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
| ^^^^^^ the trait `zerocopy::FromZeroes` is not implemented for `T`
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ^^^^^^ the trait `zerocopy::FromZeros` is not implemented for `T`
|
note: required for `Foo<T>` to implement `zerocopy::FromZeroes`
note: required for `Foo<T>` to implement `zerocopy::FromZeros`
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:10
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
Expand All @@ -17,12 +17,12 @@ note: required by a bound in `_::Subtrait`
|
::: tests/ui-stable/invalid-impls/invalid-impls.rs:22:1
|
22 | impl_or_verify!(T => FromZeroes for Foo<T>);
22 | impl_or_verify!(T => FromZeros for Foo<T>);
| ------------------------------------------- in this macro invocation
= note: this error originates in the derive macro `FromZeroes` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
= note: this error originates in the derive macro `FromZeros` which comes from the expansion of the macro `impl_or_verify` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider restricting type parameter `T`
|
22 | impl_or_verify!(T: zerocopy::FromZeroes => FromZeroes for Foo<T>);
22 | impl_or_verify!(T: zerocopy::FromZeros => FromZeros for Foo<T>);
| ++++++++++++++++++++++

error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
Expand All @@ -34,7 +34,7 @@ error[E0277]: the trait bound `T: zerocopy::FromBytes` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::FromBytes`
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:22
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
Expand All @@ -61,7 +61,7 @@ error[E0277]: the trait bound `T: zerocopy::AsBytes` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::AsBytes`
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:33
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
Expand All @@ -88,7 +88,7 @@ error[E0277]: the trait bound `T: zerocopy::Unaligned` is not satisfied
note: required for `Foo<T>` to implement `zerocopy::Unaligned`
--> tests/ui-stable/invalid-impls/invalid-impls.rs:18:42
|
18 | #[derive(FromZeroes, FromBytes, AsBytes, Unaligned)]
18 | #[derive(FromZeros, FromBytes, AsBytes, Unaligned)]
| ^^^^^^^^^ unsatisfied trait bound introduced in this `derive` macro
note: required by a bound in `_::Subtrait`
--> tests/ui-stable/invalid-impls/../../../src/macros.rs
Expand Down
24 changes: 12 additions & 12 deletions zerocopy-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ use {crate::ext::*, crate::repr::*};
// help: required by the derive of FromBytes
//
// Instead, we have more verbose error messages like "unsupported representation
// for deriving FromZeroes, FromBytes, AsBytes, or Unaligned on an enum"
// for deriving FromZeros, FromBytes, AsBytes, or Unaligned on an enum"
//
// This will probably require Span::error
// (https://doc.rust-lang.org/nightly/proc_macro/struct.Span.html#method.error),
// which is currently unstable. Revisit this once it's stable.

#[proc_macro_derive(FromZeroes)]
#[proc_macro_derive(FromZeros)]
pub fn derive_from_zeroes(ts: proc_macro::TokenStream) -> proc_macro::TokenStream {
let ast = syn::parse_macro_input!(ts as DeriveInput);
match &ast.data {
Expand Down Expand Up @@ -117,20 +117,20 @@ const STRUCT_UNION_ALLOWED_REPR_COMBINATIONS: &[&[StructRepr]] = &[
&[StructRepr::C, StructRepr::Packed],
];

// A struct is `FromZeroes` if:
// - all fields are `FromZeroes`
// A struct is `FromZeros` if:
// - all fields are `FromZeros`

fn derive_from_zeroes_struct(ast: &DeriveInput, strct: &DataStruct) -> proc_macro2::TokenStream {
impl_block(ast, strct, "FromZeroes", true, None)
impl_block(ast, strct, "FromZeros", true, None)
}

// An enum is `FromZeroes` if:
// An enum is `FromZeros` if:
// - all of its variants are fieldless
// - one of the variants has a discriminant of `0`

fn derive_from_zeroes_enum(ast: &DeriveInput, enm: &DataEnum) -> proc_macro2::TokenStream {
if !enm.is_c_like() {
return Error::new_spanned(ast, "only C-like enums can implement FromZeroes")
return Error::new_spanned(ast, "only C-like enums can implement FromZeros")
.to_compile_error();
}

Expand All @@ -150,19 +150,19 @@ fn derive_from_zeroes_enum(ast: &DeriveInput, enm: &DataEnum) -> proc_macro2::To
if !has_explicit_zero_discriminant && !has_implicit_zero_discriminant {
return Error::new_spanned(
ast,
"FromZeroes only supported on enums with a variant that has a discriminant of `0`",
"FromZeros only supported on enums with a variant that has a discriminant of `0`",
)
.to_compile_error();
}

impl_block(ast, enm, "FromZeroes", true, None)
impl_block(ast, enm, "FromZeros", true, None)
}

// Like structs, unions are `FromZeroes` if
// - all fields are `FromZeroes`
// Like structs, unions are `FromZeros` if
// - all fields are `FromZeros`

fn derive_from_zeroes_union(ast: &DeriveInput, unn: &DataUnion) -> proc_macro2::TokenStream {
impl_block(ast, unn, "FromZeroes", true, None)
impl_block(ast, unn, "FromZeros", true, None)
}

// A struct is `FromBytes` if:
Expand Down
Loading

0 comments on commit 1f18eaf

Please sign in to comment.