-
Notifications
You must be signed in to change notification settings - Fork 180
RUST-1992 Driver changes to track bson serde-optional API changes #1401
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
Changes from 4 commits
3545855
7f933ba
bf9e7a6
72003e7
697361e
9fb7694
124b4fd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,131 @@ | ||
#[cfg(feature = "bson-3")] | ||
pub(crate) trait RawDocumentBufExt { | ||
fn append_ref<'a>( | ||
use crate::bson::RawBson; | ||
|
||
pub(crate) trait RawDocumentBufExt: Sized { | ||
fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()>; | ||
|
||
fn append_ref_err<'a>( | ||
&mut self, | ||
key: impl AsRef<str>, | ||
value: impl Into<crate::bson::raw::RawBsonRef<'a>>, | ||
); | ||
) -> RawResult<()>; | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self>; | ||
} | ||
|
||
#[cfg(feature = "bson-3")] | ||
impl RawDocumentBufExt for crate::bson::RawDocumentBuf { | ||
fn append_ref<'a>( | ||
fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { | ||
self.append(key, value.into()) | ||
} | ||
|
||
fn append_ref_err<'a>( | ||
&mut self, | ||
key: impl AsRef<str>, | ||
value: impl Into<crate::bson::raw::RawBsonRef<'a>>, | ||
) { | ||
) -> RawResult<()> { | ||
self.append(key, value) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
impl RawDocumentBufExt for crate::bson::RawDocumentBuf { | ||
fn append_err(&mut self, key: impl AsRef<str>, value: impl Into<RawBson>) -> RawResult<()> { | ||
self.append(key, value); | ||
Ok(()) | ||
} | ||
|
||
fn append_ref_err<'a>( | ||
&mut self, | ||
key: impl AsRef<str>, | ||
value: impl Into<crate::bson::raw::RawBsonRef<'a>>, | ||
) -> RawResult<()> { | ||
self.append_ref(key, value); | ||
Ok(()) | ||
} | ||
|
||
fn decode_from_bytes(data: Vec<u8>) -> RawResult<Self> { | ||
Self::from_bytes(data) | ||
} | ||
} | ||
|
||
pub(crate) trait RawArrayBufExt: Sized { | ||
#[allow(dead_code)] | ||
fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self>; | ||
|
||
fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()>; | ||
} | ||
|
||
#[cfg(feature = "bson-3")] | ||
pub(crate) use crate::bson::error::Result as RawResult; | ||
impl RawArrayBufExt for crate::bson::RawArrayBuf { | ||
fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { | ||
Self::from_iter(iter.into_iter().map(|v| v.into())) | ||
} | ||
|
||
fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { | ||
self.push(value.into()) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
pub(crate) use crate::bson::raw::Result as RawResult; | ||
impl RawArrayBufExt for crate::bson::RawArrayBuf { | ||
fn from_iter_err<V: Into<RawBson>, I: IntoIterator<Item = V>>(iter: I) -> RawResult<Self> { | ||
Ok(Self::from_iter(iter)) | ||
} | ||
|
||
#[cfg(feature = "bson-3")] | ||
pub(crate) use crate::bson::error::Error as RawError; | ||
fn push_err(&mut self, value: impl Into<RawBson>) -> RawResult<()> { | ||
Ok(self.push(value)) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
pub(crate) use crate::bson::raw::Error as RawError; | ||
pub(crate) trait RawDocumentExt { | ||
fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self>; | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
impl RawDocumentExt for crate::bson::RawDocument { | ||
fn decode_from_bytes<D: AsRef<[u8]> + ?Sized>(data: &D) -> RawResult<&Self> { | ||
Self::from_bytes(data) | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
#[allow(dead_code)] | ||
pub(crate) trait DocumentExt { | ||
fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>>; | ||
} | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
impl DocumentExt for crate::bson::Document { | ||
fn encode_to_vec(&self) -> crate::bson::ser::Result<Vec<u8>> { | ||
let mut out = vec![]; | ||
self.to_writer(&mut out)?; | ||
Ok(out) | ||
} | ||
} | ||
|
||
macro_rules! use_either { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know this is kind of a frivolous use of a macro but I got real tired of writing these out by hand and I find the tabular format a lot easier to read. I can revert if you feel strongly :) |
||
($($name:ident => $path3:path | $path2:path);+;) => { | ||
$( | ||
#[cfg(feature = "bson-3")] | ||
pub(crate) use crate::bson::{$path3 as $name}; | ||
|
||
#[cfg(not(feature = "bson-3"))] | ||
#[allow(unused_imports)] | ||
pub(crate) use crate::bson::{$path2 as $name}; | ||
)+ | ||
}; | ||
} | ||
|
||
// Exported name => bson3 import | bson2 import | ||
use_either! { | ||
RawResult => error::Result | raw::Result; | ||
RawError => error::Error | raw::Error; | ||
serialize_to_raw_document_buf => serialize_to_raw_document_buf | to_raw_document_buf; | ||
serialize_to_document => serialize_to_document | to_document; | ||
serialize_to_bson => serialize_to_bson | to_bson; | ||
deserialize_from_slice => deserialize_from_slice | from_slice; | ||
deserialize_from_document => deserialize_from_document | from_document; | ||
deserialize_from_bson => deserialize_from_bson | from_bson; | ||
} |
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
append
and similar are now fallible in 3.0, the compatibility layer needed to make them fallible (at least at the type level) for 2.x as well, even if they never actually fail. The other option would be to wrap the 3.x variants inexpect
but this way people opting in tobson-3
get nicer error behavior as a bonus :)