Hi all,
Maybe I'm misunderstanding things on traits, but I can't solve this issue. Because enum variants are not considered as a type, I used the trick with traits to restrict the list of admissible types on a generic struct:
pub trait TraitA {}
impl TraitA for usize {}
impl TraitA for Vec<usize> {}
Because my push
fn is specialized for usize
and Vec<usize>
, does any trait impl using this fn have to be specialized also ? In the following example, the code impl for Clone
is the same which is awful. Is there a clue here?
use std::collections::HashMap;
pub trait TraitA {}
impl TraitA for usize {}
impl TraitA for Vec<usize> {}
pub struct S<T, I: TraitA> {
pub list: Vec<T>,
pub hmap: HashMap<String, I>,
}
impl<T, I> S<T, I>
where
I: TraitA,
{
pub fn new() -> S<T, I> {
S {
list: Vec::new(),
hmap: HashMap::new(),
}
}
}
impl<T> S<T, usize> {
pub fn push(&mut self, e: T) {
self.list.push(e);
self.hmap.insert(String::from("scalar"), 0);
}
}
impl<T> S<T, Vec<usize>> {
pub fn push(&mut self, e: T) {
self.list.push(e);
self.hmap.insert(String::from("vector"), vec![0]);
}
}
// Doesn't compile
// impl<T: Clone, I> Clone for S<T, I>
// where
// I: TraitA,
// {
// fn clone(&self) -> Self {
// let mut cloned = S::<T, I>::new();
// for e in &self.list {
// cloned.push(e.clone());
// }
// cloned
// }
// }
impl<T: Clone> Clone for S<T, usize> {
fn clone(&self) -> Self {
let mut cloned = S::<T, usize>::new();
for e in &self.list {
cloned.push(e.clone());
}
cloned
}
}
impl<T: Clone> Clone for S<T, Vec<usize>> {
fn clone(&self) -> Self {
let mut cloned = S::<T, Vec<usize>>::new();
for e in &self.list {
cloned.push(e.clone());
}
cloned
}
}
Thanks a lot for your hints.