Hi,
I have a trait DeserializeMessage
, which will process Payload
and generate some outputs. I want to implement this trait for both owned struct (struct TestOwned
) and struct with reference (struct Test<'a>
).
#[derive(Debug, Clone)]
pub struct Payload {
pub metadata: Metadata,
pub data: Vec<u8>,
}
pub trait DeserializeMessage {
type Output: Sized;
fn deserialize_message(payload: &Payload) -> Self::Output;
}
struct Test<'a> {
data: &'a [u8],
}
impl<'a> DeserializeMessage for Test<'a> {
type Output = &'a [u8];
fn deserialize_message(payload: &Payload) -> Self::Output {
&payload.data
}
}
struct TestOwned {
data: Vec<u8>,
}
impl DeserializeMessage for TestOwned {
type Output = Vec<u8>;
fn deserialize_message(payload: &Payload) -> Self::Output {
payload.data.to_vec()
}
}
The owned version compiles, while the referential version failed to compile. Anyone knows how to return a reference in this trait implementation for struct Test<'a>
?
Thanks
Here's the full code