ouroboros icon indicating copy to clipboard operation
ouroboros copied to clipboard

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

Open erikjohnston opened this issue 2 years ago • 2 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!) :slightly_smiling_face:

erikjohnston avatar Dec 05 '23 17:12 erikjohnston

https://github.com/joshua-maros/ouroboros/pull/105 is a rough potential implementation.

erikjohnston avatar Dec 08 '23 15:12 erikjohnston