data_stream/
wrappers.rs

1use crate::{FromStream, ToStream, from_stream, to_stream};
2
3use std::io::{Read, Result as IOResult, Write};
4
5impl<S, T: ToStream<S>> ToStream<S> for Box<T> {
6    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
7        (**self).to_stream(stream)
8    }
9}
10
11impl<S, T: FromStream<S>> FromStream<S> for Box<T> {
12    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
13        Ok(Self::new(from_stream(stream)?))
14    }
15}
16
17impl<S, T: ToStream<S>> ToStream<S> for Option<T> {
18    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
19        match self {
20            None => to_stream::<S, _, _>(&false, stream),
21            Some(value) => {
22                to_stream::<S, _, _>(&true, stream)?;
23                value.to_stream(stream)
24            }
25        }
26    }
27}
28
29impl<S, T: FromStream<S>> FromStream<S> for Option<T> {
30    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
31        Ok(if from_stream::<S, bool, _>(stream)? {
32            None
33        } else {
34            Some(from_stream(stream)?)
35        })
36    }
37}
38
39impl<S, T: ToStream<S>, E: ToStream<S>> ToStream<S> for Result<T, E> {
40    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
41        match self {
42            Err(err) => {
43                to_stream::<S, _, _>(&false, stream)?;
44                err.to_stream(stream)
45            }
46            Ok(value) => {
47                to_stream::<S, _, _>(&true, stream)?;
48                value.to_stream(stream)
49            }
50        }
51    }
52}
53
54impl<S, T: FromStream<S>, E: FromStream<S>> FromStream<S> for Result<T, E> {
55    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
56        Ok(if from_stream::<S, bool, _>(stream)? {
57            Err(from_stream(stream)?)
58        } else {
59            Ok(from_stream(stream)?)
60        })
61    }
62}