windowed_infinity/
tee.rs

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//! Code in this module is largely generic enough to be developed into a general-purpose Tee for
//! embedded-io.
//!
//! Going there will take two steps:
//! * Replace the internal write with embedded-io's. This is currently not done as to avoid
//!   implementing the current 0.4 version's embedded-io trait (which would be a public property).
//! * Implement a constructor and an into_inner() -> (W1, W2). These are currently not done because
//!   also here, this would bring a version compatiblity issue.
//!
//!   Instead, there are custom constructors and destructors for very specific supported cases.
//!
//! Once there is a stable embedded-io to use, the Tee can migrate (possibly into a dedicated
//! crate). The old constructors will then provide shims (which may deref into the Tee) that
//! provide the old (now deprecated) finalizers.

use super::MyWrite;

/// A "T" shaped dispatcher for blocking writes. This contains two writers, one of which is
/// infallible to simplify error handling, both of which writes to the Tee are sent to.
pub struct Tee<W1, W2>
// These are preconditions for every actual implemnetation, but we're not listing them as the
// MyWrite trait is private.
// where
//     W1: MyWrite,
//     W2: MyWrite<Error = core::convert::Infallible>,
{
    pub(crate) w1: W1,
    pub(crate) w2: W2,
}

impl<W1, W2> MyWrite for Tee<W1, W2>
where
    W1: MyWrite,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = W1::Error;

    fn write(&mut self, buf: &[u8]) -> Result<(), <W1 as MyWrite>::Error> {
        // unwrap: The W2 error type is uninhabited, but `let Ok(()) = ...;` doesn't work yet
        self.w2.write(buf).unwrap();
        self.w1.write(buf)
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        // unwrap: The W2 error type is uninhabited, but `let Ok(()) = ...;` doesn't work yet
        self.w2.flush().unwrap();
        self.w1.flush()
    }
}

#[cfg(feature = "std")]
impl<W1, W2> std::io::Write for Tee<W1, W2>
where
    // It might be possible to relax this into W1 errors that are Into<std::io::Error>
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
        // unwrap: W1::Error is infallbile
        MyWrite::write(self, buf).unwrap();
        Ok(buf.len())
    }

    fn flush(&mut self) -> std::io::Result<()> {
        self.w2.flush().unwrap();
        self.w1.flush().unwrap();
        Ok(())
    }
}

impl<W1, W2> core::fmt::Write for Tee<W1, W2>
where
    // It might be possible to relax this into W1 errors that are Into<core::fmt::Error>
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        // unwrap: W1::Error is infallbile
        self.write(s.as_bytes()).unwrap();
        Ok(())
    }
}

#[cfg(feature = "serde_cbor")]
impl<W1, W2> serde_cbor::ser::Write for Tee<W1, W2>
where
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    // To be changed to ! once that's stable and implements Into-all
    type Error = serde_cbor::error::Error;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        // unwrap: W1::Error is infallbile
        self.write(buf).unwrap();
        Ok(())
    }
}

#[cfg(feature = "with_minicbor")]
impl<W1, W2> minicbor::encode::Write for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.write(buf)
    }
}

#[cfg(feature = "with_minicbor_0_19")]
impl<W1, W2> minicbor_0_19::encode::Write for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.write(buf)
    }
}

#[cfg(feature = "with_minicbor_0_24")]
impl<W1, W2> minicbor_0_24::encode::Write for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.write(buf)
    }
}

#[cfg(feature = "with_minicbor_0_25")]
impl<W1, W2> minicbor_0_25::encode::Write for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.write(buf)
    }
}

#[cfg(feature = "with_ciborium")]
impl<W1, W2> ciborium_io::Write for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;

    fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
        self.write(buf)
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

#[cfg(feature = "with_embedded_io_0_4")]
impl<W1, W2> embedded_io::Io for Tee<W1, W2>
where
    // Could be any, once MyWrite is replaced with embedded-io (then, Error will become W1::Error).
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    type Error = core::convert::Infallible;
}

#[cfg(feature = "with_embedded_io_0_4")]
impl<W1, W2> embedded_io::blocking::Write for Tee<W1, W2>
where
    W1: MyWrite<Error = core::convert::Infallible>,
    W2: MyWrite<Error = core::convert::Infallible>,
{
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        MyWrite::write(self, buf).map(|()| buf.len())
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        MyWrite::flush(self)
    }
}