Skip to content

Commit 7e11430

Browse files
authored
Allow using the {,raw}{bson,doc} with types implementing Into<{,Raw}Bson> (#450)
1 parent 9294ee5 commit 7e11430

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ macro_rules! bson {
181181
// Any Into<Bson> type.
182182
// Must be below every other rule.
183183
($other:expr) => {
184-
$crate::Bson::from($other)
184+
<_ as ::std::convert::Into<$crate::Bson>>::into($other)
185185
};
186186
}
187187

@@ -392,7 +392,7 @@ macro_rules! rawbson {
392392
// Any Into<RawBson> type.
393393
// Must be below every other rule.
394394
($other:expr) => {
395-
$crate::RawBson::from($other)
395+
<_ as ::std::convert::Into<$crate::RawBson>>::into($other)
396396
};
397397
}
398398

src/tests/modules/macros.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,34 @@ fn recursive_macro() {
242242

243243
assert_eq!(rawdoc.into_bytes(), crate::to_vec(&doc).unwrap());
244244
}
245+
246+
#[test]
247+
#[allow(clippy::from_over_into)]
248+
fn can_use_macro_with_into_bson() {
249+
struct Custom;
250+
251+
impl Into<Bson> for Custom {
252+
fn into(self) -> Bson {
253+
"foo".into()
254+
}
255+
}
256+
257+
impl Into<RawBson> for Custom {
258+
fn into(self) -> RawBson {
259+
"foo".into()
260+
}
261+
}
262+
263+
_ = bson!({
264+
"a": Custom,
265+
});
266+
_ = doc! {
267+
"a": Custom,
268+
};
269+
_ = rawbson!({
270+
"a": Custom,
271+
});
272+
_ = rawdoc! {
273+
"a": Custom,
274+
};
275+
}

0 commit comments

Comments
 (0)