Skip to content

Commit

Permalink
Replace generic ByteSlices with concrete slices (#204)
Browse files Browse the repository at this point in the history
In `FromBytes` and `AsBytes`, we have some methods which took a `B:
ByteSlice` or `B: ByteSliceMut` bound which was unnecessary. In this
commit, we simplify those methods by just taking `&[u8]` or `&mut [u8]`
instead. This simplifies the API and also may reduce monomorphization
bloat for some users.
  • Loading branch information
joshlf authored Aug 2, 2023
1 parent 1dbc61d commit 08ac09f
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ pub unsafe trait FromBytes: FromZeroes {
/// Reads a copy of `Self` from `bytes`.
///
/// If `bytes.len() != size_of::<Self>()`, `read_from` returns `None`.
fn read_from<B: ByteSlice>(bytes: B) -> Option<Self>
fn read_from(bytes: &[u8]) -> Option<Self>
where
Self: Sized,
{
Expand All @@ -470,7 +470,7 @@ pub unsafe trait FromBytes: FromZeroes {
/// `read_from_prefix` reads a `Self` from the first `size_of::<Self>()`
/// bytes of `bytes`. If `bytes.len() < size_of::<Self>()`, it returns
/// `None`.
fn read_from_prefix<B: ByteSlice>(bytes: B) -> Option<Self>
fn read_from_prefix(bytes: &[u8]) -> Option<Self>
where
Self: Sized,
{
Expand All @@ -483,7 +483,7 @@ pub unsafe trait FromBytes: FromZeroes {
/// `read_from_suffix` reads a `Self` from the last `size_of::<Self>()`
/// bytes of `bytes`. If `bytes.len() < size_of::<Self>()`, it returns
/// `None`.
fn read_from_suffix<B: ByteSlice>(bytes: B) -> Option<Self>
fn read_from_suffix(bytes: &[u8]) -> Option<Self>
where
Self: Sized,
{
Expand Down Expand Up @@ -601,7 +601,7 @@ pub unsafe trait AsBytes {
/// Writes a copy of `self` to `bytes`.
///
/// If `bytes.len() != size_of_val(self)`, `write_to` returns `None`.
fn write_to<B: ByteSliceMut>(&self, mut bytes: B) -> Option<()> {
fn write_to(&self, bytes: &mut [u8]) -> Option<()> {
if bytes.len() != mem::size_of_val(self) {
return None;
}
Expand All @@ -614,7 +614,7 @@ pub unsafe trait AsBytes {
///
/// `write_to_prefix` writes `self` to the first `size_of_val(self)` bytes
/// of `bytes`. If `bytes.len() < size_of_val(self)`, it returns `None`.
fn write_to_prefix<B: ByteSliceMut>(&self, mut bytes: B) -> Option<()> {
fn write_to_prefix(&self, bytes: &mut [u8]) -> Option<()> {
let size = mem::size_of_val(self);
bytes.get_mut(..size)?.copy_from_slice(self.as_bytes());
Some(())
Expand All @@ -624,7 +624,7 @@ pub unsafe trait AsBytes {
///
/// `write_to_suffix` writes `self` to the last `size_of_val(self)` bytes of
/// `bytes`. If `bytes.len() < size_of_val(self)`, it returns `None`.
fn write_to_suffix<B: ByteSliceMut>(&self, mut bytes: B) -> Option<()> {
fn write_to_suffix(&self, bytes: &mut [u8]) -> Option<()> {
let start = bytes.len().checked_sub(mem::size_of_val(self))?;
bytes
.get_mut(start..)
Expand Down

0 comments on commit 08ac09f

Please sign in to comment.