Greeting,
What i do?
I try to implement some protocol using tokio codec library and nom library.
Trait, that i whant to implement looks like:
pub trait Decoder {
type Item;
type Error: From<io::Error>;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error>;
}
What i wrote to implement this trait:
pub(crate) struct Protocol<'a> {
_marker: &'a bool
}
impl<'a> Protocol<'a> {
pub fn new() -> Self {
Protocol { _marker: &true }
}
}
impl<'a> Decoder for Protocol<'a> {
type Item = parsers::Command<'a>;
type Error = parsers::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let r = parsers::parse(src);
match r {
Ok((remain, command)) => {
let to_advance = src.len() - remain.len();
src.advance(to_advance);
Ok(Some(command))
}
Err(e) => { Err(parsers::Error::RemoveMe) }
}
}
}
Where Command is:
#[derive(Debug, Clone)]
pub enum Command<'a> {
Connect(Connect<'a>),
}
And parsers::parse
pub fn parse(input: &[u8]) -> IResult<&[u8], Command> {
let r = switch!(input, alt!(str_sp_eat | str_crlf_eat),
"CONNECT" => call!(parse_connect)
);
r
}
What i get:
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements
--> src/codec.rs:32:32
|
32 | let r = parsers::parse(src);
| ^^^
|
note: first, the lifetime cannot outlive the anonymous lifetime #2 defined on the method body at 31:5...
--> src/codec.rs:31:5
|
31 | / fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
32 | | let r = parsers::parse(src);
33 | |
34 | | match r {
... |
45 | | }
46 | | }
| |_____^
note: ...so that reference does not outlive borrowed content
--> src/codec.rs:32:32
|
32 | let r = parsers::parse(src);
| ^^^
note: but, the lifetime must be valid for the lifetime 'a as defined on the impl at 27:6...
--> src/codec.rs:27:6
|
27 | impl<'a> Decoder for Protocol<'a> {
| ^^
= note: ...so that the types are compatible:
expected tokio_codec::Decoder
found tokio_codec::Decoder
error: aborting due to previous error
Help me plz, i dont know what to do (