Hi,
this is more of a style question. What I want to achieve is to have a generic definition of a trait and implement it for different types but in some cases the underlying function is exactly the same in implementation and I see no reason to repeat my code. So my question is: Is there a way to maximize code reuse ? Example of what I have
pub type A<T> = Vec<T>;
pub trait AA <T> {
fn init(i: T)-> A<T>;
}
impl AA<i32> for A<i32>{
fn init(i: i32)-> A<i32>{
let mut aa : A<i32> = Vec::new();
for x in 1..i as usize {
aa.push(x as i32);
}
aa
}
}
impl AA<i64> for A<i64>{
fn init(i: i64)-> A<i64>{
let mut aa : A<i64> = Vec::new();
for x in 1..i as usize {
aa.push(x as i64);
}
aa
}
}
fn main (){
let x = A::init(10 as i64);
println!("{:?}", x)
}
And how I would like it to look like:
pub type A<T> = Vec<T>;
pub trait AA <T> {
fn init(i: T)-> A<T>;
}
impl AA<T> for A<T>{
fn init(i: T)-> A<T>{
let mut aa : A<T> = Vec::new();
for x in 1..i as usize {
aa.push(x as T);
}
aa
}
}
fn main (){
let x = A::init(10 as i64);
println!("{:?}", x)
}
Is there a way to somehow achieve this ? (keep in mind that if I have a chr = T or a string = T, I need to redefine the function explicitly because then I might not be able to utilize the same computation strategy, however in case i32, i64 to which I wish to limit my T the utilization is exactly the same and here I would like to reuse the code )
Thank you !!