jpernst / rental

Rust macro to generate self-referential structs
Apache License 2.0
211 stars 11 forks source link

How to transform one rental into another #42

Closed dignifiedquire closed 4 years ago

dignifiedquire commented 4 years ago

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.

jpernst commented 4 years ago

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.

dignifiedquire commented 4 years ago

Thank you, that works for now.