1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use std::io;
use IterReadItem;
macro_rules! impl_byte {
($el_ty:ty, $($deref:tt)*) => {
impl<'a> IterReadItem for $el_ty {
impl_byte!(@inner [$($deref)*] []);
}
impl<'a, E> IterReadItem for Result<$el_ty, E> where io::Error: From<E> {
impl_byte!(@inner [$($deref)*] [?]);
}
};
(@inner [$($deref:tt)*] [$($try:tt)*]) => {
type Buffer = ();
fn read<I: Iterator<Item=Self>>(target: &mut [u8], it: &mut I,
_: &mut ()) -> io::Result<usize> {
let mut len = 0;
for (slot, elt) in target.iter_mut().zip(it) {
*slot = $($deref)* elt $($try)*;
len += 1;
}
Ok(len)
}
}
}
macro_rules! impl_slice_like {
($el_ty:ty, $buf_ty:ty, $conv:ident) => {
impl<'a> IterReadItem for $el_ty {
impl_slice_like!(@inner $buf_ty, $conv, buf:
Some(v) => {
*buf = (v.$conv(), 0);
});
}
impl<'a, E> IterReadItem for Result<$el_ty, E> where io::Error: From<E> {
impl_slice_like!(@inner $buf_ty, $conv, buf:
Some(Ok(v)) => {
*buf = (v.$conv(), 0);
}
Some(Err(err)) => {
*buf = (Default::default(), 0);
return Err(err.into());
});
}
};
(@inner $buf_ty:ty, $conv:ident, $buf:ident : $($match:tt)+) => {
type Buffer = ($buf_ty, usize); fn read<I: Iterator<Item=Self>>(target: &mut [u8], it: &mut I,
$buf: &mut Self::Buffer) -> io::Result<usize> {
while $buf.1 == $buf.0.len() {
match it.next() {
$($match)+
None => {
*$buf = (Default::default(), 0);
return Ok(0);
}
}
}
let mut len = 0;
for (slot, elt) in target.iter_mut().zip(&$buf.0[$buf.1..]) {
*slot = *elt;
len += 1;
}
$buf.1 += len;
Ok(len)
}
}
}
impl_byte!(u8, );
impl_byte!(&'a u8, *);
impl_slice_like!(&'a [u8], &'a [u8], into);
impl_slice_like!(&'a Vec<u8>, &'a [u8], as_slice);
impl_slice_like!(Vec<u8>, Vec<u8>, into);
impl_slice_like!(&'a str, &'a [u8], as_bytes);
impl_slice_like!(&'a String, &'a [u8], as_bytes);
impl_slice_like!(String, Vec<u8>, into_bytes);