@@ -4,7 +4,10 @@ use std::{
44 ops:: Deref ,
55} ;
66
7- use crate :: { raw:: MIN_BSON_DOCUMENT_SIZE , Document } ;
7+ use crate :: {
8+ raw:: { CStr , MIN_BSON_DOCUMENT_SIZE } ,
9+ Document ,
10+ } ;
811
912use super :: { bson:: RawBson , iter:: Iter , RawBsonRef , RawDocument , RawIter , Result } ;
1013
@@ -87,20 +90,6 @@ impl RawDocumentBuf {
8790 Self :: decode_from_bytes ( buf)
8891 }
8992
90- #[ allow( clippy:: should_implement_trait) ]
91- pub fn from_iter < S , B , I > ( iter : I ) -> Result < Self >
92- where
93- S : AsRef < str > ,
94- B : BindRawBsonRef ,
95- I : IntoIterator < Item = ( S , B ) > ,
96- {
97- let mut buf = RawDocumentBuf :: new ( ) ;
98- for ( k, v) in iter {
99- buf. append ( k, v) ?;
100- }
101- Ok ( buf)
102- }
103-
10493 /// Create a [`RawDocumentBuf`] from a [`Document`].
10594 ///
10695 /// ```
@@ -117,8 +106,9 @@ impl RawDocumentBuf {
117106 pub fn from_document ( doc : impl Borrow < Document > ) -> Result < Self > {
118107 let mut out = RawDocumentBuf :: new ( ) ;
119108 for ( k, v) in doc. borrow ( ) {
109+ let k: & CStr = k. as_str ( ) . try_into ( ) ?;
120110 let val: RawBson = v. clone ( ) . try_into ( ) ?;
121- out. append ( k, val) ? ;
111+ out. append ( k, val) ;
122112 }
123113 Ok ( out)
124114 }
@@ -215,14 +205,19 @@ impl RawDocumentBuf {
215205 /// assert_eq!(doc.to_document()?, expected);
216206 /// # Ok::<(), Error>(())
217207 /// ```
218- pub fn append (
219- & mut self ,
220- key : impl AsRef < str > ,
221- value : impl BindRawBsonRef ,
222- ) -> crate :: error:: Result < ( ) > {
223- let key = key. as_ref ( ) . try_into ( ) ?;
224- Ok ( value
225- . bind ( |value_ref| raw_writer:: RawWriter :: new ( & mut self . data ) . append ( key, value_ref) ) )
208+ pub fn append ( & mut self , key : impl AsRef < CStr > , value : impl BindRawBsonRef ) {
209+ let key = key. as_ref ( ) ;
210+ value. bind ( |value_ref| raw_writer:: RawWriter :: new ( & mut self . data ) . append ( key, value_ref) ) ;
211+ }
212+ }
213+
214+ impl < K : AsRef < CStr > , B : BindRawBsonRef > FromIterator < ( K , B ) > for RawDocumentBuf {
215+ fn from_iter < T : IntoIterator < Item = ( K , B ) > > ( iter : T ) -> Self {
216+ let mut buf = RawDocumentBuf :: new ( ) ;
217+ for ( k, v) in iter {
218+ buf. append ( k, v) ;
219+ }
220+ buf
226221 }
227222}
228223
@@ -287,8 +282,9 @@ impl TryFrom<Document> for RawDocumentBuf {
287282 fn try_from ( doc : Document ) -> std:: result:: Result < Self , Self :: Error > {
288283 let mut out = RawDocumentBuf :: new ( ) ;
289284 for ( k, v) in doc {
285+ let k: & CStr = k. as_str ( ) . try_into ( ) ?;
290286 let val: RawBson = v. try_into ( ) ?;
291- out. append ( k, val) ? ;
287+ out. append ( k, val) ;
292288 }
293289 Ok ( out)
294290 }
0 commit comments