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

Allow custom dyn traits with non-static lifetime #5

Open
srid opened this issue Sep 14, 2023 · 1 comment
Open

Allow custom dyn traits with non-static lifetime #5

srid opened this issue Sep 14, 2023 · 1 comment

Comments

@srid
Copy link

srid commented Sep 14, 2023

Instead of dyn Any I'd like to iterate over dyn MyTrait (dyn Checkable in the example below) with the assumption that all fields in the struct implement that trait (else, there should be a compile error).

Example

pub struct NixHealth {
    pub max_jobs: MaxJobs,
    pub caches: Caches,
    pub flake_enabled: FlakeEnabled,
    pub min_nix_version: MinNixVersion,
    pub trusted_users: TrustedUsers,
}

Manual implementration

impl<'a> IntoIterator for &'a NixHealth {
    type Item = &'a dyn Checkable;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        let items: Vec<Self::Item> = vec![
            &self.min_nix_version,
            &self.flake_enabled,
            &self.max_jobs,
            &self.caches,
            &self.trusted_users,
        ];
        items.into_iter()
    }
}

Code in context: https://github.com/juspay/nix-browser/blob/913c505d501efd15beff7388404d9a7f4e4e4c58/crates/nix_health/src/lib.rs#L24-L48

@kaimast
Copy link

kaimast commented Apr 10, 2024

I agree. Would love to be able to use ToString for my specific use case.

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

2 participants