Skip to content

Commit be42242

Browse files
committed
Allow any type that converts into bson for insert
1 parent 49bf9bf commit be42242

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/macros.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ macro_rules! doc {
3535
let mut document = $crate::Document::new();
3636

3737
$(
38-
document.insert($key.to_owned(), bson!($val));
38+
document.insert_bson($key.to_owned(), bson!($val));
3939
)*
4040

4141
document

src/ordered.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,15 @@ impl OrderedDocument {
189189
}
190190

191191
/// Sets the value of the entry with the OccupiedEntry's key,
192-
/// and returns the entry's old value.
193-
pub fn insert<KT: Into<String>>(&mut self, key: KT, val: Bson) -> Option<Bson> {
194-
let key = key.into();
192+
/// and returns the entry's old value. Accepts any type that
193+
/// can be converted into Bson.
194+
pub fn insert<KT: Into<String>, BT: Into<Bson>>(&mut self, key: KT, val: BT) -> Option<Bson> {
195+
self.insert_bson(key.into(), val.into())
196+
}
195197

198+
/// Sets the value of the entry with the OccupiedEntry's key,
199+
/// and returns the entry's old value.
200+
pub fn insert_bson(&mut self, key: String, val: Bson) -> Option<Bson> {
196201
{
197202
let key_slice = &key[..];
198203
if self.contains_key(key_slice) {

tests/modules/ordered.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ fn ordered_insert() {
2020
#[test]
2121
fn ordered_insert_shorthand() {
2222
let mut doc = Document::new();
23-
doc.insert("first", Bson::I32(1));
24-
doc.insert("second", Bson::String("foo".to_owned()));
25-
doc.insert("alphanumeric", Bson::String("bar".to_owned()));
23+
doc.insert("first", 1i32);
24+
doc.insert("second", "foo");
25+
doc.insert("alphanumeric", "bar".to_owned());
2626

2727
let expected_keys = vec!(
2828
"first".to_owned(),

0 commit comments

Comments
 (0)