You cannot use the HStream type directly; he does not represent anything. It is used only for constructing types of derived pointers such as &HStream and Box<HStream> .
The simplest solution would be to have a LinkedList of Stream<Box<HStream>> .
fn main() { let mut list = Arc::new(Mutex::new(LinkedList::<Stream<Box<HStream>>>::new())); }
Then we just need to implement HStream for Box<HStream> .
impl<'a> HRecv for Box<HStream + 'a> {} impl<'a> HSend for Box<HStream + 'a> {} impl<'a> AsRawFd for Box<HStream + 'a> { fn as_raw_fd(&self) -> RawFd { (&**self).as_raw_fd() } } impl<'a> HStream for Box<HStream + 'a> {}
Please note that this is not a sign of ... Clone .
Clone not object-safe, which means that it is impossible to create feature object types for this characteristic, for example &Clone or Box<Clone> . Clone not object-safe because its Clone method returns Self , which represents a particular type of developer. If you used this method through an attribute object, the compiler would not be able to know the type of result in advance (it could be any of the Clone constructors!).
Since HStream is a subtone of Clone , HStream also not object-safe. The consequence of this is that we cannot implement Clone , and types of type Box<HStream> are not legal to use.
However, we can get around this by creating our own, object-safe attribute. We can even automatically implement it for types that implement the standard Clone trait.
pub trait BoxedHStreamClone { fn boxed_clone(&self) -> Box<HStream>; } // Implementation for all types that implement HStream and Clone and don't hold any borrows impl<T: HStream + Clone + 'static> BoxedHStreamClone for T { fn boxed_clone(&self) -> Box<HStream> { Box::new(self.clone()) as Box<HStream> } } // Implementation for Box<HStream + 'a>, which cannot implement Clone impl<'a> BoxedHStreamClone for Box<HStream + 'a> { fn boxed_clone(&self) -> Box<HStream> { Box::new((&**self).boxed_clone()) as Box<HStream> } }
Replace the Clone binding on the HStream with BoxedHStreamClone and you will be fine!
pub trait HStream: HRecv + HSend + AsRawFd + BoxedHStreamClone {}
Here's the last code:
use std::sync::{Arc, Mutex}; use std::collections::LinkedList; use std::os::unix::io::{RawFd, AsRawFd}; pub trait BoxedHStreamClone { fn boxed_clone(&self) -> Box<HStream>; } impl<T: HStream + Clone + 'static> BoxedHStreamClone for T { fn boxed_clone(&self) -> Box<HStream> { Box::new(self.clone()) as Box<HStream> } } pub trait HRecv {} pub trait HSend {} pub trait HStream: HRecv + HSend + AsRawFd + BoxedHStreamClone {} pub struct Stream<T: HStream> { pub inner: T } pub type StreamList = Arc<Mutex<LinkedList<Stream<Box<HStream>>>>>; impl<'a> HRecv for Box<HStream + 'a> {} impl<'a> HSend for Box<HStream + 'a> {} impl<'a> AsRawFd for Box<HStream + 'a> { fn as_raw_fd(&self) -> RawFd { (&**self).as_raw_fd() } } impl<'a> BoxedHStreamClone for Box<HStream + 'a> { fn boxed_clone(&self) -> Box<HStream> { Box::new((&**self).boxed_clone()) as Box<HStream> } } impl<'a> HStream for Box<HStream + 'a> {} fn main() { let mut list = Arc::new(Mutex::new(LinkedList::<Stream<Box<HStream>>>::new())); }