How to satisfy const trait bounds

Hi,

I'm trying to create a generic implementation of something that also use some generic functionality.

pub trait FooValid {}
impl FooValid for Foo<42> {}

pub struct Foo<const N: usize>(core::marker::PhantomData<()>)
where
    Foo<N>: FooValid;

impl<const N: usize> Foo<N>
where
    Foo<N>: FooValid,
{
    pub fn do_foo_stuff() {
        println!("doing foo stuff");
    }
}


pub struct Bar<const N: usize>(core::marker::PhantomData<()>);

impl<const N: usize> Bar<N>
{
    pub fn do_bar_stuff() {
        Foo::<N>::do_foo_stuff();
    }
}

It fails with

error[E0599]: the function or associated item `do_foo_stuff` exists for struct `Foo<N>`, but its trait bounds were not satisfied

I tried specifying Bar the same way. But it does not help:

pub struct Bar<const N: usize>(core::marker::PhantomData<()>)
where
    Bar<N>: FooValid;

Any hints would be appreciated.

Thank you

But you are calling Foo, not Bar

    pub fn do_bar_stuff() where Foo<N>: FooValid {
        Foo::<N>::do_foo_stuff();
    }

Work fine for me.

1 Like

thank you. Thank you!

In hindsight this makes total sense, but I couldn't come up with it. I spend hours trying to figure that out.