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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
use std::io;
use super::*;
/// Changes the cookie type without introducing any buffering.
///
/// If you have a `b: BufferedReader<B>` but need a `c:
/// BufferedReader<C>`, then one way to do that is to use `let c =
/// Generic::with_cookie(b, _)`, but that introduces buffering. This
/// `Adapter` also changes cookie types, but does no buffering of its
/// own.
pub struct Adapter<T: BufferedReader<B>, B, C> {
reader: T,
_ghostly_cookie: std::marker::PhantomData<B>,
cookie: C,
}
impl<T: BufferedReader<B>, B, C> fmt::Display for Adapter<T, B, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Adapter")
}
}
impl<T: BufferedReader<B>, B, C> fmt::Debug for Adapter<T, B, C> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Adapter")
.field("reader", &self.reader)
.finish()
}
}
impl<T: BufferedReader<B>, B> Adapter<T, B, ()> {
/// Instantiates a new adapter.
///
/// `reader` is the source to wrap.
pub fn new(reader: T) -> Self {
Self::with_cookie(reader, ())
}
}
impl<T: BufferedReader<B>, B, C> Adapter<T, B, C> {
/// Like `new()`, but sets a cookie.
///
/// The cookie can be retrieved using the `cookie_ref` and
/// `cookie_mut` methods, and set using the `cookie_set` method.
pub fn with_cookie(reader: T, cookie: C)
-> Adapter<T, B, C> {
Adapter {
reader,
_ghostly_cookie: Default::default(),
cookie,
}
}
}
impl<T: BufferedReader<B>, B, C> io::Read for Adapter<T, B, C> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
self.reader.read(buf)
}
}
impl<T: BufferedReader<B>, B, C> BufferedReader<C> for Adapter<T, B, C> {
fn buffer(&self) -> &[u8] {
self.reader.buffer()
}
fn data(&mut self, amount: usize) -> Result<&[u8], io::Error> {
self.reader.data(amount)
}
fn consume(&mut self, amount: usize) -> &[u8] {
self.reader.consume(amount)
}
fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> {
self.reader.data_consume(amount)
}
fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> {
self.reader.data_consume_hard(amount)
}
fn consummated(&mut self) -> bool {
self.reader.consummated()
}
fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> {
None
}
fn get_ref(&self) -> Option<&dyn BufferedReader<C>> {
None
}
fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>>
where Self: 'b {
None
}
fn cookie_set(&mut self, cookie: C) -> C {
std::mem::replace(&mut self.cookie, cookie)
}
fn cookie_ref(&self) -> &C {
&self.cookie
}
fn cookie_mut(&mut self) -> &mut C {
&mut self.cookie
}
}
|