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

Bytes const size borrowed arrays #325

Merged
merged 2 commits into from
Jun 14, 2021
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
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

* The `Bytes` type now supports borrowed and Cow arrays of fixed size (requires Rust 1.51+)

```rust
#[serde_as(as = "Bytes")]
#[serde(borrow)]
borrowed_array: &'a [u8; 15],
#[serde_as(as = "Bytes")]
#[serde(borrow)]
cow_array: Cow<'a, [u8; 15]>,
```

Note: For borrowed arrays the used Deserializer needs to support Serde's 0-copy deserialization.

## [1.9.2] - 2021-06-07

### Fixed
Expand Down
130 changes: 130 additions & 0 deletions src/de/const_arrays.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::*;
use crate::utils::{MapIter, SeqIter};
use serde::de::*;
use std::borrow::Cow;
use std::collections::{BTreeMap, HashMap};
use std::convert::TryInto;
use std::fmt;
Expand Down Expand Up @@ -191,6 +192,135 @@ impl<'de, const N: usize> DeserializeAs<'de, [u8; N]> for Bytes {
}
}

impl<'de, const N: usize> DeserializeAs<'de, &'de [u8; N]> for Bytes {
fn deserialize_as<D>(deserializer: D) -> Result<&'de [u8; N], D::Error>
where
D: Deserializer<'de>,
{
struct ArrayVisitor<const M: usize>;

impl<'de, const M: usize> Visitor<'de> for ArrayVisitor<M> {
type Value = &'de [u8; M];

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_fmt(format_args!("a borrowed byte array of size {}", M))
}

fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
where
E: Error,
{
v.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))
}

fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
v.as_bytes()
.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))
}
}

deserializer.deserialize_bytes(ArrayVisitor::<N>)
}
}

impl<'de, const N: usize> DeserializeAs<'de, Cow<'de, [u8; N]>> for Bytes {
fn deserialize_as<D>(deserializer: D) -> Result<Cow<'de, [u8; N]>, D::Error>
where
D: Deserializer<'de>,
{
struct CowVisitor<const M: usize>;

impl<'de, const M: usize> Visitor<'de> for CowVisitor<M> {
type Value = Cow<'de, [u8; M]>;

fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a byte array")
}

fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Borrowed(
v.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))?,
))
}

fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Borrowed(
v.as_bytes()
.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))?,
))
}

fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(
v.to_vec()
.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))?,
))
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(Cow::Owned(
v.as_bytes()
.to_vec()
.try_into()
.map_err(|_| Error::invalid_length(v.len(), &self))?,
))
}

fn visit_byte_buf<E>(self, v: Vec<u8>) -> Result<Self::Value, E>
where
E: Error,
{
let len = v.len();
Ok(Cow::Owned(
v.try_into()
.map_err(|_| Error::invalid_length(len, &self))?,
))
}

fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
where
E: Error,
{
let len = v.len();
Ok(Cow::Owned(
v.into_bytes()
.try_into()
.map_err(|_| Error::invalid_length(len, &self))?,
))
}

fn visit_seq<V>(self, seq: V) -> Result<Self::Value, V::Error>
where
V: SeqAccess<'de>,
{
Ok(Cow::Owned(array_from_iterator(SeqIter::new(seq), &self)?))
}
}

deserializer.deserialize_bytes(CowVisitor)
}
}

impl<'de, const N: usize> DeserializeAs<'de, Box<[u8; N]>> for Bytes {
fn deserialize_as<D>(deserializer: D) -> Result<Box<[u8; N]>, D::Error>
where
Expand Down
55 changes: 54 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1333,11 +1333,13 @@ pub struct TimestampNanoSecondsWithFrac<
/// The type provides de-/serialization for these types:
///
/// * `[u8; N]`, Rust 1.51+, not possible using `serde_bytes`
/// * `&[u8; N]`, Rust 1.51+, not possible using `serde_bytes`
/// * `&[u8]`
/// * `Box<[u8; N]>`, Rust 1.51+, not possible using `serde_bytes`
/// * `Box<[u8]>`
/// * `Vec<u8>`
/// * `Cow<'_, [u8]>`
/// * `Cow<'_, [u8; N]>`, Rust 1.51+, not possible using `serde_bytes`
///
/// # Examples
///
Expand All @@ -1359,6 +1361,10 @@ pub struct TimestampNanoSecondsWithFrac<
/// #[serde_as(as = "Bytes")]
/// #[serde(borrow)]
/// cow: Cow<'a, [u8]>,
/// # #[cfg(FALSE)]
/// #[serde_as(as = "Bytes")]
/// #[serde(borrow)]
/// cow_array: Cow<'a, [u8; 15]>,
/// #[serde_as(as = "Bytes")]
/// vec: Vec<u8>,
/// }
Expand All @@ -1368,16 +1374,19 @@ pub struct TimestampNanoSecondsWithFrac<
/// array: b"0123456789ABCDE".clone(),
/// boxed: b"...".to_vec().into_boxed_slice(),
/// cow: Cow::Borrowed(b"FooBar"),
/// # #[cfg(FALSE)]
/// cow_array: Cow::Borrowed(&[42u8; 15]),
/// vec: vec![0x41, 0x61, 0x21],
/// };
/// let expected = r#"(
/// array: "MDEyMzQ1Njc4OUFCQ0RF",
/// boxed: "Li4u",
/// cow: "Rm9vQmFy",
/// cow_array: "KioqKioqKioqKioqKioq",
/// vec: "QWEh",
/// )"#;
/// # drop(expected);
/// # // Create a fake expected value without the array to make the test compile without const generics
/// # // Create a fake expected value that doesn't use const generics
/// # let expected = r#"(
/// # boxed: "Li4u",
/// # cow: "Rm9vQmFy",
Expand All @@ -1391,6 +1400,50 @@ pub struct TimestampNanoSecondsWithFrac<
/// # }
/// ```
///
/// Fully borrowed types can also be used but you'll need a Deserializer that
/// supports Serde's 0-copy deserialization:
///
/// ```
/// # #[cfg(feature = "macros")] {
/// # use serde::{Deserialize, Serialize};
/// # use serde_with::{serde_as, Bytes};
/// # use std::borrow::Cow;
/// #
/// #[serde_as]
/// # #[derive(Debug, PartialEq)]
/// #[derive(Deserialize, Serialize)]
/// struct TestBorrows<'a> {
/// # #[cfg(FALSE)]
/// #[serde_as(as = "Bytes")]
/// #[serde(borrow)]
/// array_buf: &'a [u8; 15],
/// #[serde_as(as = "Bytes")]
/// #[serde(borrow)]
/// buf: &'a [u8],
/// }
///
/// let value = TestBorrows {
/// # #[cfg(FALSE)]
/// array_buf: &[10u8; 15],
/// buf: &[20u8, 21u8, 22u8],
/// };
/// let expected = r#"(
/// array_buf: "CgoKCgoKCgoKCgoKCgoK",
/// buf: "FBUW",
/// )"#;
/// # drop(expected);
/// # // Create a fake expected value that doesn't use const generics
/// # let expected = r#"(
/// # buf: "FBUW",
/// # )"#;
///
/// # let pretty_config = ron::ser::PrettyConfig::new()
/// # .with_new_line("\n".into());
/// assert_eq!(expected, ron::ser::to_string_pretty(&value, pretty_config).unwrap());
/// // RON doesn't support borrowed deserialization of byte arrays
/// # }
/// ```
///
/// ## Alternative to [`BytesOrString`]
///
/// The [`Bytes`] can replace [`BytesOrString`].
Expand Down
19 changes: 19 additions & 0 deletions src/ser/const_arrays.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use super::*;
use std::collections::{BTreeMap, HashMap};

Expand Down Expand Up @@ -52,6 +53,15 @@ impl<'a, const N: usize> SerializeAs<[u8; N]> for Bytes {
}
}

impl<'a, const N: usize> SerializeAs<&[u8; N]> for Bytes {
fn serialize_as<S>(bytes: &&[u8; N], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(*bytes)
}
}

impl<'a, const N: usize> SerializeAs<Box<[u8; N]>> for Bytes {
fn serialize_as<S>(bytes: &Box<[u8; N]>, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -60,3 +70,12 @@ impl<'a, const N: usize> SerializeAs<Box<[u8; N]>> for Bytes {
serializer.serialize_bytes(&**bytes)
}
}

impl<'a, const N: usize> SerializeAs<Cow<'a, [u8; N]>> for Bytes {
fn serialize_as<S>(bytes: &Cow<'a, [u8; N]>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_bytes(bytes.as_ref())
}
}