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