1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-30 16:34:36 +01:00

cleanup Unpin constraint; simplify Framed impl

This commit is contained in:
Nikolay Kim 2019-11-19 14:51:40 +06:00
parent 617e40a7e9
commit 3bf83c1d98
30 changed files with 493 additions and 1252 deletions

View File

@ -19,8 +19,8 @@ path = "src/lib.rs"
[dependencies]
bytes = "0.4.12"
pin-utils = "0.1.0-alpha.4"
futures = "0.3.1"
pin-project = "0.4.5"
tokio-io = "0.2.0-alpha.6"
tokio-codec = "0.2.0-alpha.6"
log = "0.4"

View File

@ -1,35 +1,40 @@
#![allow(deprecated)]
use std::fmt;
use std::io::{self, Read, Write};
use std::io::{self};
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut;
use futures::{Sink, Stream};
use futures::{ready, Sink, Stream};
use pin_project::pin_project;
use tokio_codec::{Decoder, Encoder};
use tokio_io::{AsyncRead, AsyncWrite};
use super::framed_read::{framed_read2, framed_read2_with_buffer, FramedRead2};
use super::framed_write::{framed_write2, framed_write2_with_buffer, FramedWrite2};
const LW: usize = 1024;
const HW: usize = 8 * 1024;
const INITIAL_CAPACITY: usize = 8 * 1024;
/// A unified `Stream` and `Sink` interface to an underlying I/O object, using
/// the `Encoder` and `Decoder` traits to encode and decode frames.
///
/// You can create a `Framed` instance by using the `AsyncRead::framed` adapter.
#[pin_project]
pub struct Framed<T, U> {
inner: FramedRead2<FramedWrite2<Fuse<T, U>>>,
io: T,
codec: U,
eof: bool,
is_readable: bool,
read_buf: BytesMut,
write_buf: BytesMut,
write_lw: usize,
write_hw: usize,
}
pub struct Fuse<T, U>(pub T, pub U);
impl<T, U> Framed<T, U>
where
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
{
/// Provides a `Stream` and `Sink` interface for reading and writing to this
/// `Io` object, using `Decode` and `Encode` to read and write the raw data.
@ -48,17 +53,31 @@ where
/// If you want to work more directly with the streams and sink, consider
/// calling `split` on the `Framed` returned by this method, which will
/// break them into separate objects, allowing them to interact more easily.
pub fn new(inner: T, codec: U) -> Framed<T, U> {
pub fn new(io: T, codec: U) -> Framed<T, U> {
Framed {
inner: framed_read2(framed_write2(Fuse(inner, codec), LW, HW)),
io,
codec,
eof: false,
is_readable: false,
read_buf: BytesMut::with_capacity(INITIAL_CAPACITY),
write_buf: BytesMut::with_capacity(HW),
write_lw: LW,
write_hw: HW,
}
}
/// Same as `Framed::new()` with ability to specify write buffer low/high capacity watermarks.
pub fn new_with_caps(inner: T, codec: U, lw: usize, hw: usize) -> Framed<T, U> {
pub fn new_with_caps(io: T, codec: U, lw: usize, hw: usize) -> Framed<T, U> {
debug_assert!((lw < hw) && hw != 0);
Framed {
inner: framed_read2(framed_write2(Fuse(inner, codec), lw, hw)),
io,
codec,
eof: false,
is_readable: false,
read_buf: BytesMut::with_capacity(INITIAL_CAPACITY),
write_buf: BytesMut::with_capacity(hw),
write_lw: lw,
write_hw: hw,
}
}
}
@ -87,26 +106,25 @@ impl<T, U> Framed<T, U> {
/// break them into separate objects, allowing them to interact more easily.
pub fn from_parts(parts: FramedParts<T, U>) -> Framed<T, U> {
Framed {
inner: framed_read2_with_buffer(
framed_write2_with_buffer(
Fuse(parts.io, parts.codec),
parts.write_buf,
parts.write_buf_lw,
parts.write_buf_hw,
),
parts.read_buf,
),
io: parts.io,
codec: parts.codec,
eof: false,
is_readable: false,
write_buf: parts.write_buf,
write_lw: parts.write_buf_lw,
write_hw: parts.write_buf_hw,
read_buf: parts.read_buf,
}
}
/// Returns a reference to the underlying codec.
pub fn get_codec(&self) -> &U {
&self.inner.get_ref().get_ref().1
&self.codec
}
/// Returns a mutable reference to the underlying codec.
pub fn get_codec_mut(&mut self) -> &mut U {
&mut self.inner.get_mut().get_mut().1
&mut self.codec
}
/// Returns a reference to the underlying I/O stream wrapped by
@ -116,7 +134,7 @@ impl<T, U> Framed<T, U> {
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_ref(&self) -> &T {
&self.inner.get_ref().get_ref().0
&self.io
}
/// Returns a mutable reference to the underlying I/O stream wrapped by
@ -126,17 +144,17 @@ impl<T, U> Framed<T, U> {
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner.get_mut().get_mut().0
&mut self.io
}
/// Check if write buffer is empty.
pub fn is_write_buf_empty(&self) -> bool {
self.inner.get_ref().is_empty()
self.write_buf.is_empty()
}
/// Check if write buffer is full.
pub fn is_write_buf_full(&self) -> bool {
self.inner.get_ref().is_full()
self.write_buf.len() >= self.write_hw
}
/// Consumes the `Frame`, returning its underlying I/O stream.
@ -145,19 +163,20 @@ impl<T, U> Framed<T, U> {
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn into_inner(self) -> T {
self.inner.into_inner().into_inner().0
self.io
}
/// Consume the `Frame`, returning `Frame` with different codec.
pub fn into_framed<U2>(self, codec: U2) -> Framed<T, U2> {
let (inner, read_buf) = self.inner.into_parts();
let (inner, write_buf, lw, hw) = inner.into_parts();
Framed {
inner: framed_read2_with_buffer(
framed_write2_with_buffer(Fuse(inner.0, codec), write_buf, lw, hw),
read_buf,
),
io: self.io,
codec,
eof: self.eof,
is_readable: self.is_readable,
read_buf: self.read_buf,
write_buf: self.write_buf,
write_lw: self.write_lw,
write_hw: self.write_hw,
}
}
@ -166,14 +185,15 @@ impl<T, U> Framed<T, U> {
where
F: Fn(T) -> T2,
{
let (inner, read_buf) = self.inner.into_parts();
let (inner, write_buf, lw, hw) = inner.into_parts();
Framed {
inner: framed_read2_with_buffer(
framed_write2_with_buffer(Fuse(f(inner.0), inner.1), write_buf, lw, hw),
read_buf,
),
io: f(self.io),
codec: self.codec,
eof: self.eof,
is_readable: self.is_readable,
read_buf: self.read_buf,
write_buf: self.write_buf,
write_lw: self.write_lw,
write_hw: self.write_hw,
}
}
@ -182,14 +202,15 @@ impl<T, U> Framed<T, U> {
where
F: Fn(U) -> U2,
{
let (inner, read_buf) = self.inner.into_parts();
let (inner, write_buf, lw, hw) = inner.into_parts();
Framed {
inner: framed_read2_with_buffer(
framed_write2_with_buffer(Fuse(inner.0, f(inner.1)), write_buf, lw, hw),
read_buf,
),
io: self.io,
codec: f(self.codec),
eof: self.eof,
is_readable: self.is_readable,
read_buf: self.read_buf,
write_buf: self.write_buf,
write_lw: self.write_lw,
write_hw: self.write_hw,
}
}
@ -200,16 +221,13 @@ impl<T, U> Framed<T, U> {
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn into_parts(self) -> FramedParts<T, U> {
let (inner, read_buf) = self.inner.into_parts();
let (inner, write_buf, write_buf_lw, write_buf_hw) = inner.into_parts();
FramedParts {
io: inner.0,
codec: inner.1,
read_buf,
write_buf,
write_buf_lw,
write_buf_hw,
io: self.io,
codec: self.codec,
read_buf: self.read_buf,
write_buf: self.write_buf,
write_buf_lw: self.write_lw,
write_buf_hw: self.write_hw,
_priv: (),
}
}
@ -219,92 +237,168 @@ impl<T, U> Framed<T, U> {
/// Serialize item and Write to the inner buffer
pub fn write(&mut self, item: <U as Encoder>::Item) -> Result<(), <U as Encoder>::Error>
where
T: AsyncWrite + Unpin,
U: Encoder + Unpin,
T: AsyncWrite,
U: Encoder,
{
self.inner.get_mut().force_send(item)
let len = self.write_buf.len();
if len < self.write_lw {
self.write_buf.reserve(self.write_hw - len)
}
self.codec.encode(item, &mut self.write_buf)?;
Ok(())
}
pub fn is_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
where
T: AsyncWrite + Unpin,
U: Encoder + Unpin,
U::Error: From<io::Error>,
{
Pin::new(&mut self.inner.get_mut()).poll_ready(cx)
pub fn is_ready(&self) -> bool {
let len = self.write_buf.len();
len < self.write_hw
}
pub fn next_item(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<U::Item, U::Error>>>
pub fn next_item(&mut self, cx: &mut Context) -> Poll<Option<Result<U::Item, U::Error>>>
where
T: AsyncRead + Unpin,
U: Decoder + Unpin,
T: AsyncRead,
U: Decoder,
{
Pin::new(&mut self.inner).poll_next(cx)
loop {
// Repeatedly call `decode` or `decode_eof` as long as it is
// "readable". Readable is defined as not having returned `None`. If
// the upstream has returned EOF, and the decoder is no longer
// readable, it can be assumed that the decoder will never become
// readable again, at which point the stream is terminated.
if self.is_readable {
if self.eof {
match self.codec.decode_eof(&mut self.read_buf) {
Ok(Some(frame)) => return Poll::Ready(Some(Ok(frame))),
Ok(None) => return Poll::Ready(None),
Err(e) => return Poll::Ready(Some(Err(e))),
}
}
pub fn flush(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
where
T: AsyncWrite + Unpin,
U: Encoder + Unpin,
{
Pin::new(self.inner.get_mut()).poll_flush(cx)
log::trace!("attempting to decode a frame");
match self.codec.decode(&mut self.read_buf) {
Ok(Some(frame)) => {
log::trace!("frame decoded from buffer");
return Poll::Ready(Some(Ok(frame)));
}
Err(e) => return Poll::Ready(Some(Err(e))),
_ => {
// Need more data
}
}
pub fn close(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), U::Error>>
self.is_readable = false;
}
assert!(!self.eof);
// Otherwise, try to read more data and try again. Make sure we've
// got room for at least one byte to read to ensure that we don't
// get a spurious 0 that looks like EOF
self.read_buf.reserve(1);
let cnt = unsafe {
match Pin::new_unchecked(&mut self.io).poll_read_buf(cx, &mut self.read_buf) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
Poll::Ready(Ok(cnt)) => cnt,
}
};
if cnt == 0 {
self.eof = true;
}
self.is_readable = true;
}
}
pub fn flush(&mut self, cx: &mut Context) -> Poll<Result<(), U::Error>>
where
T: AsyncWrite + Unpin,
U: Encoder + Unpin,
T: AsyncWrite,
U: Encoder,
{
Pin::new(&mut self.inner.get_mut()).poll_close(cx)
log::trace!("flushing framed transport");
while !self.write_buf.is_empty() {
log::trace!("writing; remaining={}", self.write_buf.len());
let n = ready!(
unsafe { Pin::new_unchecked(&mut self.io) }.poll_write(cx, &self.write_buf)
)?;
if n == 0 {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to \
write frame to transport",
)
.into()));
}
// TODO: Add a way to `bytes` to do this w/o returning the drained
// data.
let _ = self.write_buf.split_to(n);
}
// Try flushing the underlying IO
ready!(unsafe { Pin::new_unchecked(&mut self.io) }.poll_flush(cx))?;
log::trace!("framed transport flushed");
Poll::Ready(Ok(()))
}
pub fn close(&mut self, cx: &mut Context) -> Poll<Result<(), U::Error>>
where
T: AsyncWrite,
U: Encoder,
{
ready!(unsafe { Pin::new_unchecked(&mut self.io) }.poll_flush(cx))?;
ready!(unsafe { Pin::new_unchecked(&mut self.io) }.poll_shutdown(cx))?;
Poll::Ready(Ok(()))
}
}
impl<T, U> Stream for Framed<T, U>
where
T: AsyncRead + Unpin,
U: Decoder + Unpin,
T: AsyncRead,
U: Decoder,
{
type Item = Result<U::Item, U::Error>;
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.as_mut().inner).poll_next(cx)
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
self.next_item(cx)
}
}
impl<T, U> Sink<U::Item> for Framed<T, U>
where
T: AsyncWrite + Unpin,
U: Encoder + Unpin,
T: AsyncWrite,
U: Encoder,
U::Error: From<io::Error>,
{
type Error = U::Error;
fn poll_ready(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.as_mut().inner.get_mut()).poll_ready(cx)
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
if self.is_ready() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
fn start_send(
mut self: Pin<&mut Self>,
item: <U as Encoder>::Item,
) -> Result<(), Self::Error> {
Pin::new(&mut self.as_mut().inner.get_mut()).start_send(item)
self.write(item)
}
fn poll_flush(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.as_mut().inner.get_mut()).poll_flush(cx)
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.flush(cx)
}
fn poll_close(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.as_mut().inner.get_mut()).poll_close(cx)
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
self.close(cx)
}
}
@ -315,84 +409,12 @@ where
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Framed")
.field("io", &self.inner.get_ref().get_ref().0)
.field("codec", &self.inner.get_ref().get_ref().1)
.field("io", &self.io)
.field("codec", &self.codec)
.finish()
}
}
// ===== impl Fuse =====
impl<T: Read, U> Read for Fuse<T, U> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
self.0.read(dst)
}
}
impl<T: AsyncRead, U> AsyncRead for Fuse<T, U> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.0.prepare_uninitialized_buffer(buf)
}
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_read(cx, buf) }
}
}
impl<T: Write, U> Write for Fuse<T, U> {
fn write(&mut self, src: &[u8]) -> io::Result<usize> {
self.0.write(src)
}
fn flush(&mut self) -> io::Result<()> {
self.0.flush()
}
}
impl<T: AsyncWrite, U> AsyncWrite for Fuse<T, U> {
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_write(cx, buf) }
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_flush(cx) }
}
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<io::Result<()>> {
unsafe { self.map_unchecked_mut(|s| &mut s.0).poll_shutdown(cx) }
}
}
impl<T, U: Decoder> Decoder for Fuse<T, U> {
type Item = U::Item;
type Error = U::Error;
fn decode(&mut self, buffer: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.1.decode(buffer)
}
fn decode_eof(&mut self, buffer: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
self.1.decode_eof(buffer)
}
}
impl<T, U: Encoder> Encoder for Fuse<T, U> {
type Item = U::Item;
type Error = U::Error;
fn encode(&mut self, item: Self::Item, dst: &mut BytesMut) -> Result<(), Self::Error> {
self.1.encode(item, dst)
}
}
/// `FramedParts` contains an export of the data of a Framed transport.
/// It can be used to construct a new `Framed` with a different codec.
/// It contains all current buffers and the inner transport.

View File

@ -1,248 +0,0 @@
use std::fmt;
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut;
use futures::{Sink, Stream};
use log::trace;
use tokio_codec::Decoder;
use tokio_io::AsyncRead;
use super::framed::Fuse;
/// A `Stream` of messages decoded from an `AsyncRead`.
pub struct FramedRead<T, D> {
inner: FramedRead2<Fuse<T, D>>,
}
pub struct FramedRead2<T> {
inner: T,
eof: bool,
is_readable: bool,
buffer: BytesMut,
}
const INITIAL_CAPACITY: usize = 8 * 1024;
// ===== impl FramedRead =====
impl<T, D> FramedRead<T, D>
where
T: AsyncRead,
D: Decoder,
{
/// Creates a new `FramedRead` with the given `decoder`.
pub fn new(inner: T, decoder: D) -> FramedRead<T, D> {
FramedRead {
inner: framed_read2(Fuse(inner, decoder)),
}
}
}
impl<T, D> FramedRead<T, D> {
/// Returns a reference to the underlying I/O stream wrapped by
/// `FramedRead`.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_ref(&self) -> &T {
&self.inner.inner.0
}
/// Returns a mutable reference to the underlying I/O stream wrapped by
/// `FramedRead`.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner.inner.0
}
/// Consumes the `FramedRead`, returning its underlying I/O stream.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn into_inner(self) -> T {
self.inner.inner.0
}
/// Returns a reference to the underlying decoder.
pub fn decoder(&self) -> &D {
&self.inner.inner.1
}
/// Returns a mutable reference to the underlying decoder.
pub fn decoder_mut(&mut self) -> &mut D {
&mut self.inner.inner.1
}
}
impl<T, D> Stream for FramedRead<T, D>
where
T: AsyncRead,
D: Decoder,
{
type Item = Result<D::Item, D::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_next(cx) }
}
}
impl<I, T, D> Sink<I> for FramedRead<T, D>
where
T: Sink<I>,
{
type Error = T::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe {
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
.poll_ready(cx)
}
}
fn start_send(self: Pin<&mut Self>, item: I) -> Result<(), Self::Error> {
unsafe {
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
.start_send(item)
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe {
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
.poll_flush(cx)
}
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe {
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
.poll_close(cx)
}
}
}
impl<T, D> fmt::Debug for FramedRead<T, D>
where
T: fmt::Debug,
D: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("FramedRead")
.field("inner", &self.inner.inner.0)
.field("decoder", &self.inner.inner.1)
.field("eof", &self.inner.eof)
.field("is_readable", &self.inner.is_readable)
.field("buffer", &self.inner.buffer)
.finish()
}
}
// ===== impl FramedRead2 =====
pub fn framed_read2<T>(inner: T) -> FramedRead2<T> {
FramedRead2 {
inner,
eof: false,
is_readable: false,
buffer: BytesMut::with_capacity(INITIAL_CAPACITY),
}
}
pub fn framed_read2_with_buffer<T>(inner: T, mut buf: BytesMut) -> FramedRead2<T> {
if buf.capacity() < INITIAL_CAPACITY {
let bytes_to_reserve = INITIAL_CAPACITY - buf.capacity();
buf.reserve(bytes_to_reserve);
}
FramedRead2 {
inner,
eof: false,
is_readable: !buf.is_empty(),
buffer: buf,
}
}
impl<T> FramedRead2<T> {
pub fn get_ref(&self) -> &T {
&self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
pub fn into_parts(self) -> (T, BytesMut) {
(self.inner, self.buffer)
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
}
impl<T> Stream for FramedRead2<T>
where
T: tokio_io::AsyncRead + Decoder,
{
type Item = Result<T::Item, T::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = unsafe { self.get_unchecked_mut() };
loop {
// Repeatedly call `decode` or `decode_eof` as long as it is
// "readable". Readable is defined as not having returned `None`. If
// the upstream has returned EOF, and the decoder is no longer
// readable, it can be assumed that the decoder will never become
// readable again, at which point the stream is terminated.
if this.is_readable {
if this.eof {
match this.inner.decode_eof(&mut this.buffer) {
Ok(Some(frame)) => return Poll::Ready(Some(Ok(frame))),
Ok(None) => return Poll::Ready(None),
Err(e) => return Poll::Ready(Some(Err(e))),
}
}
trace!("attempting to decode a frame");
match this.inner.decode(&mut this.buffer) {
Ok(Some(frame)) => {
trace!("frame decoded from buffer");
return Poll::Ready(Some(Ok(frame)));
}
Err(e) => return Poll::Ready(Some(Err(e))),
_ => {
// Need more data
}
}
this.is_readable = false;
}
assert!(!this.eof);
// Otherwise, try to read more data and try again. Make sure we've
// got room for at least one byte to read to ensure that we don't
// get a spurious 0 that looks like EOF
this.buffer.reserve(1);
let cnt = unsafe {
match Pin::new_unchecked(&mut this.inner).poll_read_buf(cx, &mut this.buffer) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Err(e)) => return Poll::Ready(Some(Err(e.into()))),
Poll::Ready(Ok(cnt)) => cnt,
}
};
if cnt == 0 {
this.eof = true;
}
this.is_readable = true;
}
}
}

View File

@ -1,333 +0,0 @@
use std::fmt;
use std::io::{self, Read};
use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::BytesMut;
use futures::{ready, Sink, Stream};
use log::trace;
use tokio_codec::{Decoder, Encoder};
use tokio_io::{AsyncRead, AsyncWrite};
use super::framed::Fuse;
/// A `Sink` of frames encoded to an `AsyncWrite`.
pub struct FramedWrite<T, E> {
inner: FramedWrite2<Fuse<T, E>>,
}
pub struct FramedWrite2<T> {
inner: T,
buffer: BytesMut,
low_watermark: usize,
high_watermark: usize,
}
impl<T, E> FramedWrite<T, E>
where
T: AsyncWrite,
E: Encoder,
{
/// Creates a new `FramedWrite` with the given `encoder`.
pub fn new(inner: T, encoder: E, lw: usize, hw: usize) -> FramedWrite<T, E> {
FramedWrite {
inner: framed_write2(Fuse(inner, encoder), lw, hw),
}
}
}
impl<T, E> FramedWrite<T, E> {
/// Returns a reference to the underlying I/O stream wrapped by
/// `FramedWrite`.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_ref(&self) -> &T {
&self.inner.inner.0
}
/// Returns a mutable reference to the underlying I/O stream wrapped by
/// `FramedWrite`.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner.inner.0
}
/// Consumes the `FramedWrite`, returning its underlying I/O stream.
///
/// Note that care should be taken to not tamper with the underlying stream
/// of data coming in as it may corrupt the stream of frames otherwise
/// being worked with.
pub fn into_inner(self) -> T {
self.inner.inner.0
}
/// Returns a reference to the underlying decoder.
pub fn encoder(&self) -> &E {
&self.inner.inner.1
}
/// Returns a mutable reference to the underlying decoder.
pub fn encoder_mut(&mut self) -> &mut E {
&mut self.inner.inner.1
}
/// Check if write buffer is full
pub fn is_full(&self) -> bool {
self.inner.is_full()
}
/// Check if write buffer is empty.
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
}
impl<T, E> FramedWrite<T, E>
where
E: Encoder,
{
/// Force send item
pub fn force_send(&mut self, item: E::Item) -> Result<(), E::Error> {
self.inner.force_send(item)
}
}
impl<T, E> Sink<E::Item> for FramedWrite<T, E>
where
T: AsyncWrite,
E: Encoder,
{
type Error = E::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_ready(cx) }
}
fn start_send(self: Pin<&mut Self>, item: <E as Encoder>::Item) -> Result<(), Self::Error> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).start_send(item) }
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_flush(cx) }
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_close(cx) }
}
}
impl<T, D> Stream for FramedWrite<T, D>
where
T: Stream,
{
type Item = T::Item;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
unsafe {
self.map_unchecked_mut(|s| &mut s.inner.inner.0)
.poll_next(cx)
}
}
}
impl<T, U> fmt::Debug for FramedWrite<T, U>
where
T: fmt::Debug,
U: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("FramedWrite")
.field("inner", &self.inner.get_ref().0)
.field("encoder", &self.inner.get_ref().1)
.field("buffer", &self.inner.buffer)
.finish()
}
}
// ===== impl FramedWrite2 =====
pub fn framed_write2<T>(
inner: T,
low_watermark: usize,
high_watermark: usize,
) -> FramedWrite2<T> {
FramedWrite2 {
inner,
low_watermark,
high_watermark,
buffer: BytesMut::with_capacity(high_watermark),
}
}
pub fn framed_write2_with_buffer<T>(
inner: T,
mut buffer: BytesMut,
low_watermark: usize,
high_watermark: usize,
) -> FramedWrite2<T> {
if buffer.capacity() < high_watermark {
let bytes_to_reserve = high_watermark - buffer.capacity();
buffer.reserve(bytes_to_reserve);
}
FramedWrite2 {
inner,
buffer,
low_watermark,
high_watermark,
}
}
impl<T> FramedWrite2<T> {
pub fn get_ref(&self) -> &T {
&self.inner
}
pub fn into_inner(self) -> T {
self.inner
}
pub fn into_parts(self) -> (T, BytesMut, usize, usize) {
(
self.inner,
self.buffer,
self.low_watermark,
self.high_watermark,
)
}
pub fn get_mut(&mut self) -> &mut T {
&mut self.inner
}
pub fn is_full(&self) -> bool {
self.buffer.len() >= self.high_watermark
}
pub fn is_empty(&self) -> bool {
self.buffer.is_empty()
}
}
impl<T> FramedWrite2<T>
where
T: Encoder,
{
pub fn force_send(&mut self, item: T::Item) -> Result<(), T::Error> {
let len = self.buffer.len();
if len < self.low_watermark {
self.buffer.reserve(self.high_watermark - len)
}
self.inner.encode(item, &mut self.buffer)?;
Ok(())
}
}
impl<T> Sink<T::Item> for FramedWrite2<T>
where
T: AsyncWrite + Encoder,
{
type Error = T::Error;
fn poll_ready(self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let len = self.buffer.len();
if len >= self.high_watermark {
Poll::Pending
} else {
Poll::Ready(Ok(()))
}
}
fn start_send(self: Pin<&mut Self>, item: <T as Encoder>::Item) -> Result<(), Self::Error> {
let this = unsafe { self.get_unchecked_mut() };
// Check the buffer capacity
let len = this.buffer.len();
if len < this.low_watermark {
this.buffer.reserve(this.high_watermark - len)
}
this.inner.encode(item, &mut this.buffer)?;
Ok(())
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let this = unsafe { self.get_unchecked_mut() };
trace!("flushing framed transport");
while !this.buffer.is_empty() {
trace!("writing; remaining={}", this.buffer.len());
let n = ready!(
unsafe { Pin::new_unchecked(&mut this.inner) }.poll_write(cx, &this.buffer)
)?;
if n == 0 {
return Poll::Ready(Err(io::Error::new(
io::ErrorKind::WriteZero,
"failed to \
write frame to transport",
)
.into()));
}
// TODO: Add a way to `bytes` to do this w/o returning the drained
// data.
let _ = this.buffer.split_to(n);
}
// Try flushing the underlying IO
ready!(unsafe { Pin::new_unchecked(&mut this.inner) }.poll_flush(cx))?;
trace!("framed transport flushed");
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
let mut this = unsafe { self.get_unchecked_mut() };
ready!(
unsafe { Pin::new_unchecked(&mut this).map_unchecked_mut(|s| *s) }.poll_flush(cx)
)?;
ready!(unsafe { Pin::new_unchecked(&mut this.inner) }.poll_shutdown(cx))?;
Poll::Ready(Ok(()))
}
}
impl<T: Decoder> Decoder for FramedWrite2<T> {
type Item = T::Item;
type Error = T::Error;
fn decode(&mut self, src: &mut BytesMut) -> Result<Option<T::Item>, T::Error> {
self.inner.decode(src)
}
fn decode_eof(&mut self, src: &mut BytesMut) -> Result<Option<T::Item>, T::Error> {
self.inner.decode_eof(src)
}
}
impl<T: Read> Read for FramedWrite2<T> {
fn read(&mut self, dst: &mut [u8]) -> io::Result<usize> {
self.inner.read(dst)
}
}
impl<T: AsyncRead> AsyncRead for FramedWrite2<T> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [u8]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
unsafe { self.map_unchecked_mut(|s| &mut s.inner).poll_read(cx, buf) }
}
}

View File

@ -12,13 +12,9 @@
mod bcodec;
mod framed;
mod framed_read;
mod framed_write;
pub use self::bcodec::BytesCodec;
pub use self::framed::{Framed, FramedParts};
pub use self::framed_read::FramedRead;
pub use self::framed_write::FramedWrite;
pub use tokio_codec::{Decoder, Encoder};
pub use tokio_io::{AsyncRead, AsyncWrite};

View File

@ -18,7 +18,7 @@ pub struct ConnectServiceFactory<T> {
resolver: ResolverFactory<T>,
}
impl<T: Unpin> ConnectServiceFactory<T> {
impl<T> ConnectServiceFactory<T> {
/// Construct new ConnectService factory
pub fn new() -> Self {
ConnectServiceFactory {
@ -70,7 +70,7 @@ impl<T> Clone for ConnectServiceFactory<T> {
}
}
impl<T: Address + Unpin> ServiceFactory for ConnectServiceFactory<T> {
impl<T: Address> ServiceFactory for ConnectServiceFactory<T> {
type Request = Connect<T>;
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
@ -90,7 +90,7 @@ pub struct ConnectService<T> {
resolver: Resolver<T>,
}
impl<T: Address + Unpin> Service for ConnectService<T> {
impl<T: Address> Service for ConnectService<T> {
type Request = Connect<T>;
type Response = Connection<T, TcpStream>;
type Error = ConnectError;
@ -108,12 +108,12 @@ impl<T: Address + Unpin> Service for ConnectService<T> {
}
}
enum ConnectState<T: Address + Unpin> {
enum ConnectState<T: Address> {
Resolve(<Resolver<T> as Service>::Future),
Connect(<TcpConnector<T> as Service>::Future),
}
impl<T: Address + Unpin> ConnectState<T> {
impl<T: Address> ConnectState<T> {
fn poll(
&mut self,
cx: &mut Context,
@ -129,12 +129,12 @@ impl<T: Address + Unpin> ConnectState<T> {
}
}
pub struct ConnectServiceResponse<T: Address + Unpin> {
pub struct ConnectServiceResponse<T: Address> {
state: ConnectState<T>,
tcp: TcpConnector<T>,
}
impl<T: Address + Unpin> Future for ConnectServiceResponse<T> {
impl<T: Address> Future for ConnectServiceResponse<T> {
type Output = Result<Connection<T, TcpStream>, ConnectError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
@ -159,7 +159,7 @@ pub struct TcpConnectService<T> {
resolver: Resolver<T>,
}
impl<T: Address + Unpin + 'static> Service for TcpConnectService<T> {
impl<T: Address + 'static> Service for TcpConnectService<T> {
type Request = Connect<T>;
type Response = TcpStream;
type Error = ConnectError;
@ -177,12 +177,12 @@ impl<T: Address + Unpin + 'static> Service for TcpConnectService<T> {
}
}
enum TcpConnectState<T: Address + Unpin> {
enum TcpConnectState<T: Address> {
Resolve(<Resolver<T> as Service>::Future),
Connect(<TcpConnector<T> as Service>::Future),
}
impl<T: Address + Unpin> TcpConnectState<T> {
impl<T: Address> TcpConnectState<T> {
fn poll(
&mut self,
cx: &mut Context,
@ -206,12 +206,12 @@ impl<T: Address + Unpin> TcpConnectState<T> {
}
}
pub struct TcpConnectServiceResponse<T: Address + Unpin> {
pub struct TcpConnectServiceResponse<T: Address> {
state: TcpConnectState<T>,
tcp: TcpConnector<T>,
}
impl<T: Address + Unpin> Future for TcpConnectServiceResponse<T> {
impl<T: Address> Future for TcpConnectServiceResponse<T> {
type Output = Result<TcpStream, ConnectError>;
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {

View File

@ -33,7 +33,7 @@ impl<T, U> OpensslConnector<T, U> {
impl<T, U> OpensslConnector<T, U>
where
T: Address + Unpin + 'static,
T: Address + 'static,
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug + 'static,
{
pub fn service(connector: SslConnector) -> OpensslConnectorService<T, U> {
@ -154,7 +154,7 @@ pub struct OpensslConnectServiceFactory<T> {
openssl: OpensslConnector<T, TcpStream>,
}
impl<T: Unpin> OpensslConnectServiceFactory<T> {
impl<T> OpensslConnectServiceFactory<T> {
/// Construct new OpensslConnectService factory
pub fn new(connector: SslConnector) -> Self {
OpensslConnectServiceFactory {

View File

@ -30,7 +30,7 @@ impl<T, U> RustlsConnector<T, U> {
impl<T, U> RustlsConnector<T, U>
where
T: Address + Unpin,
T: Address,
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
{
pub fn service(connector: Arc<ClientConfig>) -> RustlsConnectorService<T, U> {
@ -50,7 +50,7 @@ impl<T, U> Clone for RustlsConnector<T, U> {
}
}
impl<T: Address + Unpin, U> ServiceFactory for RustlsConnector<T, U>
impl<T: Address, U> ServiceFactory for RustlsConnector<T, U>
where
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
{
@ -84,7 +84,7 @@ impl<T, U> Clone for RustlsConnectorService<T, U> {
}
}
impl<T: Address + Unpin, U> Service for RustlsConnectorService<T, U>
impl<T: Address, U> Service for RustlsConnectorService<T, U>
where
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
{
@ -114,7 +114,7 @@ pub struct ConnectAsyncExt<T, U> {
stream: Option<Connection<T, ()>>,
}
impl<T: Address + Unpin, U> Future for ConnectAsyncExt<T, U>
impl<T: Address, U> Future for ConnectAsyncExt<T, U>
where
U: AsyncRead + AsyncWrite + Unpin + fmt::Debug,
{

View File

@ -24,6 +24,7 @@ actix-utils = "0.5.0-alpha.1"
bytes = "0.4"
either = "1.5.2"
futures = "0.3.1"
pin-project = "0.4.5"
tokio-executor = "=0.2.0-alpha.6"
log = "0.4"

View File

@ -16,7 +16,7 @@ pub struct Connect<Io, St = (), Codec = ()> {
impl<Io> Connect<Io>
where
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
{
pub(crate) fn new(io: Io) -> Self {
Self {
@ -27,7 +27,7 @@ where
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
where
Codec: Encoder + Decoder + Unpin,
Codec: Encoder + Decoder,
{
let (tx, rx) = mpsc::channel();
let sink = Sink::new(tx);
@ -41,6 +41,7 @@ where
}
}
#[pin_project::pin_project]
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
pub(crate) state: St,
pub(crate) framed: Framed<Io, Codec>,
@ -75,41 +76,38 @@ impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
}
}
impl<Io, St, Codec> Unpin for ConnectResult<Io, St, Codec>
where
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
{
}
impl<Io, St, Codec> Stream for ConnectResult<Io, St, Codec>
where
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
{
type Item = Result<<Codec as Decoder>::Item, <Codec as Decoder>::Error>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
self.get_mut().framed.next_item(cx)
self.project().framed.next_item(cx)
}
}
impl<Io, St, Codec> futures::Sink<<Codec as Encoder>::Item> for ConnectResult<Io, St, Codec>
where
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
{
type Error = <Codec as Encoder>::Error;
fn poll_ready(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.get_mut().framed.is_ready(cx)
fn poll_ready(self: Pin<&mut Self>, _: &mut Context) -> Poll<Result<(), Self::Error>> {
if self.framed.is_ready() {
Poll::Ready(Ok(()))
} else {
Poll::Pending
}
}
fn start_send(
self: Pin<&mut Self>,
item: <Codec as Encoder>::Item,
) -> Result<(), Self::Error> {
self.get_mut().framed.write(item)
self.project().framed.write(item)
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {

View File

@ -30,13 +30,14 @@ pub(crate) enum FramedMessage<T> {
/// FramedTransport - is a future that reads frames from Framed object
/// and pass then to the service.
#[pin_project::pin_project]
pub(crate) struct FramedDispatcher<St, S, T, U>
where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Encoder + Decoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Encoder + Decoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -55,8 +56,8 @@ where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -84,7 +85,7 @@ where
}
}
enum FramedState<S: Service, U: Encoder + Decoder + Unpin> {
enum FramedState<S: Service, U: Encoder + Decoder> {
Processing,
Error(ServiceError<S::Error, U>),
FramedError(ServiceError<S::Error, U>),
@ -92,7 +93,7 @@ enum FramedState<S: Service, U: Encoder + Decoder + Unpin> {
Stopping,
}
impl<S: Service, U: Encoder + Decoder + Unpin> FramedState<S, U> {
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
match self {
FramedState::FlushAndStop(ref mut vec) => {
@ -121,25 +122,13 @@ struct FramedDispatcherInner<I, E> {
task: LocalWaker,
}
impl<St, S, T, U> Unpin for FramedDispatcher<St, S, T, U>
where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>> + Unpin,
S::Error: Unpin + 'static,
S::Future: Unpin + 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
}
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>> + Unpin,
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: Unpin + 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
S::Future: 'static,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -179,8 +168,8 @@ where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -267,8 +256,8 @@ where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -328,8 +317,8 @@ where
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{

View File

@ -8,6 +8,7 @@ use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder};
use actix_service::{IntoService, IntoServiceFactory, Service, ServiceFactory};
use either::Either;
use futures::future::{FutureExt, LocalBoxFuture};
use pin_project::project;
use crate::connect::{Connect, ConnectResult};
use crate::dispatcher::FramedDispatcher;
@ -31,9 +32,9 @@ impl<St, Codec> Builder<St, Codec> {
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
where
F: IntoService<C>,
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
Codec: Decoder + Encoder + Unpin,
Codec: Decoder + Encoder,
{
ServiceBuilder {
connect: connect.into_service(),
@ -46,7 +47,7 @@ impl<St, Codec> Builder<St, Codec> {
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
where
F: IntoServiceFactory<C>,
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
@ -54,7 +55,7 @@ impl<St, Codec> Builder<St, Codec> {
>,
C::Error: 'static,
C::Future: 'static,
Codec: Decoder + Encoder + Unpin,
Codec: Decoder + Encoder,
{
NewServiceBuilder {
connect: connect.into_factory(),
@ -73,11 +74,10 @@ pub struct ServiceBuilder<St, C, Io, Codec> {
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
where
St: 'static,
Io: AsyncRead + AsyncWrite + Unpin,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C::Future: Unpin,
Codec: Decoder + Encoder + Unpin,
Io: AsyncRead + AsyncWrite,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
@ -103,9 +103,6 @@ where
Error = C::Error,
InitError = C::Error,
> + 'static,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
{
FramedServiceImpl {
connect: self.connect,
@ -125,16 +122,15 @@ pub struct NewServiceBuilder<St, C, Io, Codec> {
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
where
St: 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
Response = ConnectResult<Io, St, Codec>,
>,
C::Error: 'static,
C::Future: Unpin + 'static,
<C::Service as Service>::Future: Unpin,
Codec: Decoder + Encoder + Unpin,
C::Future: 'static,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
@ -159,9 +155,6 @@ where
Error = C::Error,
InitError = C::Error,
> + 'static,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
{
FramedService {
connect: self.connect,
@ -182,15 +175,14 @@ pub struct FramedService<St, C, T, Io, Codec, Cfg> {
impl<St, C, T, Io, Codec, Cfg> ServiceFactory for FramedService<St, C, T, Io, Codec, Cfg>
where
St: 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
C: ServiceFactory<
Config = (),
Request = Connect<Io>,
Response = ConnectResult<Io, St, Codec>,
>,
C::Error: 'static,
C::Future: Unpin + 'static,
<C::Service as Service>::Future: Unpin,
C::Future: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
@ -198,10 +190,8 @@ where
Error = C::Error,
InitError = C::Error,
> + 'static,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Codec: Decoder + Encoder + Unpin,
<T::Service as Service>::Future: 'static,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
@ -241,10 +231,9 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
where
Io: AsyncRead + AsyncWrite + Unpin,
Io: AsyncRead + AsyncWrite,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C::Future: Unpin,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
@ -252,10 +241,8 @@ where
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Codec: Decoder + Encoder + Unpin,
<T::Service as Service>::Future: 'static,
Codec: Decoder + Encoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
@ -279,11 +266,11 @@ where
}
}
#[pin_project::pin_project]
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
where
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,
C::Future: Unpin,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
@ -291,43 +278,19 @@ where
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
#[pin]
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
}
impl<St, Io, Codec, C, T> Unpin for FramedServiceImplResponse<St, Io, Codec, C, T>
where
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Future: Unpin,
C::Error: 'static,
T: ServiceFactory<
Config = St,
Request = RequestItem<St, Codec>,
Response = ResponseItem<Codec>,
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
}
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
where
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Future: Unpin,
C::Error: 'static,
T: ServiceFactory<
Config = St,
@ -336,32 +299,33 @@ where
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
type Output = Result<(), ServiceError<C::Error, Codec>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
match this.inner.poll(cx) {
Either::Left(new) => this.inner = new,
Either::Left(new) => {
this = self.as_mut().project();
this.inner.set(new)
}
Either::Right(poll) => return poll,
};
}
}
}
#[pin_project::pin_project]
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
where
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Future: Unpin,
C::Error: 'static,
T: ServiceFactory<
Config = St,
@ -370,27 +334,24 @@ where
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
Connect(C::Future, Rc<T>, Option<Rc<dyn Fn(&mut St, bool)>>),
Connect(#[pin] C::Future, Rc<T>, Option<Rc<dyn Fn(&mut St, bool)>>),
Handler(
T::Future,
#[pin] T::Future,
Option<ConnectResult<Io, St, Codec>>,
Option<Rc<dyn Fn(&mut St, bool)>>,
),
Dispatcher(FramedDispatcher<St, T::Service, Io, Codec>),
Dispatcher(#[pin] FramedDispatcher<St, T::Service, Io, Codec>),
}
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
where
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Future: Unpin,
C::Error: 'static,
T: ServiceFactory<
Config = St,
@ -399,40 +360,37 @@ where
Error = C::Error,
InitError = C::Error,
>,
T::Future: Unpin,
T::Service: Unpin,
<T::Service as Service>::Future: Unpin + 'static,
Io: AsyncRead + AsyncWrite + Unpin,
Codec: Encoder + Decoder + Unpin,
<T::Service as Service>::Future: 'static,
Io: AsyncRead + AsyncWrite,
Codec: Encoder + Decoder,
<Codec as Encoder>::Item: 'static,
<Codec as Encoder>::Error: std::fmt::Debug,
{
#[project]
fn poll(
&mut self,
self: Pin<&mut Self>,
cx: &mut Context,
) -> Either<
FramedServiceImplResponseInner<St, Io, Codec, C, T>,
Poll<Result<(), ServiceError<C::Error, Codec>>>,
> {
match self {
FramedServiceImplResponseInner::Connect(
ref mut fut,
ref handler,
ref mut disconnect,
) => match Pin::new(fut).poll(cx) {
Poll::Ready(Ok(res)) => Either::Left(FramedServiceImplResponseInner::Handler(
#[project]
match self.project() {
FramedServiceImplResponseInner::Connect(fut, handler, disconnect) => {
match fut.poll(cx) {
Poll::Ready(Ok(res)) => {
Either::Left(FramedServiceImplResponseInner::Handler(
handler.new_service(&res.state),
Some(res),
disconnect.take(),
)),
))
}
Poll::Pending => Either::Right(Poll::Pending),
Poll::Ready(Err(e)) => Either::Right(Poll::Ready(Err(e.into()))),
},
FramedServiceImplResponseInner::Handler(
ref mut fut,
ref mut res,
ref mut disconnect,
) => match Pin::new(fut).poll(cx) {
}
}
FramedServiceImplResponseInner::Handler(fut, res, disconnect) => match fut.poll(cx)
{
Poll::Ready(Ok(handler)) => {
let res = res.take().unwrap();
Either::Left(FramedServiceImplResponseInner::Dispatcher(

View File

@ -40,9 +40,7 @@ impl<T: AsyncRead + AsyncWrite, P> Clone for OpensslAcceptor<T, P> {
}
}
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P: Unpin> ServiceFactory
for OpensslAcceptor<T, P>
{
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P> ServiceFactory for OpensslAcceptor<T, P> {
type Request = Io<T, P>;
type Response = Io<SslStream<T>, P>;
type Error = HandshakeError<T>;
@ -70,9 +68,7 @@ pub struct OpensslAcceptorService<T, P> {
io: PhantomData<(T, P)>,
}
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P: Unpin> Service
for OpensslAcceptorService<T, P>
{
impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P> Service for OpensslAcceptorService<T, P> {
type Request = Io<T, P>;
type Response = Io<SslStream<T>, P>;
type Error = HandshakeError<T>;
@ -103,7 +99,6 @@ impl<T: AsyncRead + AsyncWrite + Unpin + 'static, P: Unpin> Service
pub struct OpensslAcceptorServiceFut<T, P>
where
P: Unpin,
T: AsyncRead + AsyncWrite,
{
fut: LocalBoxFuture<'static, Result<SslStream<T>, HandshakeError<T>>>,
@ -111,7 +106,9 @@ where
_guard: CounterGuard,
}
impl<T: AsyncRead + AsyncWrite, P: Unpin> Future for OpensslAcceptorServiceFut<T, P> {
impl<T: AsyncRead + AsyncWrite + Unpin, P> Unpin for OpensslAcceptorServiceFut<T, P> {}
impl<T: AsyncRead + AsyncWrite + Unpin, P> Future for OpensslAcceptorServiceFut<T, P> {
type Output = Result<Io<SslStream<T>, P>, HandshakeError<T>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {

View File

@ -42,7 +42,7 @@ impl<T, P> Clone for RustlsAcceptor<T, P> {
}
}
impl<T: AsyncRead + AsyncWrite + Unpin, P: Unpin> ServiceFactory for RustlsAcceptor<T, P> {
impl<T: AsyncRead + AsyncWrite + Unpin, P> ServiceFactory for RustlsAcceptor<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T>, P>;
type Error = io::Error;
@ -71,7 +71,7 @@ pub struct RustlsAcceptorService<T, P> {
conns: Counter,
}
impl<T: AsyncRead + AsyncWrite + Unpin, P: Unpin> Service for RustlsAcceptorService<T, P> {
impl<T: AsyncRead + AsyncWrite + Unpin, P> Service for RustlsAcceptorService<T, P> {
type Request = Io<T, P>;
type Response = Io<TlsStream<T>, P>;
type Error = io::Error;
@ -98,14 +98,15 @@ impl<T: AsyncRead + AsyncWrite + Unpin, P: Unpin> Service for RustlsAcceptorServ
pub struct RustlsAcceptorServiceFut<T, P>
where
T: AsyncRead + AsyncWrite + Unpin,
P: Unpin,
{
fut: Accept<T>,
params: Option<P>,
_guard: CounterGuard,
}
impl<T: AsyncRead + AsyncWrite + Unpin, P: Unpin> Future for RustlsAcceptorServiceFut<T, P> {
impl<T: AsyncRead + AsyncWrite + Unpin, P> Unpin for RustlsAcceptorServiceFut<T, P> {}
impl<T: AsyncRead + AsyncWrite + Unpin, P> Future for RustlsAcceptorServiceFut<T, P> {
type Output = Result<Io<TlsStream<T>, P>, io::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {

View File

@ -24,7 +24,8 @@ path = "src/lib.rs"
[dependencies]
futures = "0.3.1"
pin-project = "0.4.5"
[dev-dependencies]
tokio = "0.2.0-alpha.5"
tokio = "0.2.0-alpha.6"
actix-rt = "0.2"

View File

@ -41,8 +41,6 @@ impl<A, B> Service for AndThenService<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -63,13 +61,16 @@ where
}
}
#[pin_project::pin_project]
pub struct AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
{
b: Cell<B>,
#[pin]
fut_b: Option<B::Future>,
#[pin]
fut_a: Option<A::Future>,
}
@ -77,8 +78,6 @@ impl<A, B> AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
AndThenServiceResponse {
@ -93,23 +92,27 @@ impl<A, B> Future for AndThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = A::Response, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Output = Result<B::Response, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.fut_b {
return Pin::new(fut).poll(cx);
if let Some(fut) = this.fut_b.as_pin_mut() {
return fut.poll(cx);
}
match Pin::new(&mut this.fut_a.as_mut().expect("Bug in actix-service")).poll(cx) {
match this
.fut_a
.as_pin_mut()
.expect("Bug in actix-service")
.poll(cx)
{
Poll::Ready(Ok(resp)) => {
let _ = this.fut_a.take();
this.fut_b = Some(this.b.get_mut().call(resp));
this = self.as_mut().project();
this.fut_a.set(None);
this.fut_b.set(Some(this.b.get_mut().call(resp)));
}
Poll::Ready(Err(e)) => return Poll::Ready(Err(e)),
Poll::Pending => return Poll::Pending,
@ -153,10 +156,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -185,12 +184,15 @@ where
}
}
#[pin_project::pin_project]
pub struct AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
a: Option<A::Service>,
@ -201,10 +203,6 @@ impl<A, B> AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
AndThenServiceFactoryResponse {
@ -216,39 +214,24 @@ where
}
}
impl<A, B> Unpin for AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
}
impl<A, B> Future for AndThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Request = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Output = Result<AndThenService<A::Service, B::Service>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if this.a.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.a = Some(service);
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
*this.a = Some(service);
}
}
if this.b.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_b).poll(cx)? {
this.b = Some(service);
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
*this.b = Some(service);
}
}
if this.a.is_some() && this.b.is_some() {

View File

@ -23,8 +23,7 @@ pub fn apply_fn_factory<T, F, R, In, Out, Err, U>(
) -> ApplyServiceFactory<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
U: IntoServiceFactory<T>,
{
@ -106,8 +105,7 @@ where
impl<T, F, R, In, Out, Err> ServiceFactory for ApplyServiceFactory<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
type Request = In;
@ -124,12 +122,14 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
#[pin]
fut: T::Future,
f: Option<F>,
r: PhantomData<(In, Out)>,
@ -150,28 +150,18 @@ where
}
}
impl<T, F, R, In, Out, Err> Unpin for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
R: Future<Output = Result<Out, Err>>,
{
}
impl<T, F, R, In, Out, Err> Future for ApplyServiceFactoryResponse<T, F, R, In, Out, Err>
where
T: ServiceFactory<Error = Err>,
T::Future: Unpin,
F: FnMut(In, &mut T::Service) -> R + Unpin + Clone,
F: FnMut(In, &mut T::Service) -> R + Clone,
R: Future<Output = Result<Out, Err>>,
{
type Output = Result<Apply<T::Service, F, R, In, Out, Err>, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Apply::new(svc, this.f.take().unwrap())))
} else {
Poll::Pending

View File

@ -11,7 +11,7 @@ pub fn apply_cfg<F, C, T, R, S, E>(srv: T, f: F) -> ApplyConfigService<F, C, T,
where
F: FnMut(&C, &mut T) -> R,
T: Service,
R: Future<Output = Result<S, E>> + Unpin,
R: Future<Output = Result<S, E>>,
S: Service,
{
ApplyConfigService {
@ -75,8 +75,7 @@ impl<F, C, T, R, S, E> ServiceFactory for ApplyConfigService<F, C, T, R, S, E>
where
F: FnMut(&C, &mut T) -> R,
T: Service,
T::Future: Unpin,
R: Future<Output = Result<S, E>> + Unpin,
R: Future<Output = Result<S, E>>,
S: Service,
{
type Config = C;
@ -86,41 +85,10 @@ where
type Service = S;
type InitError = E;
type Future = ApplyConfigServiceResponse<R, S, E>;
type Future = R;
fn new_service(&self, cfg: &C) -> Self::Future {
ApplyConfigServiceResponse {
fut: unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) },
_t: PhantomData,
}
}
}
pub struct ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>>,
S: Service,
{
fut: R,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Unpin for ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
}
impl<R, S, E> Future for ApplyConfigServiceResponse<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
type Output = Result<S, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().fut).poll(cx)
unsafe { (self.f.get_mut_unsafe())(cfg, self.srv.get_mut_unsafe()) }
}
}
@ -160,9 +128,8 @@ where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
type Config = C;
@ -186,6 +153,7 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
@ -198,56 +166,48 @@ where
cfg: C,
f: Cell<F>,
srv: Option<T::Service>,
#[pin]
srv_fut: Option<T::Future>,
#[pin]
fut: Option<R>,
_t: PhantomData<(S,)>,
}
impl<F, C, T, R, S> Unpin for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
S: Service,
{
}
impl<F, C, T, R, S> Future for ApplyConfigServiceFactoryResponse<F, C, T, R, S>
where
C: Clone,
F: FnMut(&C, &mut T::Service) -> R,
T: ServiceFactory<Config = ()>,
T::Future: Unpin,
T::InitError: From<T::Error>,
R: Future<Output = Result<S, T::InitError>> + Unpin,
R: Future<Output = Result<S, T::InitError>>,
S: Service,
{
type Output = Result<S, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.srv_fut {
match Pin::new(fut).poll(cx)? {
if let Some(fut) = this.srv_fut.as_pin_mut() {
match fut.poll(cx)? {
Poll::Pending => return Poll::Pending,
Poll::Ready(srv) => {
let _ = this.srv_fut.take();
this.srv = Some(srv);
this = self.as_mut().project();
this.srv_fut.set(None);
*this.srv = Some(srv);
continue;
}
}
}
if let Some(ref mut fut) = this.fut {
return Pin::new(fut).poll(cx);
} else if let Some(ref mut srv) = this.srv {
if let Some(fut) = this.fut.as_pin_mut() {
return fut.poll(cx);
} else if let Some(srv) = this.srv {
match srv.poll_ready(cx)? {
Poll::Ready(_) => {
this.fut = Some(this.f.get_mut()(&this.cfg, srv));
let fut = this.f.get_mut()(&this.cfg, srv);
this = self.as_mut().project();
this.fut.set(Some(fut));
continue;
}
Poll::Pending => return Poll::Pending,

View File

@ -1,6 +1,5 @@
use std::future::Future;
use std::marker::PhantomData;
use std::pin::Pin;
use std::task::{Context, Poll};
use futures::future::{ok, Ready};
@ -188,7 +187,7 @@ where
impl<F, Fut, Cfg, Srv, Err> ServiceFactory for FnServiceConfig<F, Fut, Cfg, Srv, Err>
where
F: Fn(&Cfg) -> Fut,
Fut: Future<Output = Result<Srv, Err>> + Unpin,
Fut: Future<Output = Result<Srv, Err>>,
Srv: Service,
{
type Request = Srv::Request;
@ -198,41 +197,10 @@ where
type Config = Cfg;
type Service = Srv;
type InitError = Err;
type Future = NewServiceFnConfigFut<Fut, Srv, Err>;
type Future = Fut;
fn new_service(&self, cfg: &Cfg) -> Self::Future {
NewServiceFnConfigFut {
fut: (self.f)(cfg),
_t: PhantomData,
}
}
}
pub struct NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
fut: R,
_t: PhantomData<(S,)>,
}
impl<R, S, E> Unpin for NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
}
impl<R, S, E> Future for NewServiceFnConfigFut<R, S, E>
where
R: Future<Output = Result<S, E>> + Unpin,
S: Service,
{
type Output = Result<S, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().fut).poll(cx)
(self.f)(cfg)
}
}

View File

@ -73,7 +73,7 @@ pub trait Service {
fn map<F, R>(self, f: F) -> crate::dev::Map<Self, F, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R + Unpin,
F: FnMut(Self::Response) -> R,
{
crate::dev::Map::new(self, f)
}
@ -138,7 +138,7 @@ pub trait ServiceFactory {
fn map<F, R>(self, f: F) -> crate::map::MapServiceFactory<Self, F, R>
where
Self: Sized,
F: FnMut(Self::Response) -> R + Unpin + Clone,
F: FnMut(Self::Response) -> R + Clone,
{
crate::map::MapServiceFactory::new(self, f)
}
@ -147,7 +147,7 @@ pub trait ServiceFactory {
fn map_err<F, E>(self, f: F) -> crate::map_err::MapErrServiceFactory<Self, F, E>
where
Self: Sized,
F: Fn(Self::Error) -> E + Unpin + Clone,
F: Fn(Self::Error) -> E + Clone,
{
crate::map_err::MapErrServiceFactory::new(self, f)
}
@ -156,7 +156,7 @@ pub trait ServiceFactory {
fn map_init_err<F, E>(self, f: F) -> crate::map_init_err::MapInitErr<Self, F, E>
where
Self: Sized,
F: Fn(Self::InitError) -> E + Unpin + Clone,
F: Fn(Self::InitError) -> E + Clone,
{
crate::map_init_err::MapInitErr::new(self, f)
}

View File

@ -46,8 +46,7 @@ where
impl<A, F, Response> Service for Map<A, F, Response>
where
A: Service,
A::Future: Unpin,
F: FnMut(A::Response) -> Response + Unpin + Clone,
F: FnMut(A::Response) -> Response + Clone,
{
type Request = A::Request;
type Response = Response;
@ -63,12 +62,14 @@ where
}
}
#[pin_project::pin_project]
pub struct MapFuture<A, F, Response>
where
A: Service,
F: FnMut(A::Response) -> Response,
{
f: F,
#[pin]
fut: A::Future,
}
@ -85,15 +86,14 @@ where
impl<A, F, Response> Future for MapFuture<A, F, Response>
where
A: Service,
A::Future: Unpin,
F: FnMut(A::Response) -> Response + Unpin,
F: FnMut(A::Response) -> Response,
{
type Output = Result<Response, A::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
match Pin::new(&mut this.fut).poll(cx) {
match this.fut.poll(cx) {
Poll::Ready(Ok(resp)) => Poll::Ready(Ok((this.f)(resp))),
Poll::Ready(Err(e)) => Poll::Ready(Err(e)),
Poll::Pending => Poll::Pending,
@ -113,7 +113,7 @@ impl<A, F, Res> MapServiceFactory<A, F, Res> {
pub(crate) fn new(a: A, f: F) -> Self
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
Self {
a,
@ -140,9 +140,7 @@ where
impl<A, F, Res> ServiceFactory for MapServiceFactory<A, F, Res>
where
A: ServiceFactory,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
F: FnMut(A::Response) -> Res + Unpin + Clone,
F: FnMut(A::Response) -> Res + Clone,
{
type Request = A::Request;
type Response = Res;
@ -158,11 +156,13 @@ where
}
}
#[pin_project::pin_project]
pub struct MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res,
{
#[pin]
fut: A::Future,
f: Option<F>,
}
@ -170,7 +170,7 @@ where
impl<A, F, Res> MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
fn new(fut: A::Future, f: F) -> Self {
MapServiceFuture { f: Some(f), fut }
@ -180,15 +180,14 @@ where
impl<A, F, Res> Future for MapServiceFuture<A, F, Res>
where
A: ServiceFactory,
A::Future: Unpin,
F: FnMut(A::Response) -> Res + Unpin,
F: FnMut(A::Response) -> Res,
{
type Output = Result<Map<A::Service, F, Res>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(Map::new(svc, this.f.take().unwrap())))
} else {
Poll::Pending

View File

@ -47,8 +47,7 @@ where
impl<A, F, E> Service for MapErr<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -64,21 +63,21 @@ where
}
}
#[pin_project::pin_project]
pub struct MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
f: F,
#[pin]
fut: A::Future,
}
impl<A, F, E> MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
fn new(fut: A::Future, f: F) -> Self {
MapErrFuture { f, fut }
@ -88,14 +87,13 @@ where
impl<A, F, E> Future for MapErrFuture<A, F, E>
where
A: Service,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin,
F: Fn(A::Error) -> E,
{
type Output = Result<A::Response, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
Pin::new(&mut this.fut).poll(cx).map_err(&this.f)
let this = self.project();
this.fut.poll(cx).map_err(this.f)
}
}
@ -145,9 +143,7 @@ where
impl<A, F, E> ServiceFactory for MapErrServiceFactory<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -163,11 +159,13 @@ where
}
}
#[pin_project::pin_project]
pub struct MapErrServiceFuture<A, F, E>
where
A: ServiceFactory,
F: Fn(A::Error) -> E,
{
#[pin]
fut: A::Future,
f: F,
}
@ -185,14 +183,13 @@ where
impl<A, F, E> Future for MapErrServiceFuture<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::Error) -> E + Unpin + Clone,
F: Fn(A::Error) -> E + Clone,
{
type Output = Result<MapErr<A::Service, F, E>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
if let Poll::Ready(svc) = Pin::new(&mut this.fut).poll(cx)? {
let this = self.project();
if let Poll::Ready(svc) = this.fut.poll(cx)? {
Poll::Ready(Ok(MapErr::new(svc, this.f.clone())))
} else {
Poll::Pending

View File

@ -44,8 +44,7 @@ where
impl<A, F, E> ServiceFactory for MapInitErr<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::InitError) -> E + Unpin + Clone,
F: Fn(A::InitError) -> E + Clone,
{
type Request = A::Request;
type Response = A::Response;
@ -61,12 +60,14 @@ where
}
}
#[pin_project::pin_project]
pub struct MapInitErrFuture<A, F, E>
where
A: ServiceFactory,
F: Fn(A::InitError) -> E,
{
f: F,
#[pin]
fut: A::Future,
}
@ -83,13 +84,12 @@ where
impl<A, F, E> Future for MapInitErrFuture<A, F, E>
where
A: ServiceFactory,
A::Future: Unpin,
F: Fn(A::InitError) -> E + Unpin,
F: Fn(A::InitError) -> E,
{
type Output = Result<A::Service, E>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
Pin::new(&mut this.fut).poll(cx).map_err(&this.f)
let this = self.project();
this.fut.poll(cx).map_err(this.f)
}
}

View File

@ -41,8 +41,6 @@ impl<A, B> Service for ThenService<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>, Error = A::Error>,
A::Future: Unpin,
B::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -63,13 +61,16 @@ where
}
}
#[pin_project::pin_project]
pub struct ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
{
b: Cell<B>,
#[pin]
fut_b: Option<B::Future>,
#[pin]
fut_a: Option<A::Future>,
}
@ -77,8 +78,6 @@ impl<A, B> ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A::Future: Unpin,
B::Future: Unpin,
{
fn new(a: A::Future, b: Cell<B>) -> Self {
ThenServiceResponse {
@ -93,22 +92,26 @@ impl<A, B> Future for ThenServiceResponse<A, B>
where
A: Service,
B: Service<Request = Result<A::Response, A::Error>>,
A::Future: Unpin,
B::Future: Unpin,
{
type Output = Result<B::Response, B::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
loop {
if let Some(ref mut fut) = this.fut_b {
return Pin::new(fut).poll(cx);
if let Some(fut) = this.fut_b.as_pin_mut() {
return fut.poll(cx);
}
match Pin::new(this.fut_a.as_mut().expect("Bug in actix-service")).poll(cx) {
match this
.fut_a
.as_pin_mut()
.expect("Bug in actix-service")
.poll(cx)
{
Poll::Ready(r) => {
this.fut_b = Some(this.b.get_mut().call(r));
this = self.as_mut().project();
this.fut_b.set(Some(this.b.get_mut().call(r)));
}
Poll::Pending => return Poll::Pending,
@ -148,10 +151,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Request = A::Request;
type Response = B::Response;
@ -180,6 +179,7 @@ where
}
}
#[pin_project::pin_project]
pub struct ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
@ -190,7 +190,9 @@ where
InitError = A::InitError,
>,
{
#[pin]
fut_b: B::Future,
#[pin]
fut_a: A::Future,
a: Option<A::Service>,
b: Option<B::Service>,
@ -205,10 +207,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
fn new(fut_a: A::Future, fut_b: B::Future) -> Self {
Self {
@ -220,22 +218,6 @@ where
}
}
impl<A, B> Unpin for ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
B: ServiceFactory<
Config = A::Config,
Request = Result<A::Response, A::Error>,
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
}
impl<A, B> Future for ThenServiceFactoryResponse<A, B>
where
A: ServiceFactory,
@ -245,24 +227,20 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
<A::Service as Service>::Future: Unpin,
B::Future: Unpin,
<B::Service as Service>::Future: Unpin,
{
type Output = Result<ThenService<A::Service, B::Service>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if this.a.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.a = Some(service);
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
*this.a = Some(service);
}
}
if this.b.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_b).poll(cx)? {
this.b = Some(service);
if let Poll::Ready(service) = this.fut_b.poll(cx)? {
*this.b = Some(service);
}
}
if this.a.is_some() && this.b.is_some() {

View File

@ -76,9 +76,7 @@ where
pub fn apply<T, S, U>(t: T, service: U) -> ApplyTransform<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
U: IntoServiceFactory<S>,
{
ApplyTransform::new(t, service.into_factory())
@ -116,9 +114,7 @@ impl<T, S> Clone for ApplyTransform<T, S> {
impl<T, S> ServiceFactory for ApplyTransform<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
{
type Request = T::Request;
type Response = T::Response;
@ -138,12 +134,15 @@ where
}
}
#[pin_project::pin_project]
pub struct ApplyTransformFuture<T, S>
where
S: ServiceFactory,
T: Transform<S::Service, InitError = S::InitError>,
{
#[pin]
fut_a: S::Future,
#[pin]
fut_t: Option<T::Future>,
t_cell: Rc<T>,
}
@ -151,27 +150,24 @@ where
impl<T, S> Future for ApplyTransformFuture<T, S>
where
S: ServiceFactory,
S::Future: Unpin,
T: Transform<S::Service, InitError = S::InitError>,
T::Future: Unpin,
{
type Output = Result<T::Transform, T::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.get_mut();
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut this = self.as_mut().project();
if this.fut_t.is_none() {
if let Poll::Ready(service) = Pin::new(&mut this.fut_a).poll(cx)? {
this.fut_t = Some(this.t_cell.new_transform(service));
if let Some(fut) = this.fut_t.as_pin_mut() {
return fut.poll(cx);
}
if let Poll::Ready(service) = this.fut_a.poll(cx)? {
let fut = this.t_cell.new_transform(service);
this = self.as_mut().project();
this.fut_t.set(Some(fut));
this.fut_t.as_pin_mut().unwrap().poll(cx)
} else {
return Poll::Pending;
}
}
if let Some(ref mut fut) = this.fut_t {
Pin::new(fut).poll(cx)
} else {
Poll::Pending
}
}
}

View File

@ -23,6 +23,7 @@ actix-codec = "0.2.0-alpha.1"
bytes = "0.4"
either = "1.5.2"
futures = "0.3.1"
pin-project = "0.4.5"
tokio-timer = "0.3.0-alpha.6"
tokio-executor = { version="=0.2.0-alpha.6", features=["current-thread"] }
log = "0.4"

View File

@ -83,8 +83,6 @@ where
Error = A::Error,
InitError = A::InitError,
>,
A::Future: Unpin,
B::Future: Unpin,
{
type Request = either::Either<A::Request, B::Request>;
type Response = A::Response;
@ -114,39 +112,31 @@ impl<A: Clone, B: Clone> Clone for Either<A, B> {
}
#[doc(hidden)]
#[pin_project::pin_project]
pub struct EitherNewService<A: ServiceFactory, B: ServiceFactory> {
left: Option<A::Service>,
right: Option<B::Service>,
#[pin]
left_fut: A::Future,
#[pin]
right_fut: B::Future,
}
impl<A, B> Unpin for EitherNewService<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Response = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
B::Future: Unpin,
{
}
impl<A, B> Future for EitherNewService<A, B>
where
A: ServiceFactory,
B: ServiceFactory<Response = A::Response, Error = A::Error, InitError = A::InitError>,
A::Future: Unpin,
B::Future: Unpin,
{
type Output = Result<EitherService<A::Service, B::Service>, A::InitError>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.get_mut();
let this = self.project();
if this.left.is_none() {
this.left = Some(ready!(Pin::new(&mut this.left_fut).poll(cx))?);
*this.left = Some(ready!(this.left_fut.poll(cx))?);
}
if this.right.is_none() {
this.right = Some(ready!(Pin::new(&mut this.right_fut).poll(cx))?);
*this.right = Some(ready!(this.right_fut.poll(cx))?);
}
if this.left.is_some() && this.right.is_some() {

View File

@ -77,13 +77,14 @@ type Inner<S: Service, U> = Cell<FramedTransportInner<<U as Encoder>::Item, S::E
/// FramedTransport - is a future that reads frames from Framed object
/// and pass then to the service.
#[pin_project::pin_project]
pub struct FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Encoder + Decoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Encoder + Decoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -109,11 +110,11 @@ struct FramedTransportInner<I, E> {
impl<S, T, U> FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>> + Unpin,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -165,28 +166,28 @@ where
impl<S, T, U> Future for FramedTransport<S, T, U>
where
S: Service<Request = Request<U>, Response = Response<U>> + Unpin,
S::Error: Unpin + 'static,
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: Unpin + std::fmt::Debug,
<U as Decoder>::Error: Unpin + std::fmt::Debug,
<U as Encoder>::Error: std::fmt::Debug,
<U as Decoder>::Error: std::fmt::Debug,
{
type Output = Result<(), FramedTransportError<S::Error, U>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
self.inner.get_ref().task.register(cx.waker());
let this = self.get_mut();
let this = self.project();
poll(
cx,
&mut this.service,
&mut this.state,
&mut this.framed,
&mut this.rx,
&mut this.inner,
this.service,
this.state,
this.framed,
this.rx,
this.inner,
)
}
}
@ -203,8 +204,8 @@ where
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -257,8 +258,8 @@ where
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{
@ -309,8 +310,8 @@ where
S: Service<Request = Request<U>, Response = Response<U>>,
S::Error: 'static,
S::Future: 'static,
T: AsyncRead + AsyncWrite + Unpin,
U: Decoder + Encoder + Unpin,
T: AsyncRead + AsyncWrite,
U: Decoder + Encoder,
<U as Encoder>::Item: 'static,
<U as Encoder>::Error: std::fmt::Debug,
{

View File

@ -31,7 +31,6 @@ impl Default for InFlight {
impl<S> Transform<S> for InFlight
where
S: Service,
S::Future: Unpin,
{
type Request = S::Request;
type Response = S::Response;
@ -68,7 +67,6 @@ where
impl<T> Service for InFlightService<T>
where
T: Service,
T::Future: Unpin,
{
type Request = T::Request;
type Response = T::Response;
@ -95,19 +93,18 @@ where
}
#[doc(hidden)]
#[pin_project::pin_project]
pub struct InFlightServiceResponse<T: Service> {
#[pin]
fut: T::Future,
_guard: CounterGuard,
}
impl<T: Service> Future for InFlightServiceResponse<T>
where
T::Future: Unpin,
{
impl<T: Service> Future for InFlightServiceResponse<T> {
type Output = Result<T::Response, T::Error>;
fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
Pin::new(&mut self.get_mut().fut).poll(cx)
self.project().fut.poll(cx)
}
}

View File

@ -84,7 +84,6 @@ impl<E> Clone for Timeout<E> {
impl<S, E> Transform<S> for Timeout<E>
where
S: Service,
S::Future: Unpin,
{
type Request = S::Request;
type Response = S::Response;
@ -126,7 +125,6 @@ where
impl<S> Service for TimeoutService<S>
where
S: Service,
S::Future: Unpin,
{
type Request = S::Request;
type Response = S::Response;
@ -146,8 +144,10 @@ where
}
/// `TimeoutService` response future
#[pin_project::pin_project]
#[derive(Debug)]
pub struct TimeoutServiceResponse<T: Service> {
#[pin]
fut: T::Future,
sleep: Delay,
}
@ -155,15 +155,14 @@ pub struct TimeoutServiceResponse<T: Service> {
impl<T> Future for TimeoutServiceResponse<T>
where
T: Service,
T::Future: Unpin,
{
type Output = Result<T::Response, TimeoutError<T::Error>>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this = self.get_mut();
let mut this = self.project();
// First, try polling the future
match Pin::new(&mut this.fut).poll(cx) {
match this.fut.poll(cx) {
Poll::Ready(Ok(v)) => return Poll::Ready(Ok(v)),
Poll::Ready(Err(e)) => return Poll::Ready(Err(TimeoutError::Service(e))),
Poll::Pending => {}