Skip to content

Add deserialize-utf8-lossy feature to always deserialize using lossy UTF-8 conversion #1187

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

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ snappy-compression = ["dep:snap"]
# The In Use Encryption API is unstable and may have backwards-incompatible changes in minor version updates.
in-use-encryption-unstable = ["dep:mongocrypt", "dep:rayon", "dep:num_cpus"]

deserialize-utf8-lossy = []

# Enables support for emitting tracing events.
# The tracing API is unstable and may have backwards-incompatible changes in minor version updates.
# TODO: pending https://github.com/tokio-rs/tracing/issues/2036 stop depending directly on log.
Expand Down
2 changes: 1 addition & 1 deletion src/change_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ where
/// ```
pub async fn next_if_any(&mut self) -> Result<Option<T>> {
Ok(match NextInBatchFuture::new(self).await? {
BatchValue::Some { doc, .. } => Some(bson::from_slice(doc.as_bytes())?),
BatchValue::Some { doc, .. } => Some(crate::de::from_slice(doc.as_bytes())?),
BatchValue::Empty | BatchValue::Exhausted => None,
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/change_stream/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ where
match bv {
BatchValue::Some { doc, .. } => {
self.data.document_returned = true;
return Ok(Some(bson::from_slice(doc.as_bytes())?));
return Ok(Some(crate::de::from_slice(doc.as_bytes())?));
}
BatchValue::Empty | BatchValue::Exhausted => return Ok(None),
}
Expand Down
2 changes: 1 addition & 1 deletion src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl<T> Cursor<T> {
where
T: Deserialize<'a>,
{
bson::from_slice(self.current().as_bytes()).map_err(Error::from)
crate::de::from_slice(self.current().as_bytes()).map_err(Error::from)
}

/// Update the type streamed values will be parsed as.
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ where
Poll::Pending => return Poll::Pending,
Poll::Ready(bv) => match bv? {
BatchValue::Some { doc, .. } => {
return Poll::Ready(Some(Ok(bson::from_slice(doc.as_bytes())?)))
return Poll::Ready(Some(Ok(crate::de::from_slice(doc.as_bytes())?)))
}
BatchValue::Empty => continue,
BatchValue::Exhausted => return Poll::Ready(None),
Expand Down
2 changes: 1 addition & 1 deletion src/cursor/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ impl<T> SessionCursor<T> {
where
T: Deserialize<'a>,
{
bson::from_slice(self.current().as_bytes()).map_err(Error::from)
crate::de::from_slice(self.current().as_bytes()).map_err(Error::from)
}

/// Update the type streamed values will be parsed as.
Expand Down
10 changes: 10 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,13 @@ compile_error!(
"The feature 'compat-3-0-0' must be enabled to ensure forward compatibility with future \
versions of this crate."
);

#[cfg(not(feature = "deserialize-utf8-lossy"))]
pub(crate) mod de {
pub(crate) use ::bson::from_slice;
}

#[cfg(feature = "deserialize-utf8-lossy")]
pub(crate) mod de {
pub(crate) use ::bson::from_slice_utf8_lossy as from_slice;
}