jpernst/rental

How to transform one rental into another

dignifiedquire opened this issue · 2 comments

I would like to do the following

struct A {
  raw: Vec<u8>,
  parsed: Parsed1<'raw>,
}

struct B {
  raw: Vec<u8>,
  parsed: Parsed2<'raw>,  
}

// create a at some point
let a = create_a();

// something like try_into (can be a custom method) that consumes a and gives me b
let b = a.try_into().unwrap();

I have sth that can transform Parsed1<'a> into Parsed2<'a>, but the problem is that I can not get raw and parsed out of A at the same time.

This is not currently possible with rental as it stands now, but there is a workaround. You could try making your suffix field an enum with states for all the possible types it can be, and impl functions for it that can transition it between variants. This way the actual type of the rental struct doesn't change. There will be slight overhead when matching on the enum, but this is probably the best solution.

Thank you, that works for now.