@@ -42,7 +42,7 @@ pub trait OctetsBuilder {
42
42
/// The error type when appending data fails.
43
43
///
44
44
/// 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
46
46
/// may have insufficient space for appending use [`ShortBuf`].
47
47
///
48
48
/// The trait bound on the type allows upgrading the error to [`ShortBuf`]
@@ -56,88 +56,134 @@ pub trait OctetsBuilder {
56
56
///
57
57
/// If there isn’t enough space available for appending the slice,
58
58
/// 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 > ;
63
61
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 ;
66
64
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 ;
72
73
}
73
74
74
75
#[ cfg( feature = "std" ) ]
75
76
impl OctetsBuilder for Vec < u8 > {
76
77
type AppendError = Infallible ;
77
78
78
79
fn append_slice (
79
- & mut self , slice : & [ u8 ]
80
+ & mut self ,
81
+ slice : & [ u8 ] ,
80
82
) -> Result < ( ) , Self :: AppendError > {
81
83
self . extend_from_slice ( slice) ;
82
84
Ok ( ( ) )
83
85
}
86
+
87
+ fn empty ( ) -> Self {
88
+ Vec :: new ( )
89
+ }
90
+
91
+ fn with_capacity ( capacity : usize ) -> Self {
92
+ Vec :: with_capacity ( capacity)
93
+ }
84
94
}
85
95
86
96
#[ cfg( feature = "std" ) ]
87
97
impl < ' a > OctetsBuilder for Cow < ' a , [ u8 ] > {
88
98
type AppendError = Infallible ;
89
99
90
100
fn append_slice (
91
- & mut self , slice : & [ u8 ]
101
+ & mut self ,
102
+ slice : & [ u8 ] ,
92
103
) -> Result < ( ) , Self :: AppendError > {
93
104
if let Cow :: Owned ( ref mut vec) = * self {
94
105
vec. extend_from_slice ( slice) ;
95
106
} 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 ( ) ;
99
109
vec. extend_from_slice ( slice) ;
100
110
* self = Cow :: Owned ( vec) ;
101
111
}
102
112
Ok ( ( ) )
103
113
}
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
+ }
104
122
}
105
123
106
124
#[ cfg( feature = "bytes" ) ]
107
125
impl OctetsBuilder for BytesMut {
108
126
type AppendError = Infallible ;
109
127
110
128
fn append_slice (
111
- & mut self , slice : & [ u8 ]
129
+ & mut self ,
130
+ slice : & [ u8 ] ,
112
131
) -> Result < ( ) , Self :: AppendError > {
113
132
self . extend_from_slice ( slice) ;
114
133
Ok ( ( ) )
115
134
}
135
+
136
+ fn empty ( ) -> Self {
137
+ BytesMut :: new ( )
138
+ }
139
+
140
+ fn with_capacity ( capacity : usize ) -> Self {
141
+ BytesMut :: with_capacity ( capacity)
142
+ }
116
143
}
117
144
118
145
#[ cfg( feature = "smallvec" ) ]
119
146
impl < A : smallvec:: Array < Item = u8 > > OctetsBuilder for smallvec:: SmallVec < A > {
120
147
type AppendError = Infallible ;
121
148
122
149
fn append_slice (
123
- & mut self , slice : & [ u8 ]
150
+ & mut self ,
151
+ slice : & [ u8 ] ,
124
152
) -> Result < ( ) , Self :: AppendError > {
125
153
self . extend_from_slice ( slice) ;
126
154
Ok ( ( ) )
127
155
}
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
+ }
128
164
}
129
165
130
166
#[ cfg( feature = "heapless" ) ]
131
167
impl < const N : usize > OctetsBuilder for heapless:: Vec < u8 , N > {
132
168
type AppendError = ShortBuf ;
133
169
134
170
fn append_slice (
135
- & mut self , slice : & [ u8 ]
171
+ & mut self ,
172
+ slice : & [ u8 ] ,
136
173
) -> Result < ( ) , Self :: AppendError > {
137
174
self . extend_from_slice ( slice) . map_err ( |_| ShortBuf )
138
175
}
139
- }
140
176
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
+ }
141
187
142
188
//------------ Truncate ------------------------------------------------------
143
189
@@ -208,70 +254,6 @@ impl<const N: usize> Truncate for heapless::Vec<u8, N> {
208
254
}
209
255
}
210
256
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
-
275
257
//------------ FreezeBuilder -------------------------------------------------
276
258
277
259
/// An octets builder that can be frozen into a imutable octets sequence.
0 commit comments