Skip to content

Commit 905e2e8

Browse files
authored
Deprecate FromBytes::slice_from (#978)
`FromBytes::ref_from` now supports slices, and is strictly more powerful. Makes progress on #29
1 parent b766d40 commit 905e2e8

File tree

1 file changed

+3
-40
lines changed

1 file changed

+3
-40
lines changed

src/lib.rs

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2296,51 +2296,14 @@ pub unsafe trait FromBytes: FromZeros {
22962296
.map(|(_, r)| r.into_mut())
22972297
}
22982298

2299-
/// Interprets the given `bytes` as a `&[Self]` without copying.
2300-
///
2301-
/// If `bytes.len() % size_of::<Self>() != 0` or `bytes` is not aligned to
2302-
/// `align_of::<Self>()`, this returns `None`.
2303-
///
2304-
/// If you need to convert a specific number of slice elements, see
2305-
/// [`slice_from_prefix`](FromBytes::slice_from_prefix) or
2306-
/// [`slice_from_suffix`](FromBytes::slice_from_suffix).
2307-
///
2308-
/// # Panics
2309-
///
2310-
/// If `Self` is a zero-sized type.
2311-
///
2312-
/// # Examples
2313-
///
2314-
/// ```
2315-
/// use zerocopy::FromBytes;
2316-
/// # use zerocopy_derive::*;
2317-
///
2318-
/// # #[derive(Debug, PartialEq, Eq)]
2319-
/// #[derive(FromBytes, NoCell)]
2320-
/// #[repr(C)]
2321-
/// struct Pixel {
2322-
/// r: u8,
2323-
/// g: u8,
2324-
/// b: u8,
2325-
/// a: u8,
2326-
/// }
2327-
///
2328-
/// // These bytes encode two `Pixel`s.
2329-
/// let bytes = &[0, 1, 2, 3, 4, 5, 6, 7][..];
2330-
///
2331-
/// let pixels = Pixel::slice_from(bytes).unwrap();
2332-
///
2333-
/// assert_eq!(pixels, &[
2334-
/// Pixel { r: 0, g: 1, b: 2, a: 3 },
2335-
/// Pixel { r: 4, g: 5, b: 6, a: 7 },
2336-
/// ]);
2337-
/// ```
2299+
#[deprecated(since = "0.8.0", note = "`FromBytes::ref_from` now supports slices")]
2300+
#[doc(hidden)]
23382301
#[inline]
23392302
fn slice_from(bytes: &[u8]) -> Option<&[Self]>
23402303
where
23412304
Self: Sized + NoCell,
23422305
{
2343-
Ref::<_, [Self]>::new(bytes).map(|r| r.into_ref())
2306+
<[Self]>::ref_from(bytes)
23442307
}
23452308

23462309
/// Interprets the prefix of the given `bytes` as a `&[Self]` with length

0 commit comments

Comments
 (0)