37 lines
952 B
Rust
37 lines
952 B
Rust
use std::{io, os::fd::{AsRawFd, OwnedFd}};
|
|
|
|
#[cfg(target_os = "yggdrasil")]
|
|
pub mod yggdrasil;
|
|
#[cfg(target_os = "yggdrasil")]
|
|
pub use yggdrasil::*;
|
|
|
|
#[cfg(any(unix, rust_analyzer))]
|
|
pub mod unix;
|
|
#[cfg(unix)]
|
|
pub use unix::*;
|
|
|
|
pub enum Either<T, U> {
|
|
Left(T),
|
|
Right(U)
|
|
}
|
|
|
|
pub trait IpcSender: AsRawFd {
|
|
fn send_message_to(&self, message: &[u8], peer: &PeerAddr) -> Result<(), io::Error>;
|
|
fn send_file_to<F: AsRawFd>(&self, file: &F, peer: &PeerAddr) -> Result<(), io::Error>;
|
|
|
|
fn host_addr(&self) -> &PeerAddr;
|
|
}
|
|
|
|
pub trait IpcReceiver: AsRawFd {
|
|
fn receive(&self, buffer: &mut [u8]) -> Result<(PeerAddr, Either<usize, OwnedFd>), io::Error>;
|
|
}
|
|
|
|
impl<T, U> Either<T, U> {
|
|
pub fn try_map_left<E, R, F: FnOnce(T) -> Result<R, E>>(self, map: F) -> Result<Either<R, U>, E> {
|
|
match self {
|
|
Self::Left(val) => map(val).map(Either::Left),
|
|
Self::Right(val) => Ok(Either::Right(val))
|
|
}
|
|
}
|
|
}
|