someguynamedjosh/ouroboros

Add support for calling methods that take `self` and return the `Heads`

erikjohnston opened this issue · 1 comments

I have a situation where I would like to call a method that takes self on a tail field, and then get the head fields back. I don't think this is possible currently, but would there be interest in adding such an ability? (Or a reason why it might be unsafe?).

An example situation:

struct A;
struct B<'a> {
    a: &'a A
}

impl<'a> B<'a> {
   fn new(&'a A) -> B<'a> { B { a } }

   fn final(self) { ... }
}

#[self_referencing]
struct Wrapper {
    a: A,
    #[borrows(mut a)]
    b: B<'a>,
}

In this case I would like to be able to, given a Wrapper, call B::final and get back a.

I think a possible function signature for the wrapper type would be:

fn into_heads_with_b<R, F>(self, func: F) -> (R, Heads) 
where F: Fn(B<'a>) -> R;

or something?

Thoughts very much welcome (and thanks for this library!) 🙂

#105 is a rough potential implementation.