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 text;
16mod wrappers;
17
18#[cfg(feature = "derive")]
19pub use data_stream_derive::{FromStream, ToStream};
20
21use std::io::{Read, Result, Write};
22
23/// A trait for types, which can be written to streams.
24/// Most argument types might need some settings.
25pub trait ToStream<S> {
26 /// Writes a value of this type to a stream. Return an error on failure.
27 fn to_stream<W: Write>(&self, stream: &mut W) -> Result<()>;
28}
29
30/// A trait for types, which can be read from streams.
31pub trait FromStream<S>: Sized {
32 /// Reads a value of this type from a stream and returns it. Returns an error on failure instead.
33 fn from_stream<R: Read>(stream: &mut R) -> Result<Self>;
34}
35
36#[inline(always)]
37/// A simple function to write a value to a stream.
38pub fn to_stream<S, V: ToStream<S> + ?Sized, W: Write>(value: &V, stream: &mut W) -> Result<()> {
39 value.to_stream(stream)
40}
41
42#[inline(always)]
43/// A simple function to read a value from a stream.
44pub fn from_stream<S, V: FromStream<S>, R: Read>(stream: &mut R) -> Result<V> {
45 V::from_stream(stream)
46}
47
48/// Some simple default settings for streaming.
49pub mod default_settings;