Hello,
I am new to Rust. and I am implementing something about Diameter Protocol.
I got a lifetime mismatch error.
I defined a trait as below:
trait Avp where Self:Sized {
type V;
fn get_data(&self) -> Self::V;
fn get_header(&self) -> &AvpHeader;
fn new<'a>(header:AvpHeader,data:&'a [u8]) -> Option;
}
I define a struct AvpOctetString with a borrowed value of a slice of u8:
pub struct AvpOctetString<'a> {
header: AvpHeader,
payload: &'a [u8],
}
When I try implementing this trait for this struct, I can't compile:
impl<'a> Avp for AvpOctetString<'a> {
type V = &'a [u8];
fn get_data(&self) -> &'a [u8] {
self.payload
}
fn get_header(&self) -> &AvpHeader {
&self.header
}
fn new(header:AvpHeader, data:&'a [u8]) -> Option<AvpOctetString> {
let length = header.get_length();
Some(AvpOctetString{
header,
payload:data[0..length],
})
}
}
I got compile error like this:
error[E0308]: method not compatible with trait
--> src/message/avp.rs:27:5
|
27 | / fn new(header:AvpHeader, data:&'a [u8]) -> Option {
28 | | let length = header.get_length();
29 | | Some(AvpOctetString{
30 | | header,
31 | | payload:data[0..length],
32 | | })
33 | | }
| |_____^ lifetime mismatch
|
= note: expected type fn(message::avp_header::AvpHeader, &'a [u8]) -> std::option::Option<message::avp::AvpOctetString<'a>>
found type fn(message::avp_header::AvpHeader, &'a [u8]) -> std::option::Option<message::avp::AvpOctetString<'a>>
My intention is to avoid data copying, so that AvpOctetString.
Could you please help me?
Thanks,
Best regards,