Hello everyone,
I currently try to port some code from go to rust and I am running into something I don't quite understand. I want to have data structure which contains a slice of other structures and serialize them with serde (cbor to be exact). Unfortunately the compiler is not convinced that the Serialize trait for type B is really implemented. Here is the simplest example I could come up with which shows my problem:
extern crate serde;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct A<'a> {
pub b_s: &'a [B<'a>],
}
#[derive(Debug, Serialize, Deserialize)]
pub struct B<'a> {
pub value: &'a [u8],
}
Errors:
Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `&'a [B<'a>]: serde::Deserialize<'_>` is not satisfied
--> src/lib.rs:8:3
|
8 | pub b_s: &'a [B<'a>],
| ^^^ the trait `serde::Deserialize<'_>` is not implemented for `&'a [B<'a>]`
|
= help: the following implementations were found:
<&'a [u8] as serde::Deserialize<'de>>
<[T; 0] as serde::Deserialize<'de>>
<[T; 10] as serde::Deserialize<'de>>
<[T; 11] as serde::Deserialize<'de>>
and 30 others
= note: required by `serde::de::SeqAccess::next_element`
error[E0277]: the trait bound `&'a [B<'a>]: serde::Deserialize<'_>` is not satisfied
--> src/lib.rs:8:3
|
8 | pub b_s: &'a [B<'a>],
| ^^^ the trait `serde::Deserialize<'_>` is not implemented for `&'a [B<'a>]`
|
= help: the following implementations were found:
<&'a [u8] as serde::Deserialize<'de>>
<[T; 0] as serde::Deserialize<'de>>
<[T; 10] as serde::Deserialize<'de>>
<[T; 11] as serde::Deserialize<'de>>
and 30 others
= note: required by `serde::de::MapAccess::next_value`
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0277`.
error: Could not compile `playground`.
To learn more, run the command again with --verbose.
At least for me, as a beginner, this compiler error message is not really helpful. I guess that &'a [B<'a>]
is another type than B<'a>
, but I also don't know how to implement Serialize
for &'a [B<'a>]
.
Any help is appreciated. In the long run this code also need to work in no_std environments, but for now just getting it to work would also help so much.
Thanks in advance.