When I try to put type paramter bounds on function arguments, if the struct
has a kind of hierarchy structure, the codes became very verbose.
Here is my code:
#[derive(Debug, Clone)]
struct A<T, N> {
func: T,
data: N
}
#[derive(Debug, Clone)]
struct B1<T, N> {
func: T,
data: N
}
#[derive(Debug, Clone)]
struct B2<T, N> {
func: T,
data: N
}
#[derive(Debug, Clone)]
struct C<T, N> {
func: T,
data: N
}
trait Sigs {
fn get(&self);
}
impl<T: From<u8>, N: From<u8>, G: From<u8>, Z: From<u8>> Sigs for C<T, B1<N, A<G, Z>>> {
fn get(&self) {
println!("from B1");
}
}
impl<T: From<u8>, N: From<u8>, G: From<u8>, Z: From<u8>> Sigs for C<T, B2<N, A<G, Z>>> {
fn get(&self) {
println!("from B2");
}
}
Then I can use it like:
let a = A { func: 1i32, data: 1f64 };
let b1 = B1 { func: 2i32, data: a.clone() };
let b2 = B2 { func: 264, data: a.clone() };
let c1 = C { func: 1usize, data: b1.clone() };
let c2 = C { func: 1usize, data: b2.clone() };
c1.get()
from B1
c2.get()
from B2
But as can be seen above, when I implement Sigs for C<T, B>
, the codes goes very long. I don't need to know what it is the type lieing in B
, I just need to identity what the struct
name of B.
Just like this:
impl<T: From<u8>, N: B1> Sigs for C<T, N> {
fn get(&self) {
println!("from B1");
}
}
impl<T: From<u8>, N: B2> Sigs for C<T, N> {
fn get(&self) {
println!("from B2");
}
}
Is there a way I can do that?