pub trait BytesTransferExt: Sized { // Turn u32 into u8 fn bytes_transfer(self) -> BytesTransfer where Self: Iterator; } impl> BytesTransferExt for I { // Turn u32 into u8 fn bytes_transfer(self) -> BytesTransfer { BytesTransfer { iter: self, shift: 0, word: 0, } } } pub struct BytesTransfer + Sized> { iter: I, shift: u8, word: u32, } impl + Sized> Iterator for BytesTransfer { type Item = u8; fn next(&mut self) -> Option { if self.shift > 0 { self.shift -= 8; Some((self.word >> self.shift) as u8) } else { self.iter.next() .and_then(|word| { self.shift = 32; self.word = word; self.next() }) } } }