bytes
bytes copied to clipboard
Is there a way to borrow when serialize and own when deserialize?
Cow
is handled in this way in serde
. But serde-bytes
deserializes Cow
by borrowing. As a result, this compiles:
#[derive(Serialize, Deserialize)]
struct A<'a> {
a: Cow<'a, [u8]>,
}
But this does not compile:
#[derive(Serialize, Deserialize)]
struct A<'a> {
#[serde(with = "serde_bytes")]
a: Cow<'a, [u8]>,
}
The error message generated by compiler:
error: lifetime may not live long enough
--> src/main.rs:4:21
|
4 | #[derive(Serialize, Deserialize)]
| ^^^^^^^^^^^
| |
| lifetime `'de` defined here
| associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'de`
5 | struct A<'a> {
| -- lifetime `'a` defined here
|
= help: consider adding the following bound: `'de: 'a`
= note: this error originates in the derive macro `Deserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
Isn't it more reasonable to handle Cow
in a way similar to serde
so that deserializing Cow
does not borrow?