1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
use Settings;
use std::io::prelude::*;
use std::io;
/// A value which can be read and written.
///
/// All of the expected standard Rust types implement this.
///
/// Types that implement `Parcel` include (not exhaustive):
///
/// * The Rust primitive types
/// * `u8`, `i8`, `u16`, `i16` ... `u64`
/// * `bool`, `char`
/// * `f32`, `f64`
/// * Tuples
/// * `(T1) where T1: Parcel`
/// * `(T1, T2) where T1: Parcel, T2: Parcel`
/// * `(T1, T2, ... Tn) where T1: Parcel, T2: Parcel, ... Tn: Parcel`
/// * Arrays
/// * `[T; 0] where T: Parcel`
/// * `[T; 1] where T: Parcel`
/// * `[T; 2] where T: Parcel`
/// * `[T; 32] where T: Parcel`
/// * `[T; 40] where T: Parcel`
/// * `[T; 42] where T: Parcel`
/// * ...
/// * `[T; 64] where T: Parcel`
/// * ...
/// * `[T; 1024] where T: Parcel`
/// * `String`
/// * `Option<T: Parcel>`
/// * `Box<T: Parcel>`
/// * Smart pointers
/// * `std::rc::Rc`
/// * `std::sync::Arc`
/// * `std::collections`
/// * `Vec<T: Parcel>`
/// * `VecDeque<T: Parcel>`
/// * `HashMap<T: Parcel>`
/// * `BTreeMap<T: Parcel>`
pub trait Parcel : Sized
{
/// The textual name of the type.
const TYPE_NAME: &'static str;
/// Reads a value from a stream.
///
/// Blocks until a value is received.
fn read(read: &mut Read,
settings: &Settings) -> Result<Self, ::Error>;
/// Writes a value to a stream.
fn write(&self, write: &mut Write,
settings: &Settings) -> Result<(), ::Error>;
/// Parses a new value from its raw byte representation.
///
/// Returns `Err` if the bytes represent an invalid value.
fn from_raw_bytes(bytes: &[u8],
settings: &Settings) -> Result<Self, ::Error> {
let mut buffer = ::std::io::Cursor::new(bytes);
Self::read(&mut buffer, settings)
}
/// Gets the raw byte representation of the value.
fn raw_bytes(&self, settings: &Settings) -> Result<Vec<u8>, ::Error> {
let mut buffer = io::Cursor::new(Vec::new());
self.write(&mut buffer, settings)?;
Ok(buffer.into_inner())
}
/// Gets the name of the type; `Parcel::TYPE_NAME`.
fn type_name(&self) -> &'static str { Self::TYPE_NAME }
}