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

Add Type::lifetimes to make getting lifetimes out of Type easier #409

Merged

Conversation

QnnOkabayashi
Copy link
Collaborator

Forgive me because it's been a long time since I've looked at this repo- I don't remember how everything works but I wanted to get back into it.

I was looking at #406 and saw this part:

There is a way to get an exhaustive Lifetimes for every struct field: basically something like LifetimeEnv.new_lifetimes_for(&field), producing a list of relevant indices. This list is transitively evaluated.

I thought a good first step for this would be to make getting the lifetimes of a field easy, so I added Type::lifetimes which returns a &[MaybeStatic<TypeLifetime>], essentially just consolidating where lifetimes are stored in the Type enum. This will allow for a higher-level api to use this and SubtypeLifetimeVisitor to achieve what I quoted above with something like this:

let mut lifetimes = vec![];
let mut visitor = SubtypeLifetimeVisitor::new(&struct_.lifetime_env, |lt| lifetimes.push(lt));
for field in &struct_.fields {
    for lifetime in field.ty.lifetimes() { // <-- added this method!
        if let MaybeStatic::NonStatic(lt) = lifetime {
            visitor.visit_subtypes(lt);
        }
    }
}

Today this doesn't compile because MethodLifetime != TypeLifetime, but SubtypeLifetimeVisitor would probably be adjusted once each type declaration has their own LifetimeEnv.

@Manishearth
Copy link
Contributor

My plan is actually to have Field resolve lifetimes, but this helps attain that.

@Manishearth Manishearth merged commit f4188b3 into rust-diplomat:main Jan 17, 2024
6 checks passed
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

Successfully merging this pull request may close these issues.

2 participants