Debian Codegraph @

src/adapter.rs

raw symbols SCIP
1 use std::io;
2
3 use super::*;
4
5 /// Changes the cookie type without introducing any buffering.
6 ///
7 /// If you have a `b: BufferedReader<B>` but need a `c:
8 /// BufferedReader<C>`, then one way to do that is to use `let c =
9 /// Generic::with_cookie(b, _)`, but that introduces buffering. This
10 /// `Adapter` also changes cookie types, but does no buffering of its
11 /// own.
12 #[derive(Debug)]
13 pub struct Adapter<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> {
14 _ghostly_cookie: std::marker::PhantomData<B>,
15 cookie: C,
16 reader: T,
17 }
18
19 assert_send_and_sync!(Adapter<T, B, C>
20 where T: BufferedReader<B>,
21 B: fmt::Debug,
22 C: fmt::Debug);
23
24 impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> fmt::Display for Adapter<T, B, C> {
25 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
26 f.debug_struct("Adapter").finish()
27 }
28 }
29
30 impl<T: BufferedReader<B>, B: fmt::Debug + Sync + Send> Adapter<T, B, ()> {
31 /// Instantiates a new adapter.
32 ///
33 /// `reader` is the source to wrap.
34 pub fn new(reader: T) -> Self {
35 Self::with_cookie(reader, ())
36 }
37 }
38
39 impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> Adapter<T, B, C> {
40 /// Like [`Self::new`], but sets a cookie.
41 ///
42 /// The cookie can be retrieved using the [`BufferedReader::cookie_ref`] and
43 /// [`BufferedReader::cookie_mut`] methods, and set using the [`BufferedReader::cookie_set`] method.
44 pub fn with_cookie(reader: T, cookie: C)
45 -> Adapter<T, B, C> {
46 Adapter {
47 reader,
48 _ghostly_cookie: Default::default(),
49 cookie,
50 }
51 }
52 }
53
54 impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> io::Read for Adapter<T, B, C> {
55 fn read(&mut self, buf: &mut [u8]) -> Result<usize, io::Error> {
56 self.reader.read(buf)
57 }
58 }
59
60 impl<T: BufferedReader<B>, B: fmt::Debug + Send + Sync, C: fmt::Debug + Sync + Send> BufferedReader<C> for Adapter<T, B, C> {
61 fn buffer(&self) -> &[u8] {
62 self.reader.buffer()
63 }
64
65 fn data(&mut self, amount: usize) -> Result<&[u8], io::Error> {
66 self.reader.data(amount)
67 }
68
69 fn consume(&mut self, amount: usize) -> &[u8] {
70 self.reader.consume(amount)
71 }
72
73 fn data_consume(&mut self, amount: usize) -> Result<&[u8], io::Error> {
74 self.reader.data_consume(amount)
75 }
76
77 fn data_consume_hard(&mut self, amount: usize) -> Result<&[u8], io::Error> {
78 self.reader.data_consume_hard(amount)
79 }
80
81 fn consummated(&mut self) -> bool {
82 self.reader.consummated()
83 }
84
85 fn get_mut(&mut self) -> Option<&mut dyn BufferedReader<C>> {
86 None
87 }
88
89 fn get_ref(&self) -> Option<&dyn BufferedReader<C>> {
90 None
91 }
92
93 fn into_inner<'b>(self: Box<Self>) -> Option<Box<dyn BufferedReader<C> + 'b>>
94 where Self: 'b {
95 None
96 }
97
98 fn cookie_set(&mut self, cookie: C) -> C {
99 std::mem::replace(&mut self.cookie, cookie)
100 }
101
102 fn cookie_ref(&self) -> &C {
103 &self.cookie
104 }
105
106 fn cookie_mut(&mut self) -> &mut C {
107 &mut self.cookie
108 }
109 }