| 1 | //!A unidirectional pipe for bytes, analogous to a unix pipe. Available with the `io-pipe` feature toggle. |
| 2 | |
| 3 | /// A unidirectional pipe for bytes, analogous to a unix pipe. Available with the `io-pipe` feature toggle. |
| 4 | #[cfg(feature = "io-pipe")] |
| 5 | pub mod piperust-analyzer cargo gix-features 0.48.0 io/pipe/ { |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/; |
| 7 | |
| 8 | use bytesrust-analyzer cargo bytes 1.11.1 crate/::{Bufrust-analyzer cargo bytes 1.11.1 buf/buf_impl/Buf#, BufMutrust-analyzer cargo bytes 1.11.1 buf/buf_mut/BufMut#, BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#}; |
| 9 | |
| 10 | /// The write-end of the pipe, receiving items to become available in the [`Reader`]. |
| 11 | /// |
| 12 | /// It's commonly used with the [`std::io::Write`] trait it implements. |
| 13 | pub struct Writerrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer# { |
| 14 | /// The channel through which bytes are transferred. Useful for sending [`std::io::Error`]s instead. |
| 15 | pub channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#channel.: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::SyncSenderrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/SyncSender#<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#<BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#>>, |
| 16 | bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#buf.: BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#, |
| 17 | } |
| 18 | |
| 19 | /// The read-end of the pipe, implementing the [`std::io::Read`] trait. |
| 20 | pub struct Readerrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader# { |
| 21 | channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#channel.: stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::Receiverrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/Receiver#<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#<BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#>>, |
| 22 | bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf.: BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#, |
| 23 | } |
| 24 | |
| 25 | impl iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::BufReadrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/BufRead# for Readerrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader# { |
| 26 | fn fill_bufrust-analyzer cargo gix-features 0.48.0 io/pipe/impl#[Reader][BufRead]fill_buf().(&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#<&[u8]> { |
| 27 | if self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..is_emptyrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]is_empty().() { |
| 28 | match self.channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#channel..recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().() { |
| 29 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf)) => self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf. = buf, |
| 30 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err)) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err), |
| 31 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => {} |
| 32 | } |
| 33 | } |
| 34 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(&self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf.) |
| 35 | } |
| 36 | |
| 37 | fn consumerust-analyzer cargo gix-features 0.48.0 io/pipe/impl#[Reader][BufRead]consume().(&mut self, amt: usize) { |
| 38 | self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..advancerust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut][Buf]advance().(amt.minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..lenrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]len().())); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | impl iorust-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# for Readerrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader# { |
| 43 | fn readrust-analyzer cargo gix-features 0.48.0 io/pipe/impl#[Reader][Read]read().(&mut self, mut out: &mut [u8]) -> 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#<usize> { |
| 44 | let mut written = 0; |
| 45 | while !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().out.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]is_empty().() { |
| 46 | if self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..is_emptyrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]is_empty().() { |
| 47 | match self.channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#channel..recvrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`Receiver<T>`]recv().() { |
| 48 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf)) => self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf. = buf, |
| 49 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err)) => return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err), |
| 50 | Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => break, |
| 51 | } |
| 52 | } |
| 53 | let bytes_to_write = self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..lenrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]len().().minrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Ord#min().(out.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()); |
| 54 | let (to_write, rest) = out.split_at_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]split_at_mut().(bytes_to_write); |
| 55 | self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf..split_torust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]split_to().(bytes_to_write).copy_to_slicerust-analyzer cargo bytes 1.11.1 buf/buf_impl/Buf#copy_to_slice().(to_write); |
| 56 | out = rest; |
| 57 | written rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().bytes_to_write; |
| 58 | } |
| 59 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(written) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/Write# for Writerrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer# { |
| 64 | fn writerust-analyzer cargo gix-features 0.48.0 io/pipe/impl#[Writer][Write]write().(&mut self, buf: &[u8]) -> 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#<usize> { |
| 65 | self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#buf..put_slicerust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut][BufMut]put_slice().(buf); |
| 66 | self.channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#channel. |
| 67 | .sendrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/impl#[`SyncSender<T>`]send().(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(self.bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#buf..splitrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]split().())) |
| 68 | .map_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]map_err().(|err| iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::Errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Error#::newrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/impl#[Error]new().(iorust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/::ErrorKindrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#::BrokenPiperust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/ErrorKind#BrokenPipe#, err))?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 69 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()) |
| 70 | } |
| 71 | |
| 72 | fn flushrust-analyzer cargo gix-features 0.48.0 io/pipe/impl#[Writer][Write]flush().(&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#<()> { |
| 73 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | /// Returns the _([`write`][Writer], [`read`][Reader])_ ends of a pipe for transferring bytes, analogous to a unix pipe. |
| 78 | /// |
| 79 | /// * `in_flight_writes` defines the amount of chunks of bytes to keep in memory until the `write` end will block when writing. |
| 80 | /// If `0`, the `write` end will always block until the `read` end consumes the transferred bytes. |
| 81 | pub fn unidirectionalrust-analyzer cargo gix-features 0.48.0 io/pipe/unidirectional().(in_flight_writes: usize) -> (Writerrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#, Readerrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#) { |
| 82 | let (tx, rx) = stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::mpscrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/::sync_channelrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/mpsc/sync_channel().(in_flight_writes); |
| 83 | ( |
| 84 | Writerrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer# { |
| 85 | channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#channel.: tx, |
| 86 | bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Writer#buf.: BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#::with_capacityrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]with_capacity().(4096), |
| 87 | }, |
| 88 | Readerrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader# { |
| 89 | channelrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#channel.: rx, |
| 90 | bufrust-analyzer cargo gix-features 0.48.0 io/pipe/Reader#buf.: BytesMutrust-analyzer cargo bytes 1.11.1 bytes_mut/BytesMut#::newrust-analyzer cargo bytes 1.11.1 bytes_mut/impl#[BytesMut]new().(), |
| 91 | }, |
| 92 | ) |
| 93 | } |
| 94 | } |