serde-rs / bytes

Wrapper types to enable optimized handling of &[u8] and Vec<u8>
Apache License 2.0
306 stars 37 forks source link

Cannot deserialise into &[u8] #9

Closed misos1 closed 5 years ago

misos1 commented 6 years ago

When I try to use:

#[derive(Serialize, Deserialize, Debug)]
struct Test<'a>
{
    #[serde(with = "serde_bytes")]
    a: &'a [u8]
}

I am getting compile errors:

135 | #[derive(Serialize, Deserialize, Debug)]
    |                     ^^^^^^^^^^^ the trait `std::convert::From<std::vec::Vec<u8>>` is not implemented for `&[u8]`
    |
    = help: the following implementations were found:
              <&'a [u8] as std::convert::From<serde_bytes::Bytes<'a>>>
    = note: required by `serde_bytes::deserialize`

When I try:

#[derive(Serialize, Deserialize, Debug)]
struct Test<'a>
{
    a: serde_bytes::Bytes<'a>
}

Then:

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'de` due to conflicting requirements
   --> main.rs:135:21
    |
135 | #[derive(Serialize, Deserialize, Debug)]

It would be more efficient to support deserialisation into &[u8] to avoid copies similarly as I can deserialise into &str. Or I am missing something?

1tgr commented 6 years ago

I have this workaround for deserializers that support visit_borrowed_bytes, ie formats where the bytes can be sourced directly from the data:

use std::fmt;
use std::marker::PhantomData;

use serde::de::{Deserializer, Error, Visitor};
use serde::ser::Serializer;

struct BorrowedBytesVisitor<T>(PhantomData<T>);

impl<'de, T> Visitor<'de> for BorrowedBytesVisitor<T> where &'de [u8]: Into<T> {
    type Value = T;

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

    #[inline]
    fn visit_borrowed_bytes<E>(self, v: &'de [u8]) -> Result<T, E>
        where E: Error
    {
        Ok(v.into())
    }
}

pub fn serialize<S, T>(bytes: &T, serializer: S) -> Result<S::Ok, S::Error>
    where S: Serializer, T: AsRef<[u8]>
{
    serializer.serialize_bytes(bytes.as_ref())
}

pub fn deserialize<'de, T, D>(deserializer: D) -> Result<T, D::Error>
    where D: Deserializer<'de>, &'de [u8]: Into<T>
{
    Ok(deserializer.deserialize_bytes(BorrowedBytesVisitor(PhantomData))?.into())
}

In the general case I guess serde_bytes has to go via Vec<u8> to support formats like JSON where bytes must be parsed from a string or similar in the input data.

dtolnay commented 5 years ago

Fixed in 0.11.0 -- both of the following should now work:

#[derive(Serialize, Deserialize)]
struct TestSlice<'a> {
    #[serde(with = "serde_bytes")]
    a: &'a [u8]
}
#[derive(Serialize, Deserialize)]
struct TestBytes<'a> {
    #[serde(borrow)]
    a: &'a Bytes,
}