Skip to content

Commit

Permalink
Rename FromBytes::ref_from[_with_elems]
Browse files Browse the repository at this point in the history
...to `ref_from_bytes[_with_elems]`. This brings the names in line with
the `Ref` methods of the same names, and avoids the awkward name
`ref_from_with_elems`.

Similarly, rename `Ref::unaligned_from` to `unaligned_from_bytes`. We
had already renamed `unaligned_from_with_elems` to
`unaligned_from_bytes_with_elems`.

Makes progress on #871
  • Loading branch information
joshlf committed May 17, 2024
1 parent 2afd4bb commit 552635b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/deprecated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
#[doc(hidden)]
#[inline(always)]
pub fn new_slice_unaligned(bytes: B) -> Option<Ref<B, [T]>> {
Ref::unaligned_from(bytes).ok()
Ref::unaligned_from_bytes(bytes).ok()
}
}

Expand Down
29 changes: 16 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2297,7 +2297,7 @@ pub unsafe trait FromBytes: FromZeros {
/// trailing_dst: [()],
/// }
///
/// let _ = ZSTy::ref_from(0u16.as_bytes()); // ⚠ Compile Error!
/// let _ = ZSTy::ref_from_bytes(0u16.as_bytes()); // ⚠ Compile Error!
/// ```
///
/// # Examples
Expand Down Expand Up @@ -2325,7 +2325,7 @@ pub unsafe trait FromBytes: FromZeros {
/// // These bytes encode a `Packet`.
/// let bytes = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11][..];
///
/// let packet = Packet::ref_from(bytes).unwrap();
/// let packet = Packet::ref_from_bytes(bytes).unwrap();
///
/// assert_eq!(packet.header.src_port, [0, 1]);
/// assert_eq!(packet.header.dst_port, [2, 3]);
Expand All @@ -2335,7 +2335,7 @@ pub unsafe trait FromBytes: FromZeros {
/// ```
#[must_use = "has no side effects"]
#[inline]
fn ref_from(source: &[u8]) -> Result<&Self, CastError<&[u8], Self>>
fn ref_from_bytes(source: &[u8]) -> Result<&Self, CastError<&[u8], Self>>
where
Self: KnownLayout + Immutable,
{
Expand Down Expand Up @@ -2723,7 +2723,7 @@ pub unsafe trait FromBytes: FromZeros {
///
/// let bytes = &[0, 1, 2, 3, 4, 5, 6, 7][..];
///
/// let pixels = <[Pixel]>::ref_from_with_elems(bytes, 2).unwrap();
/// let pixels = <[Pixel]>::ref_from_bytes_with_elems(bytes, 2).unwrap();
///
/// assert_eq!(pixels, &[
/// Pixel { r: 0, g: 1, b: 2, a: 3 },
Expand All @@ -2748,14 +2748,17 @@ pub unsafe trait FromBytes: FromZeros {
/// }
///
/// let src = &[85, 85][..];
/// let zsty = ZSTy::ref_from_with_elems(src, 42).unwrap();
/// let zsty = ZSTy::ref_from_bytes_with_elems(src, 42).unwrap();
/// assert_eq!(zsty.trailing_dst.len(), 42);
/// ```
///
/// [`ref_from`]: FromBytes::ref_from
#[must_use = "has no side effects"]
#[inline]
fn ref_from_with_elems(source: &[u8], count: usize) -> Result<&Self, CastError<&[u8], Self>>
fn ref_from_bytes_with_elems(
source: &[u8],
count: usize,
) -> Result<&Self, CastError<&[u8], Self>>
where
Self: KnownLayout<PointerMetadata = usize> + Immutable,
{
Expand Down Expand Up @@ -3324,7 +3327,7 @@ pub unsafe trait FromBytes: FromZeros {
where
Self: Sized + Immutable,
{
<[Self]>::ref_from(source).ok()
<[Self]>::ref_from_bytes(source).ok()
}
}

Expand Down Expand Up @@ -5807,16 +5810,16 @@ mod tests {
// Fail because the buffer is too large.
let mut buf = Align::<[u8; 16], AU64>::default();
// `buf.t` should be aligned to 8, so only the length check should fail.
assert!(AU64::ref_from(&buf.t[..]).is_err());
assert!(AU64::ref_from_bytes(&buf.t[..]).is_err());
assert!(AU64::mut_from(&mut buf.t[..]).is_err());
assert!(<[u8; 8]>::ref_from(&buf.t[..]).is_err());
assert!(<[u8; 8]>::ref_from_bytes(&buf.t[..]).is_err());
assert!(<[u8; 8]>::mut_from(&mut buf.t[..]).is_err());

// Fail because the buffer is too small.
let mut buf = Align::<[u8; 4], AU64>::default();
assert!(AU64::ref_from(&buf.t[..]).is_err());
assert!(AU64::ref_from_bytes(&buf.t[..]).is_err());
assert!(AU64::mut_from(&mut buf.t[..]).is_err());
assert!(<[u8; 8]>::ref_from(&buf.t[..]).is_err());
assert!(<[u8; 8]>::ref_from_bytes(&buf.t[..]).is_err());
assert!(<[u8; 8]>::mut_from(&mut buf.t[..]).is_err());
assert!(AU64::ref_from_prefix(&buf.t[..]).is_err());
assert!(AU64::mut_from_prefix(&mut buf.t[..]).is_err());
Expand All @@ -5829,9 +5832,9 @@ mod tests {

// Fail because the alignment is insufficient.
let mut buf = Align::<[u8; 13], AU64>::default();
assert!(AU64::ref_from(&buf.t[1..]).is_err());
assert!(AU64::ref_from_bytes(&buf.t[1..]).is_err());
assert!(AU64::mut_from(&mut buf.t[1..]).is_err());
assert!(AU64::ref_from(&buf.t[1..]).is_err());
assert!(AU64::ref_from_bytes(&buf.t[1..]).is_err());
assert!(AU64::mut_from(&mut buf.t[1..]).is_err());
assert!(AU64::ref_from_prefix(&buf.t[1..]).is_err());
assert!(AU64::mut_from_prefix(&mut buf.t[1..]).is_err());
Expand Down
19 changes: 11 additions & 8 deletions src/ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,11 @@ where
/// trailing_dst: [()],
/// }
///
/// let f = Ref::<&[u8], ZSTy>::unaligned_from(&b"UU"[..]); // ⚠ Compile Error!
/// let f = Ref::<&[u8], ZSTy>::unaligned_from_bytes(&b"UU"[..]); // ⚠ Compile Error!
/// ```
#[must_use = "has no side effects"]
#[inline(always)]
pub fn unaligned_from(source: B) -> Result<Ref<B, T>, SizeError<B, T>> {
pub fn unaligned_from_bytes(source: B) -> Result<Ref<B, T>, SizeError<B, T>> {
static_assert_dst_is_not_zst!(T);
match Ref::from_bytes(source) {
Ok(dst) => Ok(dst),
Expand Down Expand Up @@ -1157,7 +1157,7 @@ mod tests {
// for `new_slice`.

let mut buf = [0u8; 8];
test_new_helper_unaligned(Ref::<_, [u8; 8]>::unaligned_from(&mut buf[..]).unwrap());
test_new_helper_unaligned(Ref::<_, [u8; 8]>::unaligned_from_bytes(&mut buf[..]).unwrap());
{
// In a block so that `r` and `suffix` don't live too long.
buf = [0u8; 8];
Expand All @@ -1175,7 +1175,10 @@ mod tests {
let mut buf = [0u8; 16];
// `buf.t` should be aligned to 8 and have a length which is a multiple
// of `size_of::AU64>()`, so this should always succeed.
test_new_helper_slice_unaligned(Ref::<_, [u8]>::unaligned_from(&mut buf[..]).unwrap(), 16);
test_new_helper_slice_unaligned(
Ref::<_, [u8]>::unaligned_from_bytes(&mut buf[..]).unwrap(),
16,
);

{
buf = [0u8; 16];
Expand Down Expand Up @@ -1247,7 +1250,7 @@ mod tests {
Align::<[u8; 16], AU64>::new([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]);

assert_eq!(
AU64::ref_from(&buf.t[8..]).unwrap().0.to_ne_bytes(),
AU64::ref_from_bytes(&buf.t[8..]).unwrap().0.to_ne_bytes(),
[8, 9, 10, 11, 12, 13, 14, 15]
);
let suffix = AU64::mut_from(&mut buf.t[8..]).unwrap();
Expand Down Expand Up @@ -1281,15 +1284,15 @@ mod tests {
let buf = Align::<[u8; 16], AU64>::default();
// `buf.t` should be aligned to 8, so only the length check should fail.
assert!(Ref::<_, AU64>::from_bytes(&buf.t[..]).is_err());
assert!(Ref::<_, [u8; 8]>::unaligned_from(&buf.t[..]).is_err());
assert!(Ref::<_, [u8; 8]>::unaligned_from_bytes(&buf.t[..]).is_err());

// Fail because the buffer is too small.

// A buffer with an alignment of 8.
let buf = Align::<[u8; 4], AU64>::default();
// `buf.t` should be aligned to 8, so only the length check should fail.
assert!(Ref::<_, AU64>::from_bytes(&buf.t[..]).is_err());
assert!(Ref::<_, [u8; 8]>::unaligned_from(&buf.t[..]).is_err());
assert!(Ref::<_, [u8; 8]>::unaligned_from_bytes(&buf.t[..]).is_err());
assert!(Ref::<_, AU64>::from_prefix(&buf.t[..]).is_err());
assert!(Ref::<_, AU64>::from_suffix(&buf.t[..]).is_err());
assert!(Ref::<_, [u8; 8]>::unaligned_from_prefix(&buf.t[..]).is_err());
Expand All @@ -1300,7 +1303,7 @@ mod tests {
let buf = Align::<[u8; 12], AU64>::default();
// `buf.t` has length 12, but element size is 8.
assert!(Ref::<_, [AU64]>::from_bytes(&buf.t[..]).is_err());
assert!(Ref::<_, [[u8; 8]]>::unaligned_from(&buf.t[..]).is_err());
assert!(Ref::<_, [[u8; 8]]>::unaligned_from_bytes(&buf.t[..]).is_err());

// Fail because the buffer is too short.
let buf = Align::<[u8; 12], AU64>::default();
Expand Down

0 comments on commit 552635b

Please sign in to comment.