someguynamedjosh / ouroboros

Easy self-referential struct generation for Rust.
Apache License 2.0
524 stars 33 forks source link

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

Open erikjohnston opened 9 months ago

erikjohnston commented 9 months ago

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 commented 9 months ago

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