error[E0495]: cannot infer an appropriate lifetime

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 (

How is Connect defined and how is parse_connect defined?

The problem is that parse desugars to

pub fn parse<'a>(input: &'a [u8]) -> IResult<&'a [u8], Command<'a>>

Which may not be what you want. It means that the command must live for as long as your input and that isn’t allowed by the api of Decoder, which says that rhe output’s lifetime is independent of the input lifetime.

Hello again!

i still dont understand, what to do.

now i have small, reproducable piece of code:

trait Decoder {
    type Item;
    type Error;

    fn decode(&mut self, src: &mut [u8]) -> Result<Option<Self::Item>, Self::Error>;
}

fn decode(input: &[u8]) -> &[u8] {
    &[0, 2, 3]
}


struct IterImpl<'a> {
    _marker: &'a bool,
}

impl IterImpl<'_> {
    fn new() -> Self {
        IterImpl {
            _marker: &true,
        }
    }
}

impl<'a> Decoder for IterImpl<'a> {
    type Item = &'a [u8];
    type Error = u8;

    fn decode(&mut self, src: &mut [u8]) -> Result<Option<Self::Item>, Self::Error> {
        Ok(Some(decode(src)))
    }
}


#[cfg(test)]
mod test {
    use crate::IterImpl;

    #[test]
    fn test_ite() {
        let raw = b"1234567890";

        let _i = IterImpl::new();
    }
}

which raize

error[E0495]: cannot infer an appropriate lifetime for lifetime parameter in function call due to conflicting requirements

ive got it!

i just need introduce a new lifetime

fn decode<'a, 'b>(input: &[u8]) -> &'b [u8] {
    &[0, 2, 3]
}

and it works.

anyway, what to read about lifetimes?

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.