1

I am doing some USB work and my design has a generic buffer type with trait bounds AsRef<[u8]> and AsMut<[u8]>. I am finding that these trait bounds "infect" all higher layers and I have to repeatedly specify them. Is there a way to tidy this up? Example:

pub struct Transfer<B: AsRef<[u8]> + AsMut<[u8]>> {
    pub buf: B,
    // ...
}

impl<B: AsRef<[u8]> + AsMut<[u8]>> Transfer<B> {
    // ...
}

pub struct Device<B: AsRef<[u8]> + AsMut<[u8]>> {
    transfers: Vec<Option<Transfer<B>>>,
    // ...
}

impl<B: AsRef<[u8]> + AsMut<[u8]>> Device<B> {
    // ...
}

impl<B: AsRef<[u8]> + AsMut<[u8]>> Evented for Device<B> {
    // ...
}
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
goertzenator
  • 1,960
  • 18
  • 28
  • Duplicate of http://stackoverflow.com/q/26983355/155423 or http://stackoverflow.com/q/26070559/155423. Use `trait Buffer: AsRef<[u8]> + AsMut<[u8]> {} impl Buffer for T where T: AsRef<[u8]> + AsMut<[u8]> {}`. – Shepmaster May 03 '16 at 15:17
  • The answer to that question definitely applies to my question (thanks!), but I feel there may be additional answers to my question. For example, traits have a means to tuck away additional types and bounds (associated types); is there some similar mechanism for generic structs? – goertzenator May 03 '16 at 15:24
  • 1
    I'm also stuck with this. I wrote about an idea here https://internals.rust-lang.org/t/rfc-idea-associated-type-parameters/3361 – malbarbo May 03 '16 at 17:41

0 Answers0