Skip to content

Commit

Permalink
remove emptybuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Jun 26, 2024
1 parent 3f7797f commit dd0f191
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 95 deletions.
7 changes: 2 additions & 5 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
use core::{cmp, fmt};
use core::ops::RangeBounds;
use crate::builder::{
EmptyBuilder, FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder,
ShortBuf, Truncate,
FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder, ShortBuf,
Truncate,
};
use crate::octets::{Octets, OctetsFrom};

Expand Down Expand Up @@ -161,9 +161,7 @@ impl<const N: usize> OctetsBuilder for Array<N> {
self.len = end;
Ok(())
}
}

impl<const N: usize> EmptyBuilder for Array<N> {
fn empty() -> Self {
Default::default()
}
Expand Down Expand Up @@ -325,4 +323,3 @@ impl<'de, const N: usize> serde::de::Visitor<'de> for ArrayVisitor<N> {
Array::try_from(value).map_err(E::custom)
}
}

152 changes: 67 additions & 85 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub trait OctetsBuilder {
/// The error type when appending data fails.
///
/// There are exactly two options for this type: Builders where appending
/// never fails use `Infallible`. Builders with a limited buffer which
/// never fails use `Infallible`. Builders with a limited buffer which
/// may have insufficient space for appending use [`ShortBuf`].
///
/// The trait bound on the type allows upgrading the error to [`ShortBuf`]
Expand All @@ -56,88 +56,134 @@ pub trait OctetsBuilder {
///
/// If there isn’t enough space available for appending the slice,
/// returns an error and leaves the builder alone.
fn append_slice(
&mut self, slice: &[u8]
) -> Result<(), Self::AppendError>;
}
fn append_slice(&mut self, slice: &[u8])
-> Result<(), Self::AppendError>;

impl<'a, T: OctetsBuilder> OctetsBuilder for &'a mut T {
type AppendError = T::AppendError;
/// Creates a new empty octets builder with a default size.
fn empty() -> Self;

fn append_slice(
&mut self, slice: &[u8]
) -> Result<(), Self::AppendError> {
(*self).append_slice(slice)
}
/// Creates a new empty octets builder with a suggested initial size.
///
/// The builder may or may not use the size provided by `capacity` as the
/// initial size of the buffer. It may very well be possibly that the
/// builder is never able to grow to this capacity at all. Therefore,
/// even if you create a builder for your data size via this function,
/// appending may still fail.
fn with_capacity(capacity: usize) -> Self;
}

#[cfg(feature = "std")]
impl OctetsBuilder for Vec<u8> {
type AppendError = Infallible;

fn append_slice(
&mut self, slice: &[u8]
&mut self,
slice: &[u8],
) -> Result<(), Self::AppendError> {
self.extend_from_slice(slice);
Ok(())
}

fn empty() -> Self {
Vec::new()
}

fn with_capacity(capacity: usize) -> Self {
Vec::with_capacity(capacity)
}
}

#[cfg(feature = "std")]
impl<'a> OctetsBuilder for Cow<'a, [u8]> {
type AppendError = Infallible;

fn append_slice(
&mut self, slice: &[u8]
&mut self,
slice: &[u8],
) -> Result<(), Self::AppendError> {
if let Cow::Owned(ref mut vec) = *self {
vec.extend_from_slice(slice);
} else {
let mut vec = std::mem::replace(
self, Cow::Borrowed(b"")
).into_owned();
let mut vec =
std::mem::replace(self, Cow::Borrowed(b"")).into_owned();
vec.extend_from_slice(slice);
*self = Cow::Owned(vec);
}
Ok(())
}

fn empty() -> Self {
Cow::Borrowed(b"")
}

fn with_capacity(capacity: usize) -> Self {
Cow::Owned(Vec::with_capacity(capacity))
}
}

#[cfg(feature = "bytes")]
impl OctetsBuilder for BytesMut {
type AppendError = Infallible;

fn append_slice(
&mut self, slice: &[u8]
&mut self,
slice: &[u8],
) -> Result<(), Self::AppendError> {
self.extend_from_slice(slice);
Ok(())
}

fn empty() -> Self {
BytesMut::new()
}

fn with_capacity(capacity: usize) -> Self {
BytesMut::with_capacity(capacity)
}
}

#[cfg(feature = "smallvec")]
impl<A: smallvec::Array<Item = u8>> OctetsBuilder for smallvec::SmallVec<A> {
type AppendError = Infallible;

fn append_slice(
&mut self, slice: &[u8]
&mut self,
slice: &[u8],
) -> Result<(), Self::AppendError> {
self.extend_from_slice(slice);
Ok(())
}

fn empty() -> Self {
smallvec::SmallVec::new()
}

fn with_capacity(capacity: usize) -> Self {
smallvec::SmallVec::with_capacity(capacity)
}
}

#[cfg(feature = "heapless")]
impl<const N: usize> OctetsBuilder for heapless::Vec<u8, N> {
type AppendError = ShortBuf;

fn append_slice(
&mut self, slice: &[u8]
&mut self,
slice: &[u8],
) -> Result<(), Self::AppendError> {
self.extend_from_slice(slice).map_err(|_| ShortBuf)
}
}

fn empty() -> Self {
heapless::Vec::new()
}

fn with_capacity(capacity: usize) -> Self {
debug_assert!(capacity <= N);
// Ignore the capacity because that's determined by the type
heapless::Vec::new()
}
}

//------------ Truncate ------------------------------------------------------

Expand Down Expand Up @@ -208,70 +254,6 @@ impl<const N: usize> Truncate for heapless::Vec<u8, N> {
}
}


//------------ EmptyBuilder --------------------------------------------------

/// An octets builder that can be newly created empty.
pub trait EmptyBuilder {
/// Creates a new empty octets builder with a default size.
fn empty() -> Self;

/// Creates a new empty octets builder with a suggested initial size.
///
/// The builder may or may not use the size provided by `capacity` as the
/// initial size of the buffer. It may very well be possibly that the
/// builder is never able to grow to this capacity at all. Therefore,
/// even if you create a builder for your data size via this function,
/// appending may still fail.
fn with_capacity(capacity: usize) -> Self;
}

#[cfg(feature = "std")]
impl EmptyBuilder for Vec<u8> {
fn empty() -> Self {
Vec::new()
}

fn with_capacity(capacity: usize) -> Self {
Vec::with_capacity(capacity)
}
}

#[cfg(feature = "bytes")]
impl EmptyBuilder for BytesMut {
fn empty() -> Self {
BytesMut::new()
}

fn with_capacity(capacity: usize) -> Self {
BytesMut::with_capacity(capacity)
}
}

#[cfg(feature = "smallvec")]
impl<A: smallvec::Array<Item = u8>> EmptyBuilder for smallvec::SmallVec<A> {
fn empty() -> Self {
smallvec::SmallVec::new()
}

fn with_capacity(capacity: usize) -> Self {
smallvec::SmallVec::with_capacity(capacity)
}
}

#[cfg(feature = "heapless")]
impl<const N: usize> EmptyBuilder for heapless::Vec<u8, N> {
fn empty() -> Self {
Self::new()
}

fn with_capacity(capacity: usize) -> Self {
debug_assert!(capacity <= N);
Self::with_capacity(capacity)
}
}


//------------ FreezeBuilder -------------------------------------------------

/// An octets builder that can be frozen into a imutable octets sequence.
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@

pub use self::array::Array;
pub use self::builder::{
EmptyBuilder, FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder,
ShortBuf, Truncate,
FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder, ShortBuf,
Truncate,
};
pub use self::octets::{Octets, OctetsFrom, OctetsInto};
pub use self::parse::{Parser, ShortInput};
Expand Down
5 changes: 2 additions & 3 deletions src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use core::{borrow, cmp, fmt, hash, ops, str};
use core::convert::Infallible;
use crate::builder::{
BuilderAppendError, EmptyBuilder, FreezeBuilder, FromBuilder,
OctetsBuilder, Truncate, infallible
infallible, BuilderAppendError, FreezeBuilder, FromBuilder,
OctetsBuilder, Truncate,
};
use crate::octets::OctetsFrom;

Expand Down Expand Up @@ -705,4 +705,3 @@ mod test {
assert_eq!(data, "ประเทศไทย中");
}
}

0 comments on commit dd0f191

Please sign in to comment.