#[allow(unused_imports)] use crate::prelude::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct Unavailable {
_priv: (),
}
#[derive(Derivative)]
#[derivative(Debug(bound = ""))]
pub enum SessionIncomplete<Tx, Rx> {
BothHalves {
tx: IncompleteHalf<Tx>,
rx: IncompleteHalf<Rx>,
},
TxHalf {
tx: IncompleteHalf<Tx>,
#[derivative(Debug = "ignore")]
rx: Rx,
},
RxHalf {
#[derivative(Debug = "ignore")]
tx: Tx,
rx: IncompleteHalf<Rx>,
},
}
#[derive(Derivative)]
#[derivative(Debug(bound = ""))]
pub enum IncompleteHalf<T> {
Unfinished(#[derivative(Debug = "ignore")] T),
Unclosed,
}
impl<T> std::error::Error for IncompleteHalf<T> {}
impl<T> std::fmt::Display for IncompleteHalf<T> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "incomplete session or sub-session: channel half ")?;
write!(
f,
"{}",
match self {
IncompleteHalf::Unfinished(_) => "was dropped before the session was `Done`",
IncompleteHalf::Unclosed => "was not closed after the session was `Done`",
}
)
}
}
impl<Tx, Rx> SessionIncomplete<Tx, Rx> {
pub fn into_halves(
self,
) -> (
Result<Tx, IncompleteHalf<Tx>>,
Result<Rx, IncompleteHalf<Rx>>,
) {
match self {
SessionIncomplete::BothHalves { tx, rx } => (Err(tx), Err(rx)),
SessionIncomplete::TxHalf { tx, rx } => (Err(tx), Ok(rx)),
SessionIncomplete::RxHalf { tx, rx } => (Ok(tx), Err(rx)),
}
}
}
impl<Tx, Rx> std::error::Error for SessionIncomplete<Tx, Rx> {}
impl<Tx, Rx> std::fmt::Display for SessionIncomplete<Tx, Rx> {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
use IncompleteHalf::*;
write!(f, "incomplete session or sub-session: channel")?;
let reason = match self {
SessionIncomplete::BothHalves { tx, rx } => match (tx, rx) {
(Unclosed, Unclosed) => " was not closed after the session was `Done`",
(Unclosed, Unfinished(_)) => {
"'s sending half was not closed after the session was `Done` \
and its receiving half was dropped before the session was `Done`"
}
(Unfinished(_), Unclosed) => {
"'s sending half was dropped before the session was `Done` \
and its receiving half was not closed after the session was `Done`"
}
(Unfinished(_), Unfinished(_)) => " was dropped before the session was `Done`",
},
SessionIncomplete::TxHalf { tx, .. } => match tx {
Unfinished(_) => "'s sending half was dropped before the session was `Done`",
Unclosed => "'s sending half was not closed after the session was `Done`",
},
SessionIncomplete::RxHalf { rx, .. } => match rx {
Unfinished(_) => "'s receiving half was dropped before the session was `Done`",
Unclosed => "'s receiving half was not closed after the session was `Done`",
},
};
write!(f, "{}", reason)
}
}