#![cfg_attr(feature = "rpc_try", feature(try_trait_v2))]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate alloc;
pub mod any_pointer;
pub mod any_pointer_list;
pub mod capability;
pub mod capability_list;
pub mod constant;
pub mod data;
pub mod data_list;
pub mod enum_list;
pub mod io;
pub mod list_list;
pub mod message;
pub mod primitive_list;
pub mod private;
pub mod raw;
pub mod serialize;
pub mod serialize_packed;
pub mod struct_list;
pub mod text;
pub mod text_list;
pub mod traits;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(C, align(8))]
pub struct Word {
raw_content: [u8; 8],
}
pub const fn word(b0: u8, b1: u8, b2: u8, b3: u8, b4: u8, b5: u8, b6: u8, b7: u8) -> Word {
Word {
raw_content: [b0, b1, b2, b3, b4, b5, b6, b7],
}
}
impl Word {
pub fn allocate_zeroed_vec(length: usize) -> Vec<Self> {
vec![word(0, 0, 0, 0, 0, 0, 0, 0); length]
}
pub fn words_to_bytes(words: &[Self]) -> &[u8] {
unsafe { core::slice::from_raw_parts(words.as_ptr() as *const u8, words.len() * 8) }
}
pub fn words_to_bytes_mut(words: &mut [Self]) -> &mut [u8] {
unsafe { core::slice::from_raw_parts_mut(words.as_mut_ptr() as *mut u8, words.len() * 8) }
}
}
#[cfg(any(feature = "quickcheck", test))]
impl quickcheck::Arbitrary for Word {
fn arbitrary(g: &mut quickcheck::Gen) -> Self {
crate::word(
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
quickcheck::Arbitrary::arbitrary(g),
)
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct MessageSize {
pub word_count: u64,
pub cap_count: u32,
}
impl core::ops::AddAssign for MessageSize {
fn add_assign(&mut self, rhs: Self) {
self.word_count += rhs.word_count;
self.cap_count += rhs.cap_count;
}
}
#[derive(PartialEq, Clone, Copy, Debug)]
pub struct NotInSchema(pub u16);
impl ::core::fmt::Display for NotInSchema {
fn fmt(
&self,
fmt: &mut ::core::fmt::Formatter,
) -> ::core::result::Result<(), ::core::fmt::Error> {
write!(
fmt,
"Enum value or union discriminant {} was not present in the schema.",
self.0
)
}
}
#[cfg(feature = "std")]
impl ::std::error::Error for NotInSchema {
fn description(&self) -> &str {
"Enum value or union discriminant was not present in schema."
}
}
pub type Result<T> = ::core::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) -> Self {
Self {
description,
kind: ErrorKind::Failed,
}
}
pub fn overloaded(description: String) -> Self {
Self {
description,
kind: ErrorKind::Overloaded,
}
}
pub fn disconnected(description: String) -> Self {
Self {
description,
kind: ErrorKind::Disconnected,
}
}
pub fn unimplemented(description: String) -> Self {
Self {
description,
kind: ErrorKind::Unimplemented,
}
}
}
#[cfg(feature = "std")]
impl core::convert::From<::std::io::Error> for Error {
fn from(err: ::std::io::Error) -> Self {
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,
};
Self {
description: format!("{err}"),
kind,
}
}
}
impl core::convert::From<alloc::string::FromUtf8Error> for Error {
fn from(err: alloc::string::FromUtf8Error) -> Self {
Self::failed(format!("{err}"))
}
}
impl core::convert::From<alloc::str::Utf8Error> for Error {
fn from(err: alloc::str::Utf8Error) -> Self {
Self::failed(format!("{err}"))
}
}
impl core::convert::From<NotInSchema> for Error {
fn from(e: NotInSchema) -> Self {
Self::failed(format!(
"Enum value or union discriminant {} was not present in schema.",
e.0
))
}
}
impl core::fmt::Display for Error {
fn fmt(&self, fmt: &mut core::fmt::Formatter) -> core::result::Result<(), core::fmt::Error> {
write!(fmt, "{:?}: {}", self.kind, self.description)
}
}
#[cfg(feature = "std")]
impl ::std::error::Error for Error {
fn description(&self) -> &str {
&self.description
}
fn cause(&self) -> Option<&dyn (::std::error::Error)> {
None
}
}
pub enum OutputSegments<'a> {
SingleSegment([&'a [u8]; 1]),
MultiSegment(Vec<&'a [u8]>),
}
impl<'a> core::ops::Deref for OutputSegments<'a> {
type Target = [&'a [u8]];
fn deref(&self) -> &[&'a [u8]] {
match self {
OutputSegments::SingleSegment(s) => s,
OutputSegments::MultiSegment(v) => v,
}
}
}
impl<'s> message::ReaderSegments for OutputSegments<'s> {
fn get_segment(&self, id: u32) -> Option<&[u8]> {
match self {
OutputSegments::SingleSegment(s) => s.get(id as usize).copied(),
OutputSegments::MultiSegment(v) => v.get(id as usize).copied(),
}
}
}