Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't refer to 'this in generic bounds? #50

Open
tornewuff opened this issue Nov 28, 2021 · 6 comments
Open

Can't refer to 'this in generic bounds? #50

tornewuff opened this issue Nov 28, 2021 · 6 comments

Comments

@tornewuff
Copy link

use std::io::Read;
struct Foo<R: Read>(R);
struct Bar<'a, R: Read + 'a>(&'a Foo<R>);

#[ouroboros::self_referencing]
struct Test<R: Read + 'this> {
    foo: Foo<R>,
    #[borrows(foo)]
    #[covariant]
    bar: Bar<'this, R>,
}

I'm trying to define a selfreferencing structure where the referencing type needs a lifetime on its generic parameter, but adding 'this doesn't work - it reports it as an undeclared lifetime.

Am I going about this wrong? Is this possible to support?

@someguynamedjosh
Copy link
Owner

someguynamedjosh commented Nov 28, 2021 via email

@JohnScience
Copy link

JohnScience commented Jun 29, 2022

I needed that too to store an iterator. I've made a workaround with a specific type

pub(super) type EnumeratedDepsLinsIter<'a> = Filter<
    Map<Enumerate<Lines<'a>>, fn((usize, &str)) -> (usize, &str)>,
    fn(&(usize, &str)) -> bool
>;

and something like this

        // The closures are coerced to function pointers so that their types could be named.
        //
        // Quting the reference, "A closure expression produces a closure value with a unique,
        // anonymous type that cannot be written out.".
        //
        // Source: https://doc.rust-lang.org/reference/types/closure.html
        // 
        // The type of the whole iterator is needed so that ouroboros crate could handle the
        // field of the type of the iterator.
        let trim_lines: fn((usize, &str)) -> (usize, &str) = |(i, line)| (i, line.trim());
        let is_trimmed_dep_line: fn(&(usize, &str)) -> bool = |(_i, line)| line.starts_with(":dep");

but it's ugly.

@JCBurnside
Copy link

damn I was looking for this exactly. I needed to store a context and a codegen struct I am making for llvm. since if the context gets dropped it would create an issue for generated modules being invalidated. guess i'll have to look else where.

@koutheir
Copy link

For anyone interested, I switched to the self_cell crate in order to work around this issue.

@fzyzcjy
Copy link

fzyzcjy commented Jun 15, 2024

Hi, is there any updates? Thanks!

@fzyzcjy
Copy link

fzyzcjy commented Jun 15, 2024

The primary
difficulty is that the generic parameter is used in places that aren't
self-referencing, such as the Heads struct returned by .into_heads().

Then is it possible that, when using Bar<'this>, the Heads features are disabled? In my scenario (hoping to use this in https://github.com/fzyzcjy/flutter_rust_bridge), I only need to borrow the bar, and no need for functions like into_heads.

EDIT: I realize I do not need this in generics bounds. Thanks for the library and it works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants