Why is using PhantomData valid in this case?

⚓ Rust    📅 2026-04-24    👤 surdeus    👁️ 1      

surdeus

Hi everyone, I'm new to Rust, but I don't understand why PhantomData helps the following code compile. However, when I directly impl F, even though I've already set a constraint for P, it doesn't compile successfully?

pub struct PhantomSystem<F, P>(F, PhantomData<P>);
pub trait TSystemParam
{
    fn new() -> Self;
}
//impl<F, P> TSystem for F // ❌ ERROR
impl<F, P> TSystem for PhantomSystem<F, P> // ✅ OK !?
where
    F: Fn(P),
    P: TSystemParam + 'static,
{
    fn run(self) { (self.0)(P::new()); }
    //fn run(self) { (self)(P::new()); }
}

6 posts - 3 participants

Read full topic

🏷️ Rust_feed