Skip to content

Commit d6e93da

Browse files
committed
Rename AsBytes to IntoBytes
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
1 parent 52a6477 commit d6e93da

File tree

84 files changed

+757
-750
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+757
-750
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ name of that subsystem in square brackets, omitting any "zerocopy" prefix
157157
zerocopy-derive crate:
158158

159159
```text
160-
[derive] Support AsBytes on types with parameters
160+
[derive] Support IntoBytes on types with parameters
161161
```
162162

163163
The body may be omitted if the subject is self-explanatory; e.g. when fixing a

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Zerocopy provides four core marker traits, each of which can be derived
2929
instance of a type
3030
- `FromBytes` indicates that a type may safely be converted from an
3131
arbitrary byte sequence
32-
- `AsBytes` indicates that a type may safely be converted *to* a byte
32+
- `IntoBytes` indicates that a type may safely be converted *to* a byte
3333
sequence
3434
- `Unaligned` indicates that a type's alignment requirement is 1
3535

@@ -77,7 +77,7 @@ for network parsing.
7777

7878
- **`simd`**
7979
When the `simd` feature is enabled, `FromZeros`, `FromBytes`, and
80-
`AsBytes` impls are emitted for all stable SIMD types which exist on the
80+
`IntoBytes` impls are emitted for all stable SIMD types which exist on the
8181
target platform. Note that the layout of SIMD types is not yet stabilized,
8282
so these impls may be removed in the future if layout changes make them
8383
invalid. For more information, see the Unsafe Code Guidelines Reference

src/byteorder.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! this module - [`U16`], [`I16`], [`U32`], [`F64`], etc. Unlike their native
1717
//! counterparts, these types have alignment 1, and take a type parameter
1818
//! specifying the byte order in which the bytes are stored in memory. Each type
19-
//! implements the [`FromBytes`], [`AsBytes`], and [`Unaligned`] traits.
19+
//! implements the [`FromBytes`], [`IntoBytes`], and [`Unaligned`] traits.
2020
//!
2121
//! These two properties, taken together, make these types useful for defining
2222
//! data structures whose memory layout matches a wire format such as that of a
@@ -35,10 +35,10 @@
3535
//!
3636
//! ```rust,edition2021
3737
//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them
38-
//! use zerocopy::{AsBytes, ByteSlice, FromBytes, FromZeros, NoCell, Ref, Unaligned};
38+
//! use zerocopy::{IntoBytes, ByteSlice, FromBytes, FromZeros, NoCell, Ref, Unaligned};
3939
//! use zerocopy::byteorder::network_endian::U16;
4040
//!
41-
//! #[derive(FromZeros, FromBytes, AsBytes, NoCell, Unaligned)]
41+
//! #[derive(FromZeros, FromBytes, IntoBytes, NoCell, Unaligned)]
4242
//! #[repr(C)]
4343
//! struct UdpHeader {
4444
//! src_port: U16,
@@ -265,18 +265,18 @@ order to uphold the invariants that a) the layout of `", stringify!($name), "`
265265
has endianness `O` and that, b) the layout of `", stringify!($native), "` has
266266
the platform's native endianness.
267267
268-
`", stringify!($name), "` implements [`FromBytes`], [`AsBytes`], and [`Unaligned`],
268+
`", stringify!($name), "` implements [`FromBytes`], [`IntoBytes`], and [`Unaligned`],
269269
making it useful for parsing and serialization. See the module documentation for an
270270
example of how it can be used for parsing UDP packets.
271271
272272
[`new`]: crate::byteorder::", stringify!($name), "::new
273273
[`get`]: crate::byteorder::", stringify!($name), "::get
274274
[`set`]: crate::byteorder::", stringify!($name), "::set
275275
[`FromBytes`]: crate::FromBytes
276-
[`AsBytes`]: crate::AsBytes
276+
[`IntoBytes`]: crate::IntoBytes
277277
[`Unaligned`]: crate::Unaligned"),
278278
#[derive(Copy, Clone, Eq, PartialEq, Hash)]
279-
#[cfg_attr(any(feature = "derive", test), derive(KnownLayout, NoCell, FromZeros, FromBytes, AsBytes, Unaligned))]
279+
#[cfg_attr(any(feature = "derive", test), derive(KnownLayout, NoCell, FromZeros, FromBytes, IntoBytes, Unaligned))]
280280
#[repr(transparent)]
281281
pub struct $name<O>([u8; $bytes], PhantomData<O>);
282282
}
@@ -288,12 +288,12 @@ example of how it can be used for parsing UDP packets.
288288
/// SAFETY:
289289
/// `$name<O>` is `repr(transparent)`, and so it has the same layout
290290
/// as its only non-zero field, which is a `u8` array. `u8` arrays
291-
/// are `NoCell`, `FromZeros`, `FromBytes`, `AsBytes`, and
291+
/// are `NoCell`, `FromZeros`, `FromBytes`, `IntoBytes`, and
292292
/// `Unaligned`.
293293
impl_or_verify!(O => NoCell for $name<O>);
294294
impl_or_verify!(O => FromZeros for $name<O>);
295295
impl_or_verify!(O => FromBytes for $name<O>);
296-
impl_or_verify!(O => AsBytes for $name<O>);
296+
impl_or_verify!(O => IntoBytes for $name<O>);
297297
impl_or_verify!(O => Unaligned for $name<O>);
298298
}
299299

@@ -607,7 +607,7 @@ mod tests {
607607

608608
use {
609609
super::*,
610-
crate::{AsBytes, FromBytes, Unaligned},
610+
crate::{FromBytes, IntoBytes, Unaligned},
611611
};
612612

613613
#[cfg(not(kani))]
@@ -655,7 +655,7 @@ mod tests {
655655
use compatibility::*;
656656

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

@@ -692,13 +692,13 @@ mod tests {
692692
}
693693

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

701-
trait ByteOrderType: FromBytes + AsBytes + Unaligned + Copy + Eq + Debug {
701+
trait ByteOrderType: FromBytes + IntoBytes + Unaligned + Copy + Eq + Debug {
702702
type Native: Native;
703703
type ByteArray: ByteArray;
704704

0 commit comments

Comments
 (0)