How to specify generic parameter of trait in trait bound?

⚓ Rust    📅 2026-04-02    👤 surdeus    👁️ 6      

surdeus

I have a use case like this:

struct S;

trait PasswordHasher<T> {}

impl<H, P> Iterator for S
where
    H: PasswordHasher<P>,
{
    type Item = ();

    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

Which does not compile.
I worked around this, by storing P as PhantomData in S as a workaround, but I'd rather not do this, since imho PhantomData is just noise when reading the code.

I also imagined that maybe this would work

struct S;

trait PasswordHasher<T> {}

impl<H> Iterator for S
where
    for<P> H: PasswordHasher<P>,
{
    type Item = ();

    fn next(&mut self) -> Option<Self::Item> {
        todo!()
    }
}

But HRTBs sadly only work with lifetimes at the moment.

Is there any way to implement this without resorting to PhantomData?

3 posts - 2 participants

Read full topic

🏷️ Rust_feed