data_stream/lib.rs
1#![deny(missing_docs)]
2
3/*!
4This crate simplifies writing data to and reading data from streams.
5You just need to implement the traits `ToStream` and `FromStream`.
6You might have to implement value specific traits to custom settings types.
7**/
8
9mod arrays;
10mod bool;
11/// Settings for collection streaming.
12pub mod collections;
13/// Settings for number streaming.
14pub mod numbers;
15mod wrappers;
16
17use std::io::{Read, Result, Write};
18
19/// A trait for types, which can be written to streams.
20/// Most argument types might need some settings.
21pub trait ToStream<S> {
22 /// Writes a value of this type to a stream. Return an error on failure.
23 fn to_stream<W: Write>(&self, stream: &mut W) -> Result<()>;
24}
25
26/// A trait for types, which can be read from streams.
27pub trait FromStream<S>: Sized {
28 /// Reads a value of this type from a stream and returns it. Returns an error on failure instead.
29 fn from_stream<R: Read>(stream: &mut R) -> Result<Self>;
30}
31
32#[inline(always)]
33/// A simple function to write a value to a stream.
34pub fn to_stream<S, V: ToStream<S> + ?Sized, W: Write>(value: &V, stream: &mut W) -> Result<()> {
35 value.to_stream(stream)
36}
37
38#[inline(always)]
39/// A simple function to read a value from a stream.
40pub fn from_stream<S, V: FromStream<S>, R: Read>(stream: &mut R) -> Result<V> {
41 V::from_stream(stream)
42}
43
44/// Some simple default settings for streaming.
45pub mod default_settings;