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: crate::collections::SizeSettings> FromStream<S> for Box<str> {
18 fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
19 let s: String = from_stream::<S, String, R>(stream)?;
20 Ok(s.into())
21 }
22}
23
24impl<S: crate::collections::SizeSettings, T: ToStream<S>> ToStream<S> for Box<[T]> {
25 fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
26 self.as_ref().to_stream(stream)
27 }
28}
29
30impl<S: crate::collections::SizeSettings, T: FromStream<S>> FromStream<S> for Box<[T]> {
31 fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
32 let v: Vec<T> = from_stream(stream)?;
33 Ok(v.into_boxed_slice())
34 }
35}
36
37impl<S, T: ToStream<S>> ToStream<S> for Option<T> {
38 fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
39 match self {
40 None => to_stream::<S, _, _>(&false, stream),
41 Some(value) => {
42 to_stream::<S, _, _>(&true, stream)?;
43 value.to_stream(stream)
44 }
45 }
46 }
47}
48
49impl<S, T: FromStream<S>> FromStream<S> for Option<T> {
50 fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
51 Ok(if from_stream::<S, bool, _>(stream)? {
52 Some(from_stream(stream)?)
53 } else {
54 None
55 })
56 }
57}
58
59impl<S, T: ToStream<S>, E: ToStream<S>> ToStream<S> for Result<T, E> {
60 fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
61 match self {
62 Ok(value) => {
63 to_stream::<S, _, _>(&true, stream)?;
64 value.to_stream(stream)
65 }
66 Err(err) => {
67 to_stream::<S, _, _>(&false, stream)?;
68 err.to_stream(stream)
69 }
70 }
71 }
72}
73
74impl<S, T: FromStream<S>, E: FromStream<S>> FromStream<S> for Result<T, E> {
75 fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
76 Ok(if from_stream::<S, bool, _>(stream)? {
77 Ok(from_stream(stream)?)
78 } else {
79 Err(from_stream(stream)?)
80 })
81 }
82}