From a50e5943f338fbea4320a41e5c1c99938d8440e2 Mon Sep 17 00:00:00 2001 From: Joshua Liebow-Feeser Date: Fri, 20 Sep 2024 12:58:19 -0700 Subject: [PATCH] [byteorder] Clean up docs Makes progress on #1692 --- src/byteorder.rs | 43 ++++++++++++++++++------------------------- 1 file changed, 18 insertions(+), 25 deletions(-) diff --git a/src/byteorder.rs b/src/byteorder.rs index 33a0bedb3d..adcfb3a607 100644 --- a/src/byteorder.rs +++ b/src/byteorder.rs @@ -13,10 +13,10 @@ //! //! For each native multi-byte integer type - `u16`, `i16`, `u32`, etc - and //! floating point type - `f32` and `f64` - an equivalent type is defined by -//! this module - [`U16`], [`I16`], [`U32`], [`F64`], etc. Unlike their native -//! counterparts, these types have alignment 1, and take a type parameter +//! this module - [`U16`], [`I16`], [`U32`], [`F32`], [`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`], [`IntoBytes`], and [`Unaligned`] traits. +//! implements this crate's relevant conversion and marker 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 @@ -33,10 +33,9 @@ //! One use of these types is for representing network packet formats, such as //! UDP: //! -//! ```rust,edition2021 -//! # #[cfg(feature = "derive")] { // This example uses derives, and won't compile without them -//! use zerocopy::{IntoBytes, FromBytes, KnownLayout, Immutable, Ref, SplitByteSlice, Unaligned}; -//! use zerocopy::byteorder::network_endian::U16; +//! ```rust +//! use zerocopy::{*, byteorder::network_endian::U16}; +//! # use zerocopy_derive::*; //! //! #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] //! #[repr(C)] @@ -47,24 +46,18 @@ //! checksum: U16, //! } //! -//! struct UdpPacket { -//! header: Ref, -//! body: B, +//! #[derive(FromBytes, IntoBytes, KnownLayout, Immutable, Unaligned)] +//! #[repr(C, packed)] +//! struct UdpPacket { +//! header: UdpHeader, +//! body: [u8], //! } //! -//! impl UdpPacket { -//! fn parse(bytes: B) -> Option> { -//! let (header, body) = Ref::from_prefix(bytes).ok()?; -//! Some(UdpPacket { header, body }) -//! } -//! -//! fn src_port(&self) -> u16 { -//! self.header.src_port.get() +//! impl UdpPacket { +//! fn parse(bytes: &[u8]) -> Option<&UdpPacket> { +//! UdpPacket::ref_from_bytes(bytes).ok() //! } -//! -//! // more getters... //! } -//! # } //! ``` use core::{ @@ -555,28 +548,28 @@ example of how it can be used for parsing UDP packets. } )* - impl AsRef<[u8; $bytes]> for $name { + impl AsRef<[u8; $bytes]> for $name { #[inline(always)] fn as_ref(&self) -> &[u8; $bytes] { &self.0 } } - impl AsMut<[u8; $bytes]> for $name { + impl AsMut<[u8; $bytes]> for $name { #[inline(always)] fn as_mut(&mut self) -> &mut [u8; $bytes] { &mut self.0 } } - impl PartialEq<$name> for [u8; $bytes] { + impl PartialEq<$name> for [u8; $bytes] { #[inline(always)] fn eq(&self, other: &$name) -> bool { self.eq(&other.0) } } - impl PartialEq<[u8; $bytes]> for $name { + impl PartialEq<[u8; $bytes]> for $name { #[inline(always)] fn eq(&self, other: &[u8; $bytes]) -> bool { self.0.eq(other)