Skip to content

Commit

Permalink
Merge #325
Browse files Browse the repository at this point in the history
325: Bytes const size borrowed arrays r=jonasbb a=matix2267

Currently `Bytes` (added in #277) supports some const size array types (e.g. `[u8; N]` and `Box<[u8; N]>`)
but not others (e.g. `&[u8; N]` and `Cow<'_, [u8; N]>`).

This pull request implements `Bytes` support for `&[u8; N]` and `Cow<'_, [u8; N]>`
This bring parity between dynamic and const sized types.

### Before:

| Sizing  | Owned          | Borrowed      | Cow                   | Box               |
| ------- | -------------- | ------------- | --------------------- | ----------------- |
| Const   | ✅ `[u8; N]`   | ❌ `&[u8; N]` | ❌ `Cow<'_, [u8; N]>` | ✅ `Box<[u8; N]>` |
| Dynamic | ✅ `Vec<u8>`\* | ✅ `&[u8]`    | ✅ `Cow<'_, [u8]>`    | ✅ `Box<[u8]>`    |

### After:

| Sizing  | Owned          | Borrowed      | Cow                   | Box               |
| ------- | -------------- | ------------- | --------------------- | ----------------- |
| Const   | ✅ `[u8; N]`   | ✅ `&[u8; N]` | ✅ `Cow<'_, [u8; N]>` | ✅ `Box<[u8; N]>` |
| Dynamic | ✅ `Vec<u8>`\* | ✅ `&[u8]`    | ✅ `Cow<'_, [u8]>`    | ✅ `Box<[u8]>`    |

I've also added a separate commit with an example of using fully borrowed types, but that example only does serialization
because RON doesn't support 0-copy deserialization for byte arrays (as it needs to decode them from base64).
Because of that I'm not sure if this example is useful so feel free to merge only the main commit.

<sub>\* Technically `[u8]` corresponds to `[u8; N]` but `[u8]` is an un`Sized` type and cannot be
directly used in structs.</sub>

Co-authored-by: matix2267 <matix2267@gmail.com>
  • Loading branch information
bors[bot] and matix2267 authored Jun 14, 2021
2 parents bb1c80a + 3130c38 commit 4f2cbae
Show file tree
Hide file tree
Showing 4 changed files with 218 additions and 1 deletion.
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())
}
}

0 comments on commit 4f2cbae

Please sign in to comment.