Skip to content

Commit

Permalink
Rename AsBytes to IntoBytes (#700)
Browse files Browse the repository at this point in the history
Previously, `T: AsBytes` indicated that `&T -> &[u8]` was a valid
transformation. As of #682, `T: AsBytes` only indicates that `T -> [u8]`
is a valid transformation. This slightly changes the meaning of
`AsBytes` and makes `IntoBytes` a more appropriate name since it only
permits value rather than reference transmutations. This also brings the
pair of `FromBytes` and `IntoBytes` in line with the standard library
`From` and `Into` traits from a naming perspective.

Closes #695
  • Loading branch information
joshlf authored Dec 14, 2023
1 parent 9c01094 commit 24b5a9b
Show file tree
Hide file tree
Showing 84 changed files with 757 additions and 750 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ name of that subsystem in square brackets, omitting any "zerocopy" prefix
zerocopy-derive crate:

```text
[derive] Support AsBytes on types with parameters
[derive] Support IntoBytes on types with parameters
```

The body may be omitted if the subject is self-explanatory; e.g. when fixing a
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Zerocopy provides four core marker traits, each of which can be derived
instance of a type
- `FromBytes` indicates that a type may safely be converted from an
arbitrary byte sequence
- `AsBytes` indicates that a type may safely be converted *to* a byte
- `IntoBytes` indicates that a type may safely be converted *to* a byte
sequence
- `Unaligned` indicates that a type's alignment requirement is 1

Expand Down Expand Up @@ -77,7 +77,7 @@ for network parsing.

- **`simd`**
When the `simd` feature is enabled, `FromZeros`, `FromBytes`, and
`AsBytes` impls are emitted for all stable SIMD types which exist on the
`IntoBytes` 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
invalid. For more information, see the Unsafe Code Guidelines Reference
Expand Down
24 changes: 12 additions & 12 deletions src/byteorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! this module - [`U16`], [`I16`], [`U32`], [`F64`], etc. Unlike their native
//! counterparts, these types have alignment 1, and take a type parameter
//! specifying the byte order in which the bytes are stored in memory. Each type
//! implements the [`FromBytes`], [`AsBytes`], and [`Unaligned`] traits.
//! implements the [`FromBytes`], [`IntoBytes`], and [`Unaligned`] traits.
//!
//! These two properties, taken together, make these types useful for defining
//! data structures whose memory layout matches a wire format such as that of a
Expand All @@ -35,10 +35,10 @@
//!
//! ```rust,edition2021
//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeros, NoCell, Ref, Unaligned};
//! use zerocopy::{IntoBytes, ByteSlice, FromBytes, FromZeros, NoCell, Ref, Unaligned};
//! use zerocopy::byteorder::network_endian::U16;
//!
//! #[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned)]
//! #[derive(FromZeros, FromBytes, IntoBytes, NoCell, Unaligned)]
//! #[repr(C)]
//! struct UdpHeader {
//! src_port: U16,
Expand Down Expand Up @@ -265,18 +265,18 @@ order to uphold the invariants that a) the layout of `", stringify!($name), "`
has endianness `O` and that, b) the layout of `", stringify!($native), "` has
the platform's native endianness.
`", stringify!($name), "` implements [`FromBytes`], [`AsBytes`], and [`Unaligned`],
`", stringify!($name), "` implements [`FromBytes`], [`IntoBytes`], and [`Unaligned`],
making it useful for parsing and serialization. See the module documentation for an
example of how it can be used for parsing UDP packets.
[`new`]: crate::byteorder::", stringify!($name), "::new
[`get`]: crate::byteorder::", stringify!($name), "::get
[`set`]: crate::byteorder::", stringify!($name), "::set
[`FromBytes`]: crate::FromBytes
[`AsBytes`]: crate::AsBytes
[`IntoBytes`]: crate::IntoBytes
[`Unaligned`]: crate::Unaligned"),
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
#[cfg_attr(any(feature = "derive", test), derive(KnownLayout, NoCell, FromZeros, FromBytes, AsBytes, Unaligned))]
#[cfg_attr(any(feature = "derive", test), derive(KnownLayout, NoCell, FromZeros, FromBytes, IntoBytes, Unaligned))]
#[repr(transparent)]
pub struct $name<O>([u8; $bytes], PhantomData<O>);
}
Expand All @@ -288,12 +288,12 @@ 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 `NoCell`, `FromZeros`, `FromBytes`, `AsBytes`, and
/// are `NoCell`, `FromZeros`, `FromBytes`, `IntoBytes`, and
/// `Unaligned`.
impl_or_verify!(O => NoCell for $name<O>);
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 => IntoBytes for $name<O>);
impl_or_verify!(O => Unaligned for $name<O>);
}

Expand Down Expand Up @@ -607,7 +607,7 @@ mod tests {

use {
super::*,
crate::{AsBytes, FromBytes, Unaligned},
crate::{FromBytes, IntoBytes, Unaligned},
};

#[cfg(not(kani))]
Expand Down Expand Up @@ -655,7 +655,7 @@ mod tests {
use compatibility::*;

// A native integer type (u16, i32, etc).
trait Native: Arbitrary + FromBytes + AsBytes + NoCell + Copy + PartialEq + Debug {
trait Native: Arbitrary + FromBytes + IntoBytes + NoCell + Copy + PartialEq + Debug {
const ZERO: Self;
const MAX_VALUE: Self;

Expand Down Expand Up @@ -692,13 +692,13 @@ mod tests {
}

trait ByteArray:
FromBytes + AsBytes + NoCell + Copy + AsRef<[u8]> + AsMut<[u8]> + Debug + Default + Eq
FromBytes + IntoBytes + NoCell + Copy + AsRef<[u8]> + AsMut<[u8]> + Debug + Default + Eq
{
/// Invert the order of the bytes in the array.
fn invert(self) -> Self;
}

trait ByteOrderType: FromBytes + AsBytes + Unaligned + Copy + Eq + Debug {
trait ByteOrderType: FromBytes + IntoBytes + Unaligned + Copy + Eq + Debug {
type Native: Native;
type ByteArray: ByteArray;

Expand Down
Loading

0 comments on commit 24b5a9b

Please sign in to comment.