How to implement a trait for n-th reference of a type?

How to implement a trait for n-th reference of a type? Preferable without without duplication of impl trait neither manually nor with a macros.

fn main() {
    let u32 = 10;
    println!("{}", Data(&u32).act1());
    println!("{}", Data(&&u32).act1());
    println!("{}", Data(&&&u32).act1());
    println!("{}", Data(&&&&u32).act1());
}

//

pub struct Data<'a, T>(&'a T);

//

trait Trait0 {}
impl Trait0 for u32 {}
impl Trait0 for &u32 {}
// ... ?

//

trait Trait1 {
    fn act1(self) -> i32;
}

//

impl<'a, T> Trait1 for Data<'a, T>
where
    T: Trait0,
{
    fn act1(self) -> i32 {
        13
    }
}

Playground

This is one way to do it

impl <T: Trait0> Trait0 for &T {}
7 Likes

Simple as everything genius. Thank you, @drewkett :heart:

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.