Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait Parsable<'a>
- where
- Self: Sized,
- {
- fn try_parse(data: &'a [u8]) -> Result<Self, ()>;
- }
- trait Containing<'a> {
- fn get_inner(&'a self) -> Layer<'a>;
- }
- struct Layer<'a> {
- typ: u8,
- data: &'a [u8],
- }
- struct Concrete<'a> {
- other: u16,
- payload: &'a [u8],
- }
- impl<'a> Parsable<'a> for Concrete<'a> {
- fn try_parse(data: &'a [u8]) -> Result<Concrete<'a>, ()> {
- Ok(Concrete {
- other: data[0] as u16,
- payload: &data[1..],
- })
- }
- }
- impl<'a> Containing<'a> for Concrete<'a> {
- fn get_inner(&'a self) -> Layer<'a> {
- Layer {
- typ: 65,
- data: self.payload,
- }
- }
- }
- fn main() {
- let data: &[u8] = &[0; 128];
- process(data)
- }
- fn process<'x>(data: &'x [u8]) {
- let mut count = 2;
- let mut outermost: Layer<'x> = Layer { typ: 12, data };
- while count > 0 {
- count -= 1;
- let res_parse: Result<Concrete<'x>, ()> = Concrete::try_parse(outermost.data);
- let concrete: Concrete<'x> = res_parse.expect("unable to parse layer");
- // expect no lifetime error, since the 'x lifetime bound
- // should be fulfilled by data being 'x?
- outermost = concrete.get_inner();
- }
- }
Add Comment
Please, Sign In to add comment