use std::{
panic::{RefUnwindSafe, UnwindSafe},
sync::Arc,
};
use super::{ReadHalf, Socket, WriteHalf};
use crate::runtime::io::PollSend;
#[derive(Debug)]
pub struct Split<R: ReadHalf, W: WriteHalf> {
pub(super) read: R,
pub(super) write: W,
pub(super) writes: Option<Writes>,
}
impl<R: ReadHalf, W: WriteHalf> Split<R, W> {
pub fn new(read: R, write: W) -> Self {
Self {
read,
write,
writes: None,
}
}
pub fn read(&self) -> &R {
&self.read
}
pub fn read_mut(&mut self) -> &mut R {
&mut self.read
}
pub fn write(&self) -> &W {
&self.write
}
pub fn write_mut(&mut self) -> &mut W {
&mut self.write
}
pub fn take(self) -> (R, W) {
(self.read, self.write)
}
pub(crate) fn with_writes(read: R, write: W, writes: Option<Writes>) -> Self {
Self {
read,
write,
writes,
}
}
pub(crate) fn take_writes(&mut self) -> Option<Writes> {
self.writes.take()
}
}
pub type BoxedSplit = Split<Box<dyn ReadHalf>, Box<dyn WriteHalf>>;
impl<S: Socket> From<S> for BoxedSplit {
fn from(socket: S) -> Self {
let split = socket.split();
Split {
read: Box::new(split.read),
write: Box::new(split.write),
writes: split.writes,
}
}
}
#[derive(Debug)]
pub(crate) enum Writes {
Driven(Arc<dyn PollSend>),
#[cfg(feature = "p2p")]
Whole,
}
impl UnwindSafe for Writes {}
impl RefUnwindSafe for Writes {}