| 1 | use corerust-analyzer cargo core https://github.com/rust-lang/rust/library/core crate/::iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/::Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#; |
| 2 | use futuresrust-analyzer cargo futures 0.3.34 crate/::iorust-analyzer cargo futures-util 0.3.34 io/::{AsyncWriterust-analyzer cargo futures-io 0.3.34 if_std/AsyncWrite#, Cursorrust-analyzer cargo futures-util 0.3.34 io/cursor/Cursor#}; |
| 3 | use futuresrust-analyzer cargo futures 0.3.34 crate/::sinkrust-analyzer cargo futures-util 0.3.34 sink/::SinkExtrust-analyzer cargo futures-util 0.3.34 sink/SinkExt#; |
| 4 | use futuresrust-analyzer cargo futures 0.3.34 crate/::{executorrust-analyzer cargo futures 0.3.34 executor/, streamrust-analyzer cargo futures-util 0.3.34 stream/, streamrust-analyzer cargo futures-util 0.3.34 stream/::StreamExtrust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#}; |
| 5 | use futures_codecrust-analyzer cargo futures_codec 0.4.1 crate/::{BytesCodecrust-analyzer cargo futures_codec 0.4.1 codec/bytes/BytesCodec#, FramedWriterust-analyzer cargo futures_codec 0.4.1 framed_write/FramedWrite#, LinesCodecrust-analyzer cargo futures_codec 0.4.1 codec/lines/LinesCodec#, Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#}; |
| 6 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/::Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#; |
| 7 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::taskrust-analyzer cargo std https://github.com/rust-lang/rust/library/std task/::{Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#, Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#}; |
| 8 | |
| 9 | // An iterator which outputs a single zero byte up to limit times |
| 10 | struct ZeroBytesrust-analyzer cargo futures_codec 0.4.1 ZeroBytes# { |
| 11 | pub countrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#count.: usize, |
| 12 | pub limitrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#limit.: usize, |
| 13 | } |
| 14 | impl Iteratorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator# for ZeroBytesrust-analyzer cargo futures_codec 0.4.1 ZeroBytes# { |
| 15 | type Itemrust-analyzer cargo futures_codec 0.4.1 impl#[ZeroBytes][Iterator]Item# = Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#; |
| 16 | fn nextrust-analyzer cargo futures_codec 0.4.1 impl#[ZeroBytes][Iterator]next().(&mut self) -> Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<Selfrust-analyzer cargo futures_codec 0.4.1 impl#[ZeroBytes][Iterator]::Itemrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#Item#> { |
| 17 | if self.countrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#count. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().self.limitrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#limit. { |
| 18 | Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None# |
| 19 | } else { |
| 20 | self.countrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#count. 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().1; |
| 21 | Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(Bytesrust-analyzer cargo bytes 1.12.1 bytes/Bytes#::from_staticrust-analyzer cargo bytes 1.12.1 bytes/impl#[Bytes]from_static().(b"\0")) |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // An AsyncWrite which is always ready and just consumes the data |
| 27 | struct AsyncWriteNullrust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull# { |
| 28 | // number of poll_write calls |
| 29 | pub num_poll_writerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#num_poll_write.: usize, |
| 30 | |
| 31 | // size of the last poll_write |
| 32 | pub last_write_sizerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#last_write_size.: usize, |
| 33 | } |
| 34 | impl AsyncWriterust-analyzer cargo futures-io 0.3.34 if_std/AsyncWrite# for AsyncWriteNullrust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull# { |
| 35 | fn poll_writerust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]poll_write().( |
| 36 | mut self: Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#<&mut Selfrust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]>, |
| 37 | _cx: &mut Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#<'_>, |
| 38 | buf: &[u8], |
| 39 | ) -> Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#<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/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<usize>> { |
| 40 | self.num_poll_writerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#num_poll_write. 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().1; |
| 41 | self.last_write_sizerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#last_write_size. = buf.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(); |
| 42 | Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(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().())) |
| 43 | } |
| 44 | |
| 45 | fn poll_flushrust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]poll_flush().(self: Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#<&mut Selfrust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]>, _cx: &mut Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#<'_>) -> Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#<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/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>> { |
| 46 | Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())) |
| 47 | } |
| 48 | |
| 49 | fn poll_closerust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]poll_close().(self: Pinrust-analyzer cargo core https://github.com/rust-lang/rust/library/core pin/Pin#<&mut Selfrust-analyzer cargo futures_codec 0.4.1 impl#[AsyncWriteNull][AsyncWrite]>, _cx: &mut Contextrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/wake/Context#<'_>) -> Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#<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/::Resultrust-analyzer cargo std https://github.com/rust-lang/rust/library/std io/error/Result#<()>> { |
| 50 | Pollrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#::Readyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core task/poll/Poll#Ready#(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(())) |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 55 | fn line_writerust-analyzer cargo futures_codec 0.4.1 line_write().() { |
| 56 | let curs = Cursorrust-analyzer cargo futures-util 0.3.34 io/cursor/Cursor#::newrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]new().(vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![0u8; 16]); |
| 57 | let mut framer = FramedWriterust-analyzer cargo futures_codec 0.4.1 framed_write/FramedWrite#::newrust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]new().(curs, LinesCodecrust-analyzer cargo futures_codec 0.4.1 codec/lines/LinesCodec# {}); |
| 58 | executorrust-analyzer cargo futures 0.3.34 executor/::block_onrust-analyzer cargo futures-executor 0.3.34 local_pool/block_on().(framer.sendrust-analyzer cargo futures-util 0.3.34 sink/SinkExt#send().("Hello\n".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 59 | executorrust-analyzer cargo futures 0.3.34 executor/::block_onrust-analyzer cargo futures-executor 0.3.34 local_pool/block_on().(framer.sendrust-analyzer cargo futures-util 0.3.34 sink/SinkExt#send().("World\n".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 60 | let (curs, _) = framer.releaserust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]release().(); |
| 61 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&curs.get_refrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]get_ref().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..12]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., b"Hello\nWorld\n"); |
| 62 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(curs.positionrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]position().(), 12); |
| 63 | } |
| 64 | |
| 65 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 66 | fn line_write_to_eofrust-analyzer cargo futures_codec 0.4.1 line_write_to_eof().() { |
| 67 | let mut buf = [0u8; 16]; |
| 68 | let curs = Cursorrust-analyzer cargo futures-util 0.3.34 io/cursor/Cursor#::newrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]new().(&mut buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFull#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core array/impl#[`[T; N]`][`IndexMut<I>`]index_mut().); |
| 69 | let mut framer = FramedWriterust-analyzer cargo futures_codec 0.4.1 framed_write/FramedWrite#::newrust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]new().(curs, LinesCodecrust-analyzer cargo futures_codec 0.4.1 codec/lines/LinesCodec# {}); |
| 70 | let _err = |
| 71 | executorrust-analyzer cargo futures 0.3.34 executor/::block_onrust-analyzer cargo futures-executor 0.3.34 local_pool/block_on().(framer.sendrust-analyzer cargo futures-util 0.3.34 sink/SinkExt#send().("This will fill up the buffer\n".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().())).unwrap_errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_err().(); |
| 72 | let (curs, _) = framer.releaserust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]release().(); |
| 73 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(curs.positionrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]position().(), 16); |
| 74 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&curs.get_refrust-analyzer cargo futures-util 0.3.34 io/cursor/impl#[`Cursor<T>`]get_ref().()[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..16]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., b"This will fill u"); |
| 75 | } |
| 76 | |
| 77 | #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:] |
| 78 | fn send_high_water_markrust-analyzer cargo futures_codec 0.4.1 send_high_water_mark().() { |
| 79 | // stream will output 999 bytes, 1 at at a time, and will always be ready |
| 80 | let mut stream = streamrust-analyzer cargo futures-util 0.3.34 stream/::iterrust-analyzer cargo futures-util 0.3.34 stream/iter/iter().(ZeroBytesrust-analyzer cargo futures_codec 0.4.1 ZeroBytes# { |
| 81 | countrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#count.: 0, |
| 82 | limitrust-analyzer cargo futures_codec 0.4.1 ZeroBytes#limit.: 999, |
| 83 | }) |
| 84 | .maprust-analyzer cargo futures-util 0.3.34 stream/stream/StreamExt#map().(Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#); |
| 85 | |
| 86 | // sink will eat whatever it receives |
| 87 | let io = AsyncWriteNullrust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull# { |
| 88 | num_poll_writerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#num_poll_write.: 0, |
| 89 | last_write_sizerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#last_write_size.: 0, |
| 90 | }; |
| 91 | |
| 92 | // expect two sends |
| 93 | let mut framer = FramedWriterust-analyzer cargo futures_codec 0.4.1 framed_write/FramedWrite#::newrust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]new().(io, BytesCodecrust-analyzer cargo futures_codec 0.4.1 codec/bytes/BytesCodec# {}); |
| 94 | framer.set_send_high_water_markrust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]set_send_high_water_mark().(500); |
| 95 | executorrust-analyzer cargo futures 0.3.34 executor/::block_onrust-analyzer cargo futures-executor 0.3.34 local_pool/block_on().(framer.send_allrust-analyzer cargo futures-util 0.3.34 sink/SinkExt#send_all().(&mut stream)).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().(); |
| 96 | let (io, _) = framer.releaserust-analyzer cargo futures_codec 0.4.1 framed_write/impl#[`FramedWrite<T, E>`]release().(); |
| 97 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(io.num_poll_writerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#num_poll_write., 2); |
| 98 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(io.last_write_sizerust-analyzer cargo futures_codec 0.4.1 AsyncWriteNull#last_write_size., 499); |
| 99 | } |