-
Notifications
You must be signed in to change notification settings - Fork 148
RUST-1887 Convince append to be more accepting #541
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
Conversation
/// Types that can be consumed to produce raw bson references valid for a limited lifetime. | ||
/// Conceptually a union between `T: Into<RawBson>` and `T: Into<RawBsonRef>`; if your type | ||
/// implements either of those you should consider adding an impl for this as well. | ||
pub trait BindRawBsonRef { |
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.
Because this is consuming a value to produce a reference, it couldn't just be a straightforward Into
-style converter; for owned types that would invalidate the backing storage for the reference we're trying to get. Instead I ended up with this mildly loopy way to consume a value to pass a reference to a closure, which bounds the lifetime.
I named it bind
because in a past life I was a Haskell programmer and this is giving me flashbacks to the infamous monadic bind operator.
src/raw/document_buf.rs
Outdated
}; | ||
} | ||
|
||
raw_bson_ref_from_impls! { |
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 pair of lists are writing out all of the From
impls for RawBsonRef
and RawBson
, with preference given to RawBsonRef
when there's overlap; merging these lists is what the new trait enables.
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.
Does the overlap prevent blanket impls for T where T: Into<RawBson>
and T where T: Into<RawBsonRef>
?
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.
We can't do both but I realized we could do one, so I've revised this to have a blanket impl for T: Into<RawBsonRef>
and a manual list jsut for RawBson
.
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.
I have a question and suggestion, but overall this seems like an improvement to me!
@@ -188,7 +188,6 @@ impl RawDocumentBuf { | |||
/// result in errors when communicating with MongoDB. | |||
/// | |||
/// If the provided key contains an interior null byte, this method will panic. |
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.
Can we add some language here about the acceptable input types and show a borrowed value being appended in the code example?
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.
Done.
src/raw/document_buf.rs
Outdated
}; | ||
} | ||
|
||
raw_bson_ref_from_impls! { |
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.
Does the overlap prevent blanket impls for T where T: Into<RawBson>
and T where T: Into<RawBsonRef>
?
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.
lgtm!
RUST-1887
This turned out to be a bit more of an adventure than I was expecting. Because we (still 😞) don't have specialization, I couldn't directly express "accept anything that's
impl Into<RawBson>
orimpl Into<RawBsonRef>
", so this PR implements the next best thing. It's a bit ugly but I lean towards it being worth it for the better user experience (append
is the core of therawdoc
macro so this will make that transparently accept a wider range of types).