jpernst / rental

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

Could covariant rentals implement Deref where Deref::Target is the suffix field? #34

Closed nerdrew closed 5 years ago

nerdrew commented 5 years ago

Apologies if this is a silly question. I have rentals like this:

struct Foo<'a> {
    pub some_field: &'a str,
}

rental! {
    mod mod_FooOwned {
        use super::*;

        #[rental(covariant, debug)]
        pub struct FooOwned {
            buf: Vec<u8>,
            proto: Foo<'buf>,
        }
    }
}
use self::mod_FooOwned::FooOwned

fn run(foo: &FooOwned) {
    let foo = foo.suffix();
    // use foo here 
}

Is there a way to make that more seamless? Would a Deref impl do that (assuming such an impl was possible)?

jpernst commented 5 years ago

I believe this is conceptually sound, but non-trivial to implement. I haven't used rust in over a year and I'm no longer comfortable making significant changes, so rental is in a critical-fix only mode. Rental's code is pretty gnarly so I'd be hesitant to even accept a PR for this feature. My recommendation is to wrap the rental struct in a custom struct for your deref impl.