-
Notifications
You must be signed in to change notification settings - Fork 144
RUST-1992 Introduce the &CStr
and CString
types for keys and regular expressions
#563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
ad52055
77a25d5
f9a76c4
2a154b8
84f2e16
a7e34ef
8005834
7918061
7ee8ad7
c904761
f3f742e
8b96eea
1da12e3
aad5212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ use std::{ | |
}; | ||
|
||
use bson::{ | ||
cstr, | ||
doc, | ||
oid::ObjectId, | ||
spec::BinarySubtype, | ||
|
@@ -835,8 +836,8 @@ fn raw_regex() { | |
|
||
let bytes = bson::serialize_to_vec(&doc! { | ||
"r": Regex { | ||
pattern: "a[b-c]d".to_string(), | ||
options: "ab".to_string(), | ||
pattern: cstr!("a[b-c]d").into(), | ||
options: cstr!("ab").into(), | ||
}, | ||
}) | ||
.expect("raw_regex"); | ||
|
@@ -927,8 +928,8 @@ impl AllTypes { | |
}; | ||
let date = DateTime::now(); | ||
let regex = Regex { | ||
pattern: "hello".to_string(), | ||
options: "x".to_string(), | ||
pattern: cstr!("hello").into(), | ||
options: cstr!("x").into(), | ||
}; | ||
let timestamp = Timestamp { | ||
time: 123, | ||
|
@@ -1058,8 +1059,8 @@ fn all_raw_types_rmp() { | |
scope: doc! { "x": 1 }, | ||
}, | ||
"regex": Regex { | ||
pattern: "pattern".to_string(), | ||
options: "opt".to_string() | ||
pattern: cstr!("pattern").into(), | ||
options: cstr!("opt").into() | ||
} | ||
}) | ||
.unwrap(); | ||
|
@@ -1254,24 +1255,22 @@ fn owned_raw_types() { | |
|
||
let f = Foo { | ||
subdoc: RawDocumentBuf::from_iter([ | ||
("a key", RawBson::String("a value".to_string())), | ||
("an objectid", RawBson::ObjectId(oid)), | ||
("a date", RawBson::DateTime(dt)), | ||
(cstr!("a key"), RawBson::String("a value".to_string())), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building from a key-value list is one place where the repeated |
||
(cstr!("an objectid"), RawBson::ObjectId(oid)), | ||
(cstr!("a date"), RawBson::DateTime(dt)), | ||
( | ||
"code_w_scope", | ||
cstr!("code_w_scope"), | ||
RawBson::JavaScriptCodeWithScope(raw_code_w_scope.clone()), | ||
), | ||
("decimal128", RawBson::Decimal128(d128)), | ||
]) | ||
.unwrap(), | ||
(cstr!("decimal128"), RawBson::Decimal128(d128)), | ||
]), | ||
array: RawArrayBuf::from_iter([ | ||
RawBson::String("a string".to_string()), | ||
RawBson::ObjectId(oid), | ||
RawBson::DateTime(dt), | ||
RawBson::JavaScriptCodeWithScope(raw_code_w_scope), | ||
RawBson::Decimal128(d128), | ||
]) | ||
.unwrap(), | ||
]), | ||
}; | ||
|
||
let expected = doc! { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -240,12 +240,12 @@ macro_rules! rawbson { | |
|
||
// Finished with trailing comma. | ||
(@array [$($elems:expr,)*]) => { | ||
$crate::RawArrayBuf::from_iter(vec![$($elems,)*]).expect("invalid bson value") | ||
$crate::RawArrayBuf::from_iter(vec![$($elems,)*]) | ||
}; | ||
|
||
// Finished without trailing comma. | ||
(@array [$($elems:expr),*]) => { | ||
$crate::RawArrayBuf::from_iter(vec![$($elems),*]).expect("invalid bson value") | ||
$crate::RawArrayBuf::from_iter(vec![$($elems),*]) | ||
}; | ||
|
||
// Next element is `null`. | ||
|
@@ -291,15 +291,26 @@ macro_rules! rawbson { | |
// Finished. | ||
(@object $object:ident () () ()) => {}; | ||
|
||
// Insert the current entry followed by trailing comma. | ||
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => { | ||
$object.append(($($key)+), $value).expect("invalid bson value"); | ||
// Insert the current entry with followed by trailing comma, with a key literal. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tweaked the behavior of |
||
(@object $object:ident [$key:literal] ($value:expr) , $($rest:tt)*) => {{ | ||
$object.append($crate::raw::cstr!($key), $value); | ||
$crate::rawbson!(@object $object () ($($rest)*) ($($rest)*)); | ||
}}; | ||
|
||
// Insert the current entry with followed by trailing comma, with a key expression. | ||
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {{ | ||
$object.append($($key)+, $value); | ||
$crate::rawbson!(@object $object () ($($rest)*) ($($rest)*)); | ||
}}; | ||
|
||
// Insert the last entry without trailing comma, with a key literal. | ||
(@object $object:ident [$key:literal] ($value:expr)) => { | ||
$object.append($crate::raw::cstr!($key), $value); | ||
}; | ||
|
||
// Insert the last entry without trailing comma. | ||
// Insert the last entry without trailing comma, with a key expression. | ||
(@object $object:ident [$($key:tt)+] ($value:expr)) => { | ||
$object.append(($($key)+), $value).expect("invalid bson value"); | ||
$object.append($($key)+, $value); | ||
}; | ||
|
||
// Next value is `null`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This really is the whole story in this one line - the failure point has been shifted from writing bytes to the document buffer (
append
and everything that used it) to constructing the string, and that in turn can now be done either at run-time or at compile-time if it's just a literal.