@@ -70,7 +70,8 @@ pub struct RawDocumentBuf {
7070impl RawDocumentBuf {
7171 /// Creates a new, empty [`RawDocumentBuf`].
7272 pub fn new ( ) -> RawDocumentBuf {
73- let mut data: Vec < u8 > = MIN_BSON_DOCUMENT_SIZE . to_le_bytes ( ) . to_vec ( ) ;
73+ let mut data = Vec :: new ( ) ;
74+ data. extend ( & MIN_BSON_DOCUMENT_SIZE . to_le_bytes ( ) ) ;
7475 data. push ( 0 ) ;
7576 Self { data }
7677 }
@@ -192,27 +193,27 @@ impl RawDocumentBuf {
192193 /// assert_eq!(doc.to_document()?, expected);
193194 /// # Ok::<(), Error>(())
194195 /// ```
195- pub fn append ( & mut self , key : impl Into < String > , value : impl Into < RawBson > ) {
196- fn append_string ( doc : & mut RawDocumentBuf , value : String ) {
196+ pub fn append ( & mut self , key : impl AsRef < str > , value : impl Into < RawBson > ) {
197+ fn append_string ( doc : & mut RawDocumentBuf , value : & str ) {
197198 doc. data
198199 . extend ( & ( ( value. as_bytes ( ) . len ( ) + 1 ) as i32 ) . to_le_bytes ( ) ) ;
199- doc. data . extend ( value. into_bytes ( ) ) ;
200+ doc. data . extend ( value. as_bytes ( ) ) ;
200201 doc. data . push ( 0 ) ;
201202 }
202203
203- fn append_cstring ( doc : & mut RawDocumentBuf , value : String ) {
204+ fn append_cstring ( doc : & mut RawDocumentBuf , value : & str ) {
204205 if value. contains ( '\0' ) {
205206 panic ! ( "cstr includes interior null byte: {}" , value)
206207 }
207- doc. data . extend ( value. into_bytes ( ) ) ;
208+ doc. data . extend ( value. as_bytes ( ) ) ;
208209 doc. data . push ( 0 ) ;
209210 }
210211
211212 let original_len = self . data . len ( ) ;
212213
213214 // write the key for the next value to the end
214215 // the element type will replace the previous null byte terminator of the document
215- append_cstring ( self , key. into ( ) ) ;
216+ append_cstring ( self , key. as_ref ( ) ) ;
216217
217218 let value = value. into ( ) ;
218219 let element_type = value. element_type ( ) ;
@@ -222,7 +223,7 @@ impl RawDocumentBuf {
222223 self . data . extend ( & i. to_le_bytes ( ) ) ;
223224 }
224225 RawBson :: String ( s) => {
225- append_string ( self , s) ;
226+ append_string ( self , s. as_str ( ) ) ;
226227 }
227228 RawBson :: Document ( d) => {
228229 self . data . extend ( d. into_bytes ( ) ) ;
@@ -251,7 +252,7 @@ impl RawDocumentBuf {
251252 self . data . extend ( & dt. timestamp_millis ( ) . to_le_bytes ( ) ) ;
252253 }
253254 RawBson :: DbPointer ( dbp) => {
254- append_string ( self , dbp. namespace ) ;
255+ append_string ( self , dbp. namespace . as_str ( ) ) ;
255256 self . data . extend ( & dbp. id . bytes ( ) ) ;
256257 }
257258 RawBson :: Decimal128 ( d) => {
@@ -264,11 +265,11 @@ impl RawDocumentBuf {
264265 self . data . extend ( & i. to_le_bytes ( ) ) ;
265266 }
266267 RawBson :: RegularExpression ( re) => {
267- append_cstring ( self , re. pattern ) ;
268- append_cstring ( self , re. options ) ;
268+ append_cstring ( self , re. pattern . as_str ( ) ) ;
269+ append_cstring ( self , re. options . as_str ( ) ) ;
269270 }
270271 RawBson :: JavaScriptCode ( js) => {
271- append_string ( self , js) ;
272+ append_string ( self , js. as_str ( ) ) ;
272273 }
273274 RawBson :: JavaScriptCodeWithScope ( code_w_scope) => {
274275 let len = RawJavaScriptCodeWithScopeRef {
@@ -277,7 +278,7 @@ impl RawDocumentBuf {
277278 }
278279 . len ( ) ;
279280 self . data . extend ( & len. to_le_bytes ( ) ) ;
280- append_string ( self , code_w_scope. code ) ;
281+ append_string ( self , code_w_scope. code . as_str ( ) ) ;
281282 self . data . extend ( code_w_scope. scope . into_bytes ( ) ) ;
282283 }
283284 RawBson :: Timestamp ( ts) => {
@@ -287,7 +288,7 @@ impl RawDocumentBuf {
287288 self . data . extend ( & oid. bytes ( ) ) ;
288289 }
289290 RawBson :: Symbol ( s) => {
290- append_string ( self , s) ;
291+ append_string ( self , s. as_str ( ) ) ;
291292 }
292293 RawBson :: Null | RawBson :: Undefined | RawBson :: MinKey | RawBson :: MaxKey => { }
293294 }
@@ -296,8 +297,8 @@ impl RawDocumentBuf {
296297 // append trailing null byte
297298 self . data . push ( 0 ) ;
298299 // update length
299- self . data
300- . splice ( 0 ..4 , ( self . data . len ( ) as i32 ) . to_le_bytes ( ) . iter ( ) . cloned ( ) ) ;
300+ let new_len = ( self . data . len ( ) as i32 ) . to_le_bytes ( ) ;
301+ self . data [ 0 ..4 ] . copy_from_slice ( & new_len ) ;
301302 }
302303
303304 /// Convert this [`RawDocumentBuf`] to a [`Document`], returning an error
@@ -389,7 +390,7 @@ impl Borrow<RawDocument> for RawDocumentBuf {
389390 }
390391}
391392
392- impl < S : Into < String > , T : Into < RawBson > > FromIterator < ( S , T ) > for RawDocumentBuf {
393+ impl < S : AsRef < str > , T : Into < RawBson > > FromIterator < ( S , T ) > for RawDocumentBuf {
393394 fn from_iter < I : IntoIterator < Item = ( S , T ) > > ( iter : I ) -> Self {
394395 let mut buf = RawDocumentBuf :: new ( ) ;
395396 for ( k, v) in iter {
0 commit comments