I want to do basically this:

struct MyStruct < T> {
    data: T
}

impl < T> for MyStruct < T> {
    fn foo() {
        println!("Generic")
    }
}

impl for MyStruct < u32> {
    fn foo() {
        println!("u32")
    }
}

I have tried doing

impl < T: !u32> for MyStruct < T> {
    ...
}

But it doesn’t seem to work. I’ve also tried various things with traits but none of them seem to work. Is this even possible?

EDIT: Fixed formatting

  • @edinbruh
    link
    710 months ago

    What you are looking for is called specialization.

    It is currently unstable and incomplete, which means you can activate it by adding the macros #![feature(specialization)] and #![allow(incomplete_features)] at the beginning of the file. But you have no guarantee that it will work the same way on the next version of rust.

    • @calcopiritus@lemmy.worldOP
      link
      fedilink
      310 months ago

      Yeah, that seems like it would work! Unfortunately I can’t use unstable features. I’ll keep it in mind for other projects though.