#![forbid(unsafe_code)]
mod bread_thread;
mod completer;
mod controller;
mod directive;
mod key;
mod thread_handle;
mod thread_state;
pub(crate) mod directive_thread;
pub(crate) mod mutex;
pub use bread_thread::*;
pub use completer::*;
pub use controller::*;
pub use directive::*;
pub use key::*;
pub use thread_handle::*;
pub(crate) use thread_state::*;
use std::{error::Error as StdError, fmt, num::NonZeroUsize};
use thread_safe::NotInOriginThread;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum Error<InnerError> {
InvalidPtr(NonZeroUsize),
Closed,
UnableToComplete,
AlreadyABreadThread,
NotInBreadThread,
Controller(InnerError),
}
impl<InnerError> From<NotInOriginThread> for Error<InnerError> {
#[inline]
fn from(_: NotInOriginThread) -> Error<InnerError> {
Error::NotInBreadThread
}
}
impl<InnerError: fmt::Display> fmt::Display for Error<InnerError> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::InvalidPtr(ptr) => {
write!(f, "Directive contained external pointer that does not belong to the bread thread: {:#08X}", ptr)
}
Error::Closed => f.write_str("The bread thread is closed and cannot be used"),
Error::UnableToComplete => f.write_str("The controller did not complete the directive"),
Error::AlreadyABreadThread => f.write_str("This thread is already a bread thread"),
Error::NotInBreadThread => f.write_str("This thread is not the bread thread"),
Error::Controller(inner) => fmt::Display::fmt(inner, f),
}
}
}
impl<InnerError: fmt::Display + fmt::Debug> StdError for Error<InnerError> {
#[inline]
fn source(&self) -> Option<&(dyn StdError + 'static)> {
match self {
Error::NotInBreadThread => Some(&NotInOriginThread),
_ => None,
}
}
}