Skip to content

Commit dd0f191

Browse files
committed
remove emptybuilder
1 parent 3f7797f commit dd0f191

File tree

4 files changed

+73
-95
lines changed

4 files changed

+73
-95
lines changed

Diff for: src/array.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use core::{cmp, fmt};
44
use core::ops::RangeBounds;
55
use crate::builder::{
6-
EmptyBuilder, FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder,
7-
ShortBuf, Truncate,
6+
FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder, ShortBuf,
7+
Truncate,
88
};
99
use crate::octets::{Octets, OctetsFrom};
1010

@@ -161,9 +161,7 @@ impl<const N: usize> OctetsBuilder for Array<N> {
161161
self.len = end;
162162
Ok(())
163163
}
164-
}
165164

166-
impl<const N: usize> EmptyBuilder for Array<N> {
167165
fn empty() -> Self {
168166
Default::default()
169167
}
@@ -325,4 +323,3 @@ impl<'de, const N: usize> serde::de::Visitor<'de> for ArrayVisitor<N> {
325323
Array::try_from(value).map_err(E::custom)
326324
}
327325
}
328-

Diff for: src/builder.rs

+67-85
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ pub trait OctetsBuilder {
4242
/// The error type when appending data fails.
4343
///
4444
/// There are exactly two options for this type: Builders where appending
45-
/// never fails use `Infallible`. Builders with a limited buffer which
45+
/// never fails use `Infallible`. Builders with a limited buffer which
4646
/// may have insufficient space for appending use [`ShortBuf`].
4747
///
4848
/// The trait bound on the type allows upgrading the error to [`ShortBuf`]
@@ -56,88 +56,134 @@ pub trait OctetsBuilder {
5656
///
5757
/// If there isn’t enough space available for appending the slice,
5858
/// returns an error and leaves the builder alone.
59-
fn append_slice(
60-
&mut self, slice: &[u8]
61-
) -> Result<(), Self::AppendError>;
62-
}
59+
fn append_slice(&mut self, slice: &[u8])
60+
-> Result<(), Self::AppendError>;
6361

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

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

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

7879
fn append_slice(
79-
&mut self, slice: &[u8]
80+
&mut self,
81+
slice: &[u8],
8082
) -> Result<(), Self::AppendError> {
8183
self.extend_from_slice(slice);
8284
Ok(())
8385
}
86+
87+
fn empty() -> Self {
88+
Vec::new()
89+
}
90+
91+
fn with_capacity(capacity: usize) -> Self {
92+
Vec::with_capacity(capacity)
93+
}
8494
}
8595

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

90100
fn append_slice(
91-
&mut self, slice: &[u8]
101+
&mut self,
102+
slice: &[u8],
92103
) -> Result<(), Self::AppendError> {
93104
if let Cow::Owned(ref mut vec) = *self {
94105
vec.extend_from_slice(slice);
95106
} else {
96-
let mut vec = std::mem::replace(
97-
self, Cow::Borrowed(b"")
98-
).into_owned();
107+
let mut vec =
108+
std::mem::replace(self, Cow::Borrowed(b"")).into_owned();
99109
vec.extend_from_slice(slice);
100110
*self = Cow::Owned(vec);
101111
}
102112
Ok(())
103113
}
114+
115+
fn empty() -> Self {
116+
Cow::Borrowed(b"")
117+
}
118+
119+
fn with_capacity(capacity: usize) -> Self {
120+
Cow::Owned(Vec::with_capacity(capacity))
121+
}
104122
}
105123

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

110128
fn append_slice(
111-
&mut self, slice: &[u8]
129+
&mut self,
130+
slice: &[u8],
112131
) -> Result<(), Self::AppendError> {
113132
self.extend_from_slice(slice);
114133
Ok(())
115134
}
135+
136+
fn empty() -> Self {
137+
BytesMut::new()
138+
}
139+
140+
fn with_capacity(capacity: usize) -> Self {
141+
BytesMut::with_capacity(capacity)
142+
}
116143
}
117144

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

122149
fn append_slice(
123-
&mut self, slice: &[u8]
150+
&mut self,
151+
slice: &[u8],
124152
) -> Result<(), Self::AppendError> {
125153
self.extend_from_slice(slice);
126154
Ok(())
127155
}
156+
157+
fn empty() -> Self {
158+
smallvec::SmallVec::new()
159+
}
160+
161+
fn with_capacity(capacity: usize) -> Self {
162+
smallvec::SmallVec::with_capacity(capacity)
163+
}
128164
}
129165

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

134170
fn append_slice(
135-
&mut self, slice: &[u8]
171+
&mut self,
172+
slice: &[u8],
136173
) -> Result<(), Self::AppendError> {
137174
self.extend_from_slice(slice).map_err(|_| ShortBuf)
138175
}
139-
}
140176

177+
fn empty() -> Self {
178+
heapless::Vec::new()
179+
}
180+
181+
fn with_capacity(capacity: usize) -> Self {
182+
debug_assert!(capacity <= N);
183+
// Ignore the capacity because that's determined by the type
184+
heapless::Vec::new()
185+
}
186+
}
141187

142188
//------------ Truncate ------------------------------------------------------
143189

@@ -208,70 +254,6 @@ impl<const N: usize> Truncate for heapless::Vec<u8, N> {
208254
}
209255
}
210256

211-
212-
//------------ EmptyBuilder --------------------------------------------------
213-
214-
/// An octets builder that can be newly created empty.
215-
pub trait EmptyBuilder {
216-
/// Creates a new empty octets builder with a default size.
217-
fn empty() -> Self;
218-
219-
/// Creates a new empty octets builder with a suggested initial size.
220-
///
221-
/// The builder may or may not use the size provided by `capacity` as the
222-
/// initial size of the buffer. It may very well be possibly that the
223-
/// builder is never able to grow to this capacity at all. Therefore,
224-
/// even if you create a builder for your data size via this function,
225-
/// appending may still fail.
226-
fn with_capacity(capacity: usize) -> Self;
227-
}
228-
229-
#[cfg(feature = "std")]
230-
impl EmptyBuilder for Vec<u8> {
231-
fn empty() -> Self {
232-
Vec::new()
233-
}
234-
235-
fn with_capacity(capacity: usize) -> Self {
236-
Vec::with_capacity(capacity)
237-
}
238-
}
239-
240-
#[cfg(feature = "bytes")]
241-
impl EmptyBuilder for BytesMut {
242-
fn empty() -> Self {
243-
BytesMut::new()
244-
}
245-
246-
fn with_capacity(capacity: usize) -> Self {
247-
BytesMut::with_capacity(capacity)
248-
}
249-
}
250-
251-
#[cfg(feature = "smallvec")]
252-
impl<A: smallvec::Array<Item = u8>> EmptyBuilder for smallvec::SmallVec<A> {
253-
fn empty() -> Self {
254-
smallvec::SmallVec::new()
255-
}
256-
257-
fn with_capacity(capacity: usize) -> Self {
258-
smallvec::SmallVec::with_capacity(capacity)
259-
}
260-
}
261-
262-
#[cfg(feature = "heapless")]
263-
impl<const N: usize> EmptyBuilder for heapless::Vec<u8, N> {
264-
fn empty() -> Self {
265-
Self::new()
266-
}
267-
268-
fn with_capacity(capacity: usize) -> Self {
269-
debug_assert!(capacity <= N);
270-
Self::with_capacity(capacity)
271-
}
272-
}
273-
274-
275257
//------------ FreezeBuilder -------------------------------------------------
276258

277259
/// An octets builder that can be frozen into a imutable octets sequence.

Diff for: src/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@
3939

4040
pub use self::array::Array;
4141
pub use self::builder::{
42-
EmptyBuilder, FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder,
43-
ShortBuf, Truncate,
42+
FreezeBuilder, FromBuilder, IntoBuilder, OctetsBuilder, ShortBuf,
43+
Truncate,
4444
};
4545
pub use self::octets::{Octets, OctetsFrom, OctetsInto};
4646
pub use self::parse::{Parser, ShortInput};

Diff for: src/str.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
use core::{borrow, cmp, fmt, hash, ops, str};
99
use core::convert::Infallible;
1010
use crate::builder::{
11-
BuilderAppendError, EmptyBuilder, FreezeBuilder, FromBuilder,
12-
OctetsBuilder, Truncate, infallible
11+
infallible, BuilderAppendError, FreezeBuilder, FromBuilder,
12+
OctetsBuilder, Truncate,
1313
};
1414
use crate::octets::OctetsFrom;
1515

@@ -705,4 +705,3 @@ mod test {
705705
assert_eq!(data, "ประเทศไทย中");
706706
}
707707
}
708-

0 commit comments

Comments
 (0)