odem_rs_sync/
error.rs

1//! A module for sync-specific error types.
2
3/* ************************************************************** Error Types */
4
5/// Enumeration of errors for the functions and structures contained in this
6/// module of the simulation library.
7#[derive(Copy, Clone, Debug, thiserror::Error)]
8pub enum Error {
9	/// The operation needs to block to complete.
10	#[error("operation would block")]
11	WouldBlock,
12	/// The connection between Sender and Receiver has been terminated.
13	#[error("connection terminated")]
14	Disconnected,
15	/// The facility is already seized.
16	#[error("facility already seized")]
17	Occupied,
18}
19
20/* ******************************************************** Minor Error Types */
21
22/// attempt to send a value through a disconnected channel
23#[derive(Debug, thiserror::Error)]
24#[error("no receiver left")]
25pub struct SendError<T>(pub T);
26
27impl<T> From<SendError<T>> for Error {
28	#[inline]
29	fn from(_: SendError<T>) -> Self {
30		Error::Disconnected
31	}
32}
33
34/// attempted to receive a value from a disconnected channel
35#[derive(Debug, thiserror::Error)]
36#[error("no sender left")]
37pub struct RecvError;
38
39impl From<RecvError> for Error {
40	#[inline]
41	fn from(_: RecvError) -> Self {
42		Error::Disconnected
43	}
44}
45
46/// Signals that a non-blocking attempt at sending a value through a channel has
47/// failed. There are two possible reasons for this: (1) the message buffer of
48/// the channel is full, or (2) the channel is closed.
49#[derive(Debug, thiserror::Error)]
50pub enum TrySendError<T> {
51	/// attempt to send a value has failed for now because the channel is full
52	#[error("channel buffer is full")]
53	Full(T),
54	/// attempt to send a value has failed because the channel is disconnected
55	#[error("channel disconnected")]
56	Disconnected(T),
57}
58
59impl<T> From<TrySendError<T>> for Error {
60	#[inline]
61	fn from(err: TrySendError<T>) -> Self {
62		match err {
63			TrySendError::Full(_) => Error::WouldBlock,
64			TrySendError::Disconnected(_) => Error::Disconnected,
65		}
66	}
67}
68
69/// Signals that a non-blocking attempt at receiving a value through a channel
70/// has failed. There are two possible reasons for this: (1) the message buffer
71/// of the channel is empty, or (2) the channel is closed.
72#[derive(Debug, thiserror::Error)]
73pub enum TryRecvError {
74	/// attempt to receive a value has failed because the channel is empty
75	#[error("channel buffer is empty")]
76	Empty,
77	/// attempt to receive a value has failed because the channel is disconnected
78	#[error("channel disconnected")]
79	Disconnected,
80}
81
82impl From<TryRecvError> for Error {
83	#[inline]
84	fn from(err: TryRecvError) -> Self {
85		match err {
86			TryRecvError::Empty => Error::WouldBlock,
87			TryRecvError::Disconnected => Error::Disconnected,
88		}
89	}
90}
91
92/// Signals that a facility is already seized and cannot be seized at this time.
93#[derive(Debug, thiserror::Error)]
94#[error("facility already seized")]
95pub struct FacilityOccupied;
96
97impl From<FacilityOccupied> for Error {
98	#[inline]
99	fn from(_: FacilityOccupied) -> Self {
100		Error::Occupied
101	}
102}