use std::sync::mpsc; trait Printer { fn print(&mut self, val: M) where Self: Prints; } struct SimplePrinter {} impl Printer for SimplePrinter { fn print(&mut self, val: M) where Self: Prints, { self.print(val); } } trait Prints { fn print(&self, val: U); } impl Prints for SimplePrinter { fn print(&self, val: u8) { println!("u8: {0}", val); } } impl Prints for SimplePrinter { fn print(&self, val: bool) { if val { println!("bool: true"); } else { println!("bool: false"); } } } /// Need `Message` so that I can store `Box` as the type /// in the PrintManager::queue trait Message { type Underlying; } impl Message for u8 { type Underlying = u8; } impl Message for bool { type Underlying = bool; } struct PrintManager where P: Printer, P: Prints, M: Message::Underlying, { printer: P, queue: mpsc::Receiver>, writer: mpsc::Sender>, } impl

PrintManager

where P: Printer { pub fn do_print(&mut self) { match self.queue.try_recv() { Ok(msg) => self.printer.print(msg), _ => (), } } fn new (p: P) -> Self { let (tx, rx) = mpsc::channel::>(); PrintManager { printer: p, queue: rx, writer: tx, } } fn submit_job(&mut self, msg: M) where P: Prints, M: Message, { self.writer.send(Box::new(msg)).unwrap(); } } fn main() { let pm = PrintManager::new(SimplePrinter{}); pm.submit_job(1u8); pm.submit_job(false); pm.do_print(); pm.do_print(); }