data-stream 0.3.3

A simple serialization library based on streams
Documentation
use crate::{FromStream, ToStream, from_stream, to_stream};

use std::io::{Read, Result as IOResult, Write};

impl<S, T: ToStream<S> + ?Sized> ToStream<S> for Box<T> {
    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
        (**self).to_stream(stream)
    }
}

impl<S, T: FromStream<S>> FromStream<S> for Box<T> {
    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
        Ok(Self::new(from_stream(stream)?))
    }
}

impl<S: crate::collections::SizeSettings> FromStream<S> for Box<str> {
    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
        let s: String = from_stream::<S, String, R>(stream)?;
        Ok(s.into())
    }
}

impl<S: crate::collections::SizeSettings, T: FromStream<S>> FromStream<S> for Box<[T]> {
    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
        let v: Vec<T> = from_stream(stream)?;
        Ok(v.into_boxed_slice())
    }
}

impl<S, T: ToStream<S>> ToStream<S> for Option<T> {
    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
        match self {
            None => to_stream::<S, _, _>(&false, stream),
            Some(value) => {
                to_stream::<S, _, _>(&true, stream)?;
                value.to_stream(stream)
            }
        }
    }
}

impl<S, T: FromStream<S>> FromStream<S> for Option<T> {
    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
        Ok(if from_stream::<S, bool, _>(stream)? {
            Some(from_stream(stream)?)
        } else {
            None
        })
    }
}

impl<S, T: ToStream<S>, E: ToStream<S>> ToStream<S> for Result<T, E> {
    fn to_stream<W: Write>(&self, stream: &mut W) -> IOResult<()> {
        match self {
            Ok(value) => {
                to_stream::<S, _, _>(&true, stream)?;
                value.to_stream(stream)
            }
            Err(err) => {
                to_stream::<S, _, _>(&false, stream)?;
                err.to_stream(stream)
            }
        }
    }
}

impl<S, T: FromStream<S>, E: FromStream<S>> FromStream<S> for Result<T, E> {
    fn from_stream<R: Read>(stream: &mut R) -> IOResult<Self> {
        Ok(if from_stream::<S, bool, _>(stream)? {
            Ok(from_stream(stream)?)
        } else {
            Err(from_stream(stream)?)
        })
    }
}