Skip to content
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

Allow using the {,raw}{bson,doc} with types implementing Into<{,Raw}Bson> #450

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};
}