Skip to content

Commit 1aa5d08

Browse files
committed
cstr append key
1 parent 1c33eb5 commit 1aa5d08

File tree

7 files changed

+146
-159
lines changed

7 files changed

+146
-159
lines changed

src/macros.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,12 @@ macro_rules! rawbson {
240240

241241
// Finished with trailing comma.
242242
(@array [$($elems:expr,)*]) => {
243-
$crate::RawArrayBuf::from_iter(vec![$($elems,)*]).expect("invalid bson value")
243+
$crate::RawArrayBuf::from_iter(vec![$($elems,)*])
244244
};
245245

246246
// Finished without trailing comma.
247247
(@array [$($elems:expr),*]) => {
248-
$crate::RawArrayBuf::from_iter(vec![$($elems),*]).expect("invalid bson value")
248+
$crate::RawArrayBuf::from_iter(vec![$($elems),*])
249249
};
250250

251251
// Next element is `null`.
@@ -292,14 +292,18 @@ macro_rules! rawbson {
292292
(@object $object:ident () () ()) => {};
293293

294294
// Insert the current entry followed by trailing comma.
295-
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
296-
$object.append(($($key)+), $value).expect("invalid bson value");
295+
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {{
296+
let key: &str = ($($key)+).as_ref();
297+
let key: &$crate::raw::CStr = key.try_into().expect("invalid key");
298+
$object.append(key, $value);
297299
$crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
298-
};
300+
}};
299301

300302
// Insert the last entry without trailing comma.
301303
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
302-
$object.append(($($key)+), $value).expect("invalid bson value");
304+
let key: &str = ($($key)+).as_ref();
305+
let key: &$crate::raw::CStr = key.try_into().expect("invalid key");
306+
$object.append(key, $value);
303307
};
304308

305309
// Next value is `null`.

src/raw/array_buf.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,6 @@ impl RawArrayBuf {
5353
}
5454
}
5555

56-
#[allow(clippy::should_implement_trait)]
57-
pub fn from_iter<B, I>(iter: I) -> crate::error::Result<Self>
58-
where
59-
B: BindRawBsonRef,
60-
I: IntoIterator<Item = B>,
61-
{
62-
let mut array_buf = RawArrayBuf::new();
63-
for item in iter {
64-
array_buf.push(item)?;
65-
}
66-
Ok(array_buf)
67-
}
68-
6956
/// Construct a new [`RawArrayBuf`] from the provided [`Vec`] of bytes.
7057
///
7158
/// This involves a traversal of the array to count the values.
@@ -102,10 +89,22 @@ impl RawArrayBuf {
10289
/// assert!(iter.next().is_none());
10390
/// # Ok::<(), Error>(())
10491
/// ```
105-
pub fn push(&mut self, value: impl BindRawBsonRef) -> crate::error::Result<()> {
106-
self.inner.append(self.len.to_string(), value)?;
92+
pub fn push(&mut self, value: impl BindRawBsonRef) {
93+
self.inner.append(
94+
super::CString::from_string_unchecked(self.len.to_string()),
95+
value,
96+
);
10797
self.len += 1;
108-
Ok(())
98+
}
99+
}
100+
101+
impl<B: BindRawBsonRef> FromIterator<B> for RawArrayBuf {
102+
fn from_iter<T: IntoIterator<Item = B>>(iter: T) -> Self {
103+
let mut array_buf = RawArrayBuf::new();
104+
for item in iter {
105+
array_buf.push(item);
106+
}
107+
array_buf
109108
}
110109
}
111110

@@ -200,7 +199,7 @@ impl TryFrom<crate::Array> for RawArrayBuf {
200199
let mut tmp = RawArrayBuf::new();
201200
for val in value {
202201
let raw: super::RawBson = val.try_into()?;
203-
tmp.push(raw)?;
202+
tmp.push(raw);
204203
}
205204
Ok(tmp)
206205
}

src/raw/bson_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ impl<'a> RawBsonRef<'a> {
261261
let options: String = chars.into_iter().collect();
262262
RawBson::RegularExpression(Regex {
263263
pattern: re.pattern.into(),
264-
options: super::CString::from_unchecked(options),
264+
options: super::CString::from_string_unchecked(options),
265265
})
266266
}
267267
RawBsonRef::JavaScriptCode(c) => RawBson::JavaScriptCode(c.to_owned()),

src/raw/cstr.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ impl std::borrow::ToOwned for CStr {
5959
}
6060
}
6161

62+
impl AsRef<CStr> for CStr {
63+
fn as_ref(&self) -> &CStr {
64+
self
65+
}
66+
}
67+
6268
#[cfg(feature = "serde")]
6369
impl serde::Serialize for &CStr {
6470
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
@@ -123,7 +129,7 @@ impl TryFrom<&str> for CString {
123129
}
124130

125131
impl CString {
126-
pub(crate) fn from_unchecked(data: String) -> Self {
132+
pub(crate) fn from_string_unchecked(data: String) -> Self {
127133
Self { data }
128134
}
129135

src/raw/document_buf.rs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -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

912
use 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

Comments
 (0)