Open doy-materialize opened 1 year ago
I am not sure Ouroboros could/should provide a nice API for this.
As per the Destructors page in the Rustonomicon: (Emphasis added by me)
Note that taking &mut self means that even if you could suppress recursive Drop, Rust will prevent you from e.g. moving fields out of self. For most types, this is totally fine.
For self-referencing types, it is not fine. This is the error you are encountering above.
But there is a simple workaround: Wrap the self-referencing struct in a 'normal' struct, and use the normal borrow_fieldname()
, with_fieldname_mut()
etc. functions in there as desired:
pub struct Foo(FooInner);
#[ouroboros::self_referencing]
struct FooInner {
a: String,
#[borrows(a)]
b: &'this str,
}
impl Drop for Foo {
fn drop(&mut self) {
println!("Drop called with b: {:?}", self.0.borrow_b());
// etc
}
}
impl Foo {
// Use whatever parameters are appropriate to construct the default FooInner here:
pub fn new(...) -> Foo {
let inner = FooInnerBuilder{...}.build();
Foo(inner)
}
}
results in this error: