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
pub use self::numerics::Integer;
pub use self::array::Array;
pub use self::string::String;

pub mod numerics;
#[macro_use]
pub mod composite;
pub mod array;
pub mod string;
pub mod char;
pub mod tuple;
pub mod option;
/// Defintions for the `std::collections` module.
pub mod collections;
/// Definitions for smart pointers in the `std` module.
pub mod smart_ptr;
#[cfg(feature = "uuid")]
pub mod uuid;

use std::io::prelude::*;
use std::{fmt, io};

/// The default byte ordering.
pub type ByteOrder = ::byteorder::BigEndian;

/// A type which can be read or written.
pub trait Type : Clone + fmt::Debug
{
    /// Reads a type for a stream.
    fn read(read: &mut Read) -> Result<Self, ::Error>;

    /// Writes a type to a stream.
    fn write(&self, write: &mut Write) -> Result<(), ::Error>;

    fn from_raw_bytes(bytes: &[u8]) -> Result<Self, ::Error> {
        let mut buffer = ::std::io::Cursor::new(bytes);
        Self::read(&mut buffer)
    }

    fn raw_bytes(&self) -> Result<Vec<u8>, ::Error> {
        let mut buffer = io::Cursor::new(Vec::new());
        self.write(&mut buffer)?;

        Ok(buffer.into_inner())
    }
}