1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
#[cfg(feature = "lines")]
use async_codec_lite::LinesCodec;
use async_codec_lite::{BytesMut, Decoder, Framed};
use futures_lite::future::block_on;
use futures_util::{io::AsyncRead, stream::StreamExt};
use std::{
io,
pin::Pin,
task::{Context, Poll},
};
struct MockBurstySender {
sent: bool,
}
impl AsyncRead for MockBurstySender {
fn poll_read(mut self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
const MESSAGES: &[u8] = b"one\ntwo\n";
if !self.sent && buf.len() >= MESSAGES.len() {
self.sent = true;
buf[0 .. MESSAGES.len()].clone_from_slice(MESSAGES);
Poll::Ready(Ok(MESSAGES.len()))
} else {
Poll::Pending
}
}
}
#[cfg(feature = "lines")]
#[test]
fn line_read_multi() {
let io = MockBurstySender { sent: false };
let mut framed = Framed::new(io, LinesCodec {});
let one = block_on(framed.next()).unwrap().unwrap();
assert_eq!(one, "one\n");
let two = block_on(framed.next()).unwrap().unwrap();
assert_eq!(two, "two\n");
}
struct OneByteAtATime<'a> {
input: &'a [u8],
}
impl AsyncRead for OneByteAtATime<'_> {
fn poll_read(mut self: Pin<&mut Self>, _cx: &mut Context<'_>, buf: &mut [u8]) -> Poll<io::Result<usize>> {
if self.input.is_empty() {
Poll::Ready(Ok(0))
} else {
buf[0] = self.input[0];
self.input = &self.input[1 ..];
Poll::Ready(Ok(1))
}
}
}
struct AllTheAs;
impl Decoder for AllTheAs {
type Error = io::Error;
type Item = char;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
while !src.is_empty() {
let buf = src.split_to(1);
let c = char::from(buf[0]);
if c == 'a' {
return Ok(Some(c));
}
}
Ok(None)
}
}
#[test]
fn read_few_messages() {
let string: &[u8] = b"aabbbabbbabbbabb";
let input = OneByteAtATime { input: string };
let mut framed = Framed::new(input, AllTheAs);
for _ in 0 .. 5 {
let item = block_on(framed.next()).unwrap().unwrap();
assert_eq!(item, 'a');
}
}
|