extern crate byteorder;
#[cfg(any(feature="quickcheck", test))]
extern crate quickcheck;
#[cfg(feature = "rpc")]
extern crate gj;
pub mod any_pointer;
pub mod capability;
pub mod data;
pub mod data_list;
pub mod enum_list;
pub mod list_list;
pub mod message;
pub mod primitive_list;
pub mod private;
pub mod serialize;
pub mod serialize_packed;
pub mod struct_list;
pub mod text;
pub mod text_list;
pub mod traits;
mod util;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C)]
pub struct Word(u64);
impl Word {
pub fn allocate_zeroed_vec(length: usize) -> Vec<Word> {
let mut result : Vec<Word> = Vec::with_capacity(length);
unsafe {
result.set_len(length);
let p : *mut u8 = result.as_mut_ptr() as *mut u8;
::std::ptr::write_bytes(p, 0u8, length * ::std::mem::size_of::<Word>());
}
result
}
pub fn bytes_to_words<'a>(bytes: &'a [u8]) -> &'a [Word] {
unsafe {
::std::slice::from_raw_parts(bytes.as_ptr() as *const Word, bytes.len() / 8)
}
}
pub fn bytes_to_words_mut<'a>(bytes: &'a mut [u8]) -> &'a mut [Word] {
unsafe {
::std::slice::from_raw_parts_mut(bytes.as_ptr() as *mut Word, bytes.len() / 8)
}
}
pub fn words_to_bytes<'a>(words: &'a [Word]) -> &'a [u8] {
unsafe {
::std::slice::from_raw_parts(words.as_ptr() as *const u8, words.len() * 8)
}
}
pub fn words_to_bytes_mut<'a>(words: &'a mut [Word]) -> &'a mut [u8] {
unsafe {
::std::slice::from_raw_parts_mut(words.as_mut_ptr() as *mut u8, words.len() * 8)
}
}
#[cfg(test)]
pub fn from(n: u64) -> Word {
Word(n)
}
}
#[cfg(any(feature="quickcheck", test))]
impl quickcheck::Arbitrary for Word {
fn arbitrary<G: quickcheck::Gen>(g: &mut G) -> Word {
Word(quickcheck::Arbitrary::arbitrary(g))
}
}
#[derive(Clone, Copy, PartialEq)]
pub struct MessageSize {
pub word_count: u64,
pub cap_count: u32
}
impl MessageSize {
pub fn plus_eq(&mut self, other : MessageSize) {
self.word_count += other.word_count;
self.cap_count += other.cap_count;
}
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct NotInSchema(pub u16);
impl ::std::fmt::Display for NotInSchema {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!(fmt, "Enum value or union discriminant {} was not present in the schema.", self.0)
}
}
impl ::std::error::Error for NotInSchema {
fn description<'a>(&'a self) -> &'a str {
"Enum value or union disriminant was not present in schema."
}
}
pub type Result<T> = ::std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct Error {
pub kind: ErrorKind,
pub description: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorKind {
Failed,
Overloaded,
Disconnected,
Unimplemented,
}
impl Error {
pub fn failed(description: String) -> Error {
Error { description: description, kind: ErrorKind::Failed }
}
pub fn overloaded(description: String) -> Error {
Error { description: description, kind: ErrorKind::Overloaded }
}
pub fn disconnected(description: String) -> Error {
Error { description: description, kind: ErrorKind::Disconnected }
}
pub fn unimplemented(description: String) -> Error {
Error { description: description, kind: ErrorKind::Unimplemented }
}
}
impl ::std::convert::From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Error {
use std::io;
let kind = match err.kind() {
io::ErrorKind::TimedOut => ErrorKind::Overloaded,
io::ErrorKind::BrokenPipe |
io::ErrorKind::ConnectionRefused |
io::ErrorKind::ConnectionReset |
io::ErrorKind::ConnectionAborted |
io::ErrorKind::NotConnected => ErrorKind::Disconnected,
_ => ErrorKind::Failed,
};
Error { description: format!("{}", err), kind: kind }
}
}
impl ::std::convert::From<NotInSchema> for Error {
fn from(e: NotInSchema) -> Error {
Error::failed(format!("Enum value or union discriminant {} was not present in schema.", e.0))
}
}
impl ::std::fmt::Display for Error {
fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
write!(fmt, "{:?}: {}", self.kind, self.description)
}
}
impl ::std::error::Error for Error {
fn description(&self) -> &str {
&self.description
}
fn cause(&self) -> Option<&::std::error::Error> {
None
}
}
#[cfg(feature = "rpc")]
impl ::gj::FulfillerDropped for Error {
fn fulfiller_dropped() -> Error {
Error::failed("Promise fulfiller was dropped.".to_string())
}
}
pub enum OutputSegments<'a> {
#[doc(hidden)]
SingleSegment([&'a [Word]; 1]),
#[doc(hidden)]
MultiSegment(Vec<&'a [Word]>),
}
impl <'a> ::std::ops::Deref for OutputSegments<'a> {
type Target = [&'a [Word]];
fn deref<'b>(&'b self) -> &'b [&'a [Word]] {
match self {
&OutputSegments::SingleSegment(ref s) => {
s
}
&OutputSegments::MultiSegment(ref v) => {
&*v
}
}
}
}