Skip to content

Commit

Permalink
Allow using the {,raw}{bson,doc} with types implementing Into<{,Raw}B…
Browse files Browse the repository at this point in the history
…son> (#450)
  • Loading branch information
tyilo authored Jan 18, 2024
1 parent 9294ee5 commit 7e11430
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ macro_rules! bson {
// Any Into<Bson> type.
// Must be below every other rule.
($other:expr) => {
$crate::Bson::from($other)
<_ as ::std::convert::Into<$crate::Bson>>::into($other)
};
}

Expand Down Expand Up @@ -392,7 +392,7 @@ macro_rules! rawbson {
// Any Into<RawBson> type.
// Must be below every other rule.
($other:expr) => {
$crate::RawBson::from($other)
<_ as ::std::convert::Into<$crate::RawBson>>::into($other)
};
}

Expand Down
31 changes: 31 additions & 0 deletions src/tests/modules/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,3 +242,34 @@ fn recursive_macro() {

assert_eq!(rawdoc.into_bytes(), crate::to_vec(&doc).unwrap());
}

#[test]
#[allow(clippy::from_over_into)]
fn can_use_macro_with_into_bson() {
struct Custom;

impl Into<Bson> for Custom {
fn into(self) -> Bson {
"foo".into()
}
}

impl Into<RawBson> for Custom {
fn into(self) -> RawBson {
"foo".into()
}
}

_ = bson!({
"a": Custom,
});
_ = doc! {
"a": Custom,
};
_ = rawbson!({
"a": Custom,
});
_ = rawdoc! {
"a": Custom,
};
}

0 comments on commit 7e11430

Please sign in to comment.