Skip to content

Commit 8b155c5

Browse files
Improve consistency of derive(Byte*) documentation (#2306) (#2465)
This edit brings the structure of these docs in line with that used for our other derives. Co-authored-by: Jack Wrenn <[email protected]>
1 parent b88aa53 commit 8b155c5

File tree

1 file changed

+60
-15
lines changed

1 file changed

+60
-15
lines changed

src/lib.rs

Lines changed: 60 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3250,8 +3250,7 @@ unsafe fn try_read_from<S, T: TryFromBytes>(
32503250
Ok(unsafe { candidate.assume_init() })
32513251
}
32523252

3253-
/// Types for which a sequence of bytes all set to zero represents a valid
3254-
/// instance of the type.
3253+
/// Types for which a sequence of `0` bytes is a valid instance.
32553254
///
32563255
/// Any memory region of the appropriate length which is guaranteed to contain
32573256
/// only zero bytes can be viewed as any `FromZeros` type with no runtime
@@ -5869,14 +5868,37 @@ pub unsafe trait Unaligned {
58695868
Self: Sized;
58705869
}
58715870

5872-
/// Derives an optimized implementation of [`Hash`] for types that implement
5873-
/// [`IntoBytes`] and [`Immutable`].
5871+
/// Derives an optimized [`Hash`] implementation.
58745872
///
5875-
/// The standard library's derive for `Hash` generates a recursive descent
5876-
/// into the fields of the type it is applied to. Instead, the implementation
5877-
/// derived by this macro makes a single call to [`Hasher::write()`] for both
5878-
/// [`Hash::hash()`] and [`Hash::hash_slice()`], feeding the hasher the bytes
5879-
/// of the type or slice all at once.
5873+
/// This derive can be applied to structs and enums implementing both
5874+
/// [`Immutable`] and [`IntoBytes`]; e.g.:
5875+
///
5876+
/// ```
5877+
/// # use zerocopy_derive::{ByteHash, Immutable, IntoBytes};
5878+
/// #[derive(ByteHash, Immutable, IntoBytes)]
5879+
/// #[repr(C)]
5880+
/// struct MyStruct {
5881+
/// # /*
5882+
/// ...
5883+
/// # */
5884+
/// }
5885+
///
5886+
/// #[derive(ByteHash, Immutable, IntoBytes)]
5887+
/// #[repr(u8)]
5888+
/// enum MyEnum {
5889+
/// # Variant,
5890+
/// # /*
5891+
/// ...
5892+
/// # */
5893+
/// }
5894+
/// ```
5895+
///
5896+
/// The standard library's [`derive(Hash)`][derive@Hash] produces hashes by
5897+
/// individually hashing each field and combining the results. Instead, the
5898+
/// implementations of [`Hash::hash()`] and [`Hash::hash_slice()`] generated by
5899+
/// `derive(ByteHash)` convert the entirey of `self` to a byte slice and hashes
5900+
/// it in a single call to [`Hasher::write()`]. This may have performance
5901+
/// advantages.
58805902
///
58815903
/// [`Hash`]: core::hash::Hash
58825904
/// [`Hash::hash()`]: core::hash::Hash::hash()
@@ -5885,13 +5907,36 @@ pub unsafe trait Unaligned {
58855907
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
58865908
pub use zerocopy_derive::ByteHash;
58875909

5888-
/// Derives an optimized implementation of [`PartialEq`] and [`Eq`] for types
5889-
/// that implement [`IntoBytes`] and [`Immutable`].
5910+
/// Derives optimized [`PartialEq`] and [`Eq`] implementations.
5911+
///
5912+
/// This derive can be applied to structs and enums implementing both
5913+
/// [`Immutable`] and [`IntoBytes`]; e.g.:
5914+
///
5915+
/// ```
5916+
/// # use zerocopy_derive::{ByteEq, Immutable, IntoBytes};
5917+
/// #[derive(ByteEq, Immutable, IntoBytes)]
5918+
/// #[repr(C)]
5919+
/// struct MyStruct {
5920+
/// # /*
5921+
/// ...
5922+
/// # */
5923+
/// }
5924+
///
5925+
/// #[derive(ByteEq, Immutable, IntoBytes)]
5926+
/// #[repr(u8)]
5927+
/// enum MyEnum {
5928+
/// # Variant,
5929+
/// # /*
5930+
/// ...
5931+
/// # */
5932+
/// }
5933+
/// ```
58905934
///
5891-
/// The standard library's derive for [`PartialEq`] generates a recursive
5892-
/// descent into the fields of the type it is applied to. Instead, the
5893-
/// implementation derived by this macro performs a single slice comparison of
5894-
/// the bytes of the two values being compared.
5935+
/// The standard library's [`derive(Eq, PartialEq)`][derive@PartialEq] computes
5936+
/// equality by individually comparing each field. Instead, the implementation
5937+
/// of [`PartialEq::eq`] emitted by `derive(ByteHash)` converts the entirey of
5938+
/// `self` and `other` to byte slices and compares those slices for equality.
5939+
/// This may have performance advantages.
58955940
#[cfg(any(feature = "derive", test))]
58965941
#[cfg_attr(doc_cfg, doc(cfg(feature = "derive")))]
58975942
pub use zerocopy_derive::ByteEq;

0 commit comments

Comments
 (0)