Debian Codegraph @

src/common/bin_serde.rs

raw symbols SCIP
1 //! Binary serialization, and an implementation over Unix pipes.
2 use sealedrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/::DeSerializeBytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#;
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
4 iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/, Readrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#, Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#},
5 markerrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/::PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#,
6 osrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/::{
7 fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/::{AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd#, BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#},
8 unixrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/::netrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/::UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#,
9 },
10 };
11
12 mod sealedrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/ {
13 pub trait DeSerializeBytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes# {
14 fn zero_initrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#zero_init().() -> Self;
15 fn as_mut_refrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#as_mut_ref().(&mut self) -> &mut [u8];
16 }
17
18 impl<const N: usize> DeSerializeBytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes# for [u8; N] {
19 fn zero_initrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/impl#[`[u8; N]`][DeSerializeBytes]zero_init().() -> [u8; N] {
20 [0; N]
21 }
22 fn as_mut_refrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/impl#[`[u8; N]`][DeSerializeBytes]as_mut_ref().(&mut self) -> &mut [u8] {
23 self.as_mut_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`]as_mut_slice().()
24 }
25 }
26 }
27
28 /// Serialization/deserialization trait using a byte array as storage.
29 pub trait DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize# {
30 /// Usually `[u8; std::mem::size_of::<Self>()]`.
31 type Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes#: sealedrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/::DeSerializeBytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#;
32 fn serializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#serialize().(&self) -> Self::Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes#;
33 fn deserializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#deserialize().(bytes: Self::Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes#) -> Self;
34 }
35
36 /// A binary pipe that can send and receive typed messages.
37 ///
38 /// By default, if only one generic is included,
39 /// the types of the [BinPipe::write()] and [BinPipe::read()] messages
40 /// are the same.
41 pub struct BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#<R: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#, W: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize# = R> {
42 sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock.: UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#,
43 _read_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_read_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<R>,
44 _write_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_write_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#<W>,
45 }
46
47 impl<R: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#, W: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#> BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#<R, W> {
48 /// A pipe abstracting over a [UnixStream] with easier
49 /// binary serialization, to help with the buffer sizes and ser/de steps.
50 /// Uses [UnixStream::pair()].
51 pub fn pairrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]pair().() -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<(BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#<R, W>, BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#<W, R>)> {
52 let (first, second) = UnixStreamrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/UnixStream#::pairrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/impl#[UnixStream]pair().()?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
53 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((
54 BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe# {
55 sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock.: first,
56 _read_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_read_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<R>,
57 _write_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_write_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<W>,
58 },
59 // R and W are inverted here since the type of what's written in one
60 // pipe is read in the other, and vice versa.
61 BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe# {
62 sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock.: second,
63 _read_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_read_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<W>,
64 _write_markerrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#_write_marker.: PhantomDatarust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/PhantomData#::<R>,
65 },
66 ))
67 }
68
69 /// Read a `R` from the pipe.
70 pub fn readrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]read().(&mut self) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<R> {
71 let mut bytes = R::Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes#::zero_initrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#zero_init().();
72 self.sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock..read_exactrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Read#read_exact().(bytes.as_mut_refrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#as_mut_ref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
73 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(R::deserializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#deserialize().(bytes))
74 }
75
76 /// Write a `W` to the pipe.
77 pub fn writerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]write().(&mut self, bytes: &W) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
78 self.sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock..write_allrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write#write_all().(bytes.serializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#serialize().().as_mut_refrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/sealed/DeSerializeBytes#as_mut_ref().())?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().;
79 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())
80 }
81
82 /// Calls [std::net::TcpStream::set_nonblocking] on the underlying socket.
83 #[cfg(debug_assertions)]
84 pub fn set_nonblockingrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]set_nonblocking().(&self, nonblocking: bool) -> iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()> {
85 self.sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock..set_nonblockingrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/impl#[UnixStream]set_nonblocking().(nonblocking)
86 }
87 }
88
89 impl<R: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#, W: DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#> AsFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/AsFd# for BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#<R, W> {
90 fn as_fdrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`][AsFd]as_fd().(&self) -> BorrowedFdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/fd/owned/BorrowedFd#<'_> {
91 self.sockrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#sock..as_fdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std os/unix/net/stream/impl#[UnixStream][AsFd]as_fd().()
92 }
93 }
94
95 impl DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize# for i32 {
96 type Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]Bytes# = [u8; stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]>()];
97
98 fn serializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]serialize().(&self) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]::Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes# {
99 self.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]to_ne_bytes().()
100 }
101 fn deserializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]deserialize().(bytes: Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]::Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize#Bytes#) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize] {
102 Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[i32][DeSerialize]::from_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[i32]from_ne_bytes().(bytes)
103 }
104 }
105
106 #[cfg(test)]
107 mod testsrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/ {
108 use superrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/::*;
109
110 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
111 pub fn single_typerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/single_type().() {
112 let (mut tx, mut rx) = BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#::pairrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]pair().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
113 tx.writerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]write().(&42i32).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
114 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rx.readrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]read().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 42);
115 rx.writerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]write().(&23i32).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
116 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(tx.readrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]read().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 23);
117 }
118
119 impl DeSerializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/DeSerialize# for u8 {
120 type Bytesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize]Bytes# = [u8; stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::size_ofrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/size_of().::<Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize]>()];
121 fn serializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize]serialize().(&self) -> [u8; 1] {
122 self.to_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]to_ne_bytes().()
123 }
124 fn deserializerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize]deserialize().(bytes: [u8; 1]) -> Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize] {
125 Selfrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/impl#[u8][DeSerialize]::from_ne_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core num/impl#[u8]from_ne_bytes().(bytes)
126 }
127 }
128
129 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
130 pub fn different_typesrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/tests/different_types().() {
131 let (mut tx, mut rx) = BinPiperust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/BinPipe#::pairrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]pair().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
132 tx.writerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]write().(&42i32).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
133 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(rx.readrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]read().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 42);
134 rx.writerust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]write().(&23u8).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
135 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(tx.readrust-analyzer cargo sudo-rs 0.2.14 common/bin_serde/impl#[`BinPipe<R, W>`]read().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(), 23);
136 }
137 }