|
| 1 | +//! Binary serialization, and an implementation over Unix pipes. |
| 2 | +use sealed::DeSerializeBytes; |
| 3 | +use std::{ |
| 4 | + io::{self, Read, Write}, |
| 5 | + marker::PhantomData, |
| 6 | + os::{fd::AsRawFd, unix::net::UnixStream}, |
| 7 | +}; |
| 8 | + |
| 9 | +mod sealed { |
| 10 | + pub trait DeSerializeBytes { |
| 11 | + fn zero_init() -> Self; |
| 12 | + fn as_mut_ref(&mut self) -> &mut [u8]; |
| 13 | + } |
| 14 | + |
| 15 | + impl<const N: usize> DeSerializeBytes for [u8; N] { |
| 16 | + fn zero_init() -> [u8; N] { |
| 17 | + [0; N] |
| 18 | + } |
| 19 | + fn as_mut_ref(&mut self) -> &mut [u8] { |
| 20 | + self.as_mut_slice() |
| 21 | + } |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +/// Serialization/deserialization trait using a byte array as storage. |
| 26 | +pub trait DeSerialize { |
| 27 | + /// Usually `[u8; std::mem::size_of::<Self>()]`. |
| 28 | + type Bytes: sealed::DeSerializeBytes; |
| 29 | + fn serialize(&self) -> Self::Bytes; |
| 30 | + fn deserialize(bytes: Self::Bytes) -> Self; |
| 31 | +} |
| 32 | + |
| 33 | +/// A binary pipe that can send and recieve typed messages. |
| 34 | +/// |
| 35 | +/// By default, if only one generic is included, |
| 36 | +/// the types of the [BinPipe::write()] and [BinPipe::read()] messages |
| 37 | +/// are the same. |
| 38 | +pub struct BinPipe<R: DeSerialize, W: DeSerialize = R> { |
| 39 | + sock: UnixStream, |
| 40 | + _read_marker: PhantomData<R>, |
| 41 | + _write_marker: PhantomData<W>, |
| 42 | +} |
| 43 | + |
| 44 | +impl<R: DeSerialize, W: DeSerialize> BinPipe<R, W> { |
| 45 | + /// A pipe abstracting over a [UnixStream] with easier |
| 46 | + /// binary serialization, to help with the buffer sizes and ser/de steps. |
| 47 | + /// Uses [UnixStream::pair()]. |
| 48 | + pub fn pair() -> io::Result<(BinPipe<R, W>, BinPipe<W, R>)> { |
| 49 | + let (first, second) = UnixStream::pair()?; |
| 50 | + Ok(( |
| 51 | + BinPipe { |
| 52 | + sock: first, |
| 53 | + _read_marker: PhantomData::<R>, |
| 54 | + _write_marker: PhantomData::<W>, |
| 55 | + }, |
| 56 | + // R and W are inverted here since the type of what's written in one |
| 57 | + // pipe is read in the other, and vice versa. |
| 58 | + BinPipe { |
| 59 | + sock: second, |
| 60 | + _read_marker: PhantomData::<W>, |
| 61 | + _write_marker: PhantomData::<R>, |
| 62 | + }, |
| 63 | + )) |
| 64 | + } |
| 65 | + |
| 66 | + /// Read a `R` from the pipe. |
| 67 | + pub fn read(&mut self) -> io::Result<R> { |
| 68 | + let mut bytes = R::Bytes::zero_init(); |
| 69 | + self.sock.read_exact(bytes.as_mut_ref())?; |
| 70 | + Ok(R::deserialize(bytes)) |
| 71 | + } |
| 72 | + |
| 73 | + /// Write a `W` to the pipe. |
| 74 | + pub fn write(&mut self, bytes: &W) -> io::Result<()> { |
| 75 | + self.sock.write_all(bytes.serialize().as_mut_ref())?; |
| 76 | + Ok(()) |
| 77 | + } |
| 78 | + |
| 79 | + /// Calls [std::net::TcpStream::set_nonblocking] on the underlying socket. |
| 80 | + pub fn set_nonblocking(&self, nonblocking: bool) -> io::Result<()> { |
| 81 | + self.sock.set_nonblocking(nonblocking) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +impl<R: DeSerialize, W: DeSerialize> AsRawFd for BinPipe<R, W> { |
| 86 | + fn as_raw_fd(&self) -> std::os::fd::RawFd { |
| 87 | + self.sock.as_raw_fd() |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl DeSerialize for i32 { |
| 92 | + type Bytes = [u8; std::mem::size_of::<Self>()]; |
| 93 | + |
| 94 | + fn serialize(&self) -> Self::Bytes { |
| 95 | + self.to_ne_bytes() |
| 96 | + } |
| 97 | + fn deserialize(bytes: Self::Bytes) -> Self { |
| 98 | + Self::from_ne_bytes(bytes) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +#[cfg(test)] |
| 103 | +mod tests { |
| 104 | + use super::*; |
| 105 | + |
| 106 | + #[test] |
| 107 | + pub fn single_type() { |
| 108 | + let (mut tx, mut rx) = BinPipe::pair().unwrap(); |
| 109 | + tx.write(&42i32).unwrap(); |
| 110 | + assert_eq!(rx.read().unwrap(), 42); |
| 111 | + rx.write(&23i32).unwrap(); |
| 112 | + assert_eq!(tx.read().unwrap(), 23); |
| 113 | + } |
| 114 | + |
| 115 | + #[test] |
| 116 | + pub fn different_types() { |
| 117 | + impl DeSerialize for u8 { |
| 118 | + type Bytes = [u8; std::mem::size_of::<Self>()]; |
| 119 | + fn serialize(&self) -> [u8; 1] { |
| 120 | + self.to_ne_bytes() |
| 121 | + } |
| 122 | + fn deserialize(bytes: [u8; 1]) -> Self { |
| 123 | + Self::from_ne_bytes(bytes) |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + let (mut tx, mut rx) = BinPipe::pair().unwrap(); |
| 128 | + tx.write(&42i32).unwrap(); |
| 129 | + assert_eq!(rx.read().unwrap(), 42); |
| 130 | + rx.write(&23u8).unwrap(); |
| 131 | + assert_eq!(tx.read().unwrap(), 23); |
| 132 | + } |
| 133 | +} |
0 commit comments