Skip to content

Commit 2ac2cb7

Browse files
committed
fix serde-tests
1 parent fe810e9 commit 2ac2cb7

File tree

3 files changed

+33
-24
lines changed

3 files changed

+33
-24
lines changed

serde-tests/json.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde_json::json;
33

44
use super::AllTypes;
55

6-
use bson::{doc, Bson, JavaScriptCodeWithScope, RawArrayBuf, RawBson, RawDocumentBuf};
6+
use bson::{cstr, doc, Bson, JavaScriptCodeWithScope, RawArrayBuf, RawBson, RawDocumentBuf};
77

88
use serde::{Deserialize, Serialize};
99

@@ -99,18 +99,18 @@ fn owned_raw_bson() {
9999
});
100100

101101
let mut doc_buf = RawDocumentBuf::new();
102-
doc_buf.append("a", "key").unwrap();
103-
doc_buf.append("number", 12).unwrap();
104-
doc_buf.append("bool", false).unwrap();
105-
doc_buf.append("nu", RawBson::Null).unwrap();
102+
doc_buf.append(cstr!("a"), "key");
103+
doc_buf.append(cstr!("number"), 12);
104+
doc_buf.append(cstr!("bool"), false);
105+
doc_buf.append(cstr!("nu"), RawBson::Null);
106106

107107
let mut array_buf = RawArrayBuf::new();
108-
array_buf.push(1).unwrap();
109-
array_buf.push("string").unwrap();
108+
array_buf.push(1);
109+
array_buf.push("string");
110110

111111
let mut bson_doc = RawDocumentBuf::new();
112-
bson_doc.append("first", true).unwrap();
113-
bson_doc.append("second", "string").unwrap();
112+
bson_doc.append(cstr!("first"), true);
113+
bson_doc.append(cstr!("second"), "string");
114114

115115
let expected = Foo {
116116
doc_buf,

serde-tests/test.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::{
1818
};
1919

2020
use bson::{
21+
cstr,
2122
doc,
2223
oid::ObjectId,
2324
spec::BinarySubtype,
@@ -835,8 +836,8 @@ fn raw_regex() {
835836

836837
let bytes = bson::serialize_to_vec(&doc! {
837838
"r": Regex {
838-
pattern: "a[b-c]d".to_string(),
839-
options: "ab".to_string(),
839+
pattern: cstr!("a[b-c]d").into(),
840+
options: cstr!("ab").into(),
840841
},
841842
})
842843
.expect("raw_regex");
@@ -927,8 +928,8 @@ impl AllTypes {
927928
};
928929
let date = DateTime::now();
929930
let regex = Regex {
930-
pattern: "hello".to_string(),
931-
options: "x".to_string(),
931+
pattern: cstr!("hello").into(),
932+
options: cstr!("x").into(),
932933
};
933934
let timestamp = Timestamp {
934935
time: 123,
@@ -1058,8 +1059,8 @@ fn all_raw_types_rmp() {
10581059
scope: doc! { "x": 1 },
10591060
},
10601061
"regex": Regex {
1061-
pattern: "pattern".to_string(),
1062-
options: "opt".to_string()
1062+
pattern: cstr!("pattern").into(),
1063+
options: cstr!("opt").into()
10631064
}
10641065
})
10651066
.unwrap();
@@ -1254,24 +1255,22 @@ fn owned_raw_types() {
12541255

12551256
let f = Foo {
12561257
subdoc: RawDocumentBuf::from_iter([
1257-
("a key", RawBson::String("a value".to_string())),
1258-
("an objectid", RawBson::ObjectId(oid)),
1259-
("a date", RawBson::DateTime(dt)),
1258+
(cstr!("a key"), RawBson::String("a value".to_string())),
1259+
(cstr!("an objectid"), RawBson::ObjectId(oid)),
1260+
(cstr!("a date"), RawBson::DateTime(dt)),
12601261
(
1261-
"code_w_scope",
1262+
cstr!("code_w_scope"),
12621263
RawBson::JavaScriptCodeWithScope(raw_code_w_scope.clone()),
12631264
),
1264-
("decimal128", RawBson::Decimal128(d128)),
1265-
])
1266-
.unwrap(),
1265+
(cstr!("decimal128"), RawBson::Decimal128(d128)),
1266+
]),
12671267
array: RawArrayBuf::from_iter([
12681268
RawBson::String("a string".to_string()),
12691269
RawBson::ObjectId(oid),
12701270
RawBson::DateTime(dt),
12711271
RawBson::JavaScriptCodeWithScope(raw_code_w_scope),
12721272
RawBson::Decimal128(d128),
1273-
])
1274-
.unwrap(),
1273+
]),
12751274
};
12761275

12771276
let expected = doc! {

src/raw/cstr.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,3 +183,13 @@ impl std::borrow::Borrow<CStr> for CString {
183183
self.as_ref()
184184
}
185185
}
186+
187+
#[cfg(feature = "serde")]
188+
impl serde::Serialize for CString {
189+
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
190+
where
191+
S: serde::Serializer,
192+
{
193+
self.data.serialize(serializer)
194+
}
195+
}

0 commit comments

Comments
 (0)