Two blanket implementations for different classes of objects

Thank you for the idea.
I made slightly different implementation based on it:
playground

The usage is as follows:

// simple types
impl MyMainTrait for i32 {
    type BlanketType = SimpleType<Self>;
}
impl MyMainTrait for &str {
    type BlanketType = SimpleType<Self>;
}

// struct types
struct Foo {
    field: i32,
}

impl MyMainTrait for Foo {
    type BlanketType = StructType<Foo>;
}

impl StructTrait for Foo {
    const FIELDS: &'static [&'static str] = &["field"];
    
    fn do_something_to_field(&self, field: &str) {
        match field {
            "field" => self.field.do_something(),
            _ => panic!("unknown field"),
        }
    }
}

// custom types with their own implementation
struct SomethingNotUsingBlanket {
    field: i32
}

impl MyMainTrait for SomethingNotUsingBlanket {
    type BlanketType = Self;
}

impl BlanketMainTrait for SomethingNotUsingBlanket {
    type Target = Self;
    
    fn do_something(t: &Self) {
        println!("Something special: {}", t.field);
    }
}
1 Like