use common_traits::*; use core::sync::atomic::{AtomicUsize, Ordering}; pub struct MyVec { data: Vec, } pub trait Get { type Item: Copy; fn get(&self, index: usize) -> Self::Item; } impl> Get for MyVec { type Item = T::Item; fn get(&self, index: usize) -> Self::Item { GetHelper::get(self, index) } } pub trait GetHelper: Sized { type Item: Copy; fn get(data: &MyVec, index: usize) -> Self::Item; } impl GetHelper for T where T::NonAtomicType: Copy, { type Item = T::NonAtomicType; fn get(data: &MyVec, index: usize) -> Self::Item { data.data[index].load(Ordering::SeqCst) } } impl GetHelper for T { type Item = T; fn get(data: &MyVec, index: usize) -> Self::Item { data.data[index] } } fn get(v: &V, index: usize) -> V::Item { v.get(index) } fn main() { let v: MyVec = MyVec { data: vec![1, 2, 3], }; let res = get(&v, 0); println!("{:?}", res); let v: MyVec = MyVec { data: vec![ AtomicUsize::new(1), AtomicUsize::new(2), AtomicUsize::new(3), ], }; let res = get(&v, 0); println!("{:?}", res); }