-
Notifications
You must be signed in to change notification settings - Fork 141
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
RUST-1045 Support appending to RawDocumentBuf
#326
RUST-1045 Support appending to RawDocumentBuf
#326
Conversation
This reverts commit 0f96cb0.
use serde::{Deserialize, Serialize}; | ||
|
||
#[test] | ||
fn all_types_json() { |
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 test was moved without modification from test.rs
. The one after it is new though.
@@ -648,13 +648,20 @@ impl<'d, 'de> serde::de::Deserializer<'de> for DocumentKeyDeserializer<'d, 'de> | |||
} | |||
} | |||
|
|||
fn deserialize_newtype_struct<V>(self, _name: &'static str, visitor: V) -> Result<V::Value> |
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 was needed to support deserializing the Cow
wrappers used in deserialization now. It just forwards deserialization to the wrapped type.
} | ||
|
||
/// A visitor used to deserialize types backed by raw BSON. | ||
pub(crate) struct RawBsonVisitor; |
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 was moved to src/raw/serde.rs
and slightly modified so that it could be shared with OwnedRawBson
.
src/raw/document_buf.rs
Outdated
/// assert_eq!(doc.to_document()?, expected); | ||
/// # Ok::<(), Error>(()) | ||
/// ``` | ||
pub fn append(&mut self, key: impl Into<String>, value: impl Into<OwnedRawBson>) { |
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.
Since this only appends to the end, we can't call it insert
(which would imply key lookup). I opted for something different than push
though which seems to be coupled with stacks/lists rather than key-value stores. Let me know if these seems right.
src/raw/owned_bson.rs
Outdated
|
||
/// A BSON value backed by owned raw BSON bytes. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum OwnedRawBson { |
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 type was needed to have something to append to documents. I originally tried using a RawBson<'a>
for this, but it was clunky / awkward, and it would potentially make the rawdoc!
macro impossible.
Regarding the name, I originally opted for RawBson
being the owned one and RawBsonRef
being the borrowed one, but I ended up going with OwnedRawBson
and RawBson
to match how we added the suffix to the owned variants of RawDocument
and RawArray
. I'm still not certain this is the correct naming scheme for RawBson
though, would love to hear your all's thoughts on this.
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 think I'd prefer RawBson
/ RawBsonRef
in this case, although I have a hard time explaining why. OwnedX
seems more unusual than XRef
as Rust type names go.
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 think I agree. I originally thought it would be confusing to have RawDocument
typically be borrowed and RawBson
be owned, but OwnedRawBson
does seem odd enough to warrant the inconsistency. Also, RawDocument
technically isn't a borrowed type anyways, though it's always accessed as a wide-pointer (&RawDocument
).
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.
agreed here, RawBson
and RawBsonRef
feel more intuitive to me
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.
Updated to be RawBsonRef
and RawBson
. I also updated RawBinary<'a>
et al to be RawBinaryRef<'a>
and what not. OwnedRawJavaScriptCodeWithScope
dropped the Owned
prefix. I also renamed RawBsonRef::to_owned_raw_bson
to just RawBsonRef::to_raw_bson
.
src/raw/owned_bson.rs
Outdated
} | ||
|
||
/// Gets a [`RawBson`] value referencing this owned raw BSON value. | ||
pub fn as_raw_bson(&self) -> 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.
Unfortunately, we can't implement Deref
, AsRef
, Borrow
, or ToOwned
between these two types, the first three because the trait requires a reference to be returned and the last one due to the blanket implementation for Clone
types. See rust-lang/rust#44950 for more info.
} | ||
|
||
/// A visitor used to deserialize types backed by raw BSON. | ||
pub(crate) struct OwnedOrBorrowedRawBsonVisitor; |
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 is essentially the same as the old RawBsonVisitor
, but it now can handle owned stuff (e.g. visit_string
, visit_byte_buf
) and arbitrary maps and sequences.
src/raw/array_buf.rs
Outdated
/// bytes. This type can be used to construct owned array values, which can be used to append to | ||
/// [`RawDocumentBuf`] or as a field in a `Deserialize` struct. | ||
/// | ||
/// Iterating over a [`RawArrayBuf`] yields either an error or a key-value pair that borrows from |
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.
key-value pair?
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.
fixed
src/raw/owned_bson.rs
Outdated
|
||
/// A BSON value backed by owned raw BSON bytes. | ||
#[derive(Debug, Clone, PartialEq)] | ||
pub enum OwnedRawBson { |
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 think I'd prefer RawBson
/ RawBsonRef
in this case, although I have a hard time explaining why. OwnedX
seems more unusual than XRef
as Rust type names go.
src/raw/owned_bson.rs
Outdated
|
||
/// Gets a reference to the [`RawArrayBuf`] that's wrapped or returns `None` if the wrapped | ||
/// value isn't a BSON array. | ||
pub fn as_array(&self) -> Option<&'_ RawArray> { |
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.
Since there are now mutating methods for those, I think it'd be good to have as_array_mut -> Option<&mut RawArrayBuf>
and similar for as_document_mut
.
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.
good idea, added
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 modulo naming questions.
RUST-1045
This PR adds a
RawDocumentBuf::append
method which enablesRawDocumentBuf
s to be created and mutated without having to go throughDocument
.To achieve this, a number of public API additions/changes were needed:
OwnedRawBson
type was addedBson
, except the cases that hold documents/arrays useRawDocumentBuf
andRawArrayBuf
(also new type)OwnedRawJavaScriptCodeWithScope
was also added, since the existingJavaScriptCodeWithScope
stored its scope as aDocument
RawDocumentBuf
can now de deserialized from arbitrary mapsRawArrayBuf
and arbitrary sequencesRawDocumentBuf::new
was renamed toRawDocumentBuf::from_bytes
RawDocumentBuf::new()
to be added for constructing brand new documentsRawDocument::new
was also renamed to be consistent with thisRawDocumentBuf
andRawArrayBuf
now implementFromIterator
RawBson
andOwnedRawBson
both implement a number ofFrom
traits