codec: update to use std-future (#1214)

Strategy was to

- copy the old codec code that was temporarily being stashed in `tokio-io`
- modify all the type signatures to use Pin, as literal a translation as possible
- fix up the tests likewise

This is intended just to get things compiling and passing tests. Beyond that there is surely
lots of refactoring that can be done to make things more idiomatic. The docs are unchanged.

Closes #1189
This commit is contained in:
jesskfullwood
2019-06-27 10:10:29 -07:00
committed by Carl Lerche
parent ed4d4a5353
commit 6b9e7bdace
16 changed files with 1256 additions and 100 deletions
+22 -9
View File
@@ -1,11 +1,16 @@
#![deny(warnings, rust_2018_idioms)]
use bytes::{Buf, BufMut, BytesMut, IntoBuf};
use futures::{Future, Stream};
use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio_codec::{Decoder, Encoder, Framed, FramedParts};
use tokio_current_thread::block_on_all;
use tokio_io::AsyncRead;
use bytes::{Buf, BufMut, BytesMut, IntoBuf};
use futures::prelude::{FutureExt, StreamExt};
const INITIAL_CAPACITY: usize = 8 * 1024;
/// Encode and decode u32 values.
@@ -49,7 +54,15 @@ impl Read for DontReadIntoThis {
}
}
impl AsyncRead for DontReadIntoThis {}
impl AsyncRead for DontReadIntoThis {
fn poll_read(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
_buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unreachable!()
}
}
#[test]
fn can_read_from_existing_buf() {
@@ -58,12 +71,12 @@ fn can_read_from_existing_buf() {
let framed = Framed::from_parts(parts);
let num = framed
.into_future()
.map(|(first_num, _)| first_num.unwrap())
.wait()
.map_err(|e| e.0)
.unwrap();
let num = block_on_all(
framed
.into_future()
.map(|(first_num, _)| first_num.unwrap()),
)
.unwrap();
assert_eq!(num, 42);
}