1
0
mirror of https://github.com/fafhrd91/actix-net synced 2024-11-24 01:11:07 +01:00

impl Stream and Sink for Connect

This commit is contained in:
Nikolay Kim 2019-06-30 22:58:23 +06:00
parent 27baf03f64
commit 9d8b3e6275
2 changed files with 90 additions and 28 deletions

View File

@ -6,49 +6,75 @@ use crate::dispatcher::FramedMessage;
use crate::sink::Sink;
pub struct Connect<Io, St = (), Codec = ()> {
io: Io,
codec: Codec,
io: IoItem<Io, Codec>,
state: St,
// rx: mpsc::UnboundedReceiver<FramedMessage<<Codec as Encoder>::Item>>,
// sink: Sink<<Codec as Encoder>::Item>,
}
enum IoItem<Io, Codec> {
Io(Io),
Framed(Framed<Io, Codec>),
}
impl<Io, C> IoItem<Io, C>
where
Io: AsyncRead + AsyncWrite,
{
fn into_codec<Codec>(self, codec: Codec) -> IoItem<Io, Codec>
where
Codec: Encoder + Decoder,
{
match self {
IoItem::Io(io) => IoItem::Framed(Framed::new(io, codec)),
IoItem::Framed(framed) => IoItem::Framed(framed.into_framed(codec)),
}
}
fn as_framed(&mut self) -> &mut Framed<Io, C>
where
C: Encoder + Decoder,
{
match self {
IoItem::Io(_) => panic!("Codec is not set"),
IoItem::Framed(ref mut framed) => framed,
}
}
fn into_framed(self) -> Framed<Io, C>
where
C: Encoder + Decoder,
{
match self {
IoItem::Io(_) => panic!("Codec is not set"),
IoItem::Framed(framed) => framed,
}
}
}
impl<Io> Connect<Io> {
pub(crate) fn new(io: Io) -> Self {
Self {
io,
codec: (),
io: IoItem::Io(io),
state: (),
}
}
}
impl<Io, S, C> Connect<Io, S, C> {
pub fn codec<Codec>(self, codec: Codec) -> Connect<Io, S, Codec> {
impl<Io, S, C> Connect<Io, S, C>
where
Io: AsyncRead + AsyncWrite,
{
pub fn codec<Codec>(self, codec: Codec) -> Connect<Io, S, Codec>
where
Codec: Encoder + Decoder,
{
Connect {
codec,
io: self.io,
io: self.io.into_codec(codec),
state: self.state,
}
}
pub fn state<St>(self, state: St) -> Connect<Io, St, C> {
Connect {
state,
io: self.io,
codec: self.codec,
}
}
pub fn state_fn<St, F>(self, f: F) -> Connect<Io, St, C>
where
F: FnOnce(&Connect<Io, S, C>) -> St,
{
Connect {
state: f(&self),
io: self.io,
codec: self.codec,
}
Connect { state, io: self.io }
}
}
@ -63,13 +89,50 @@ where
ConnectResult {
state: Cell::new(self.state),
framed: Framed::new(self.io, self.codec),
framed: self.io.into_framed(),
rx,
sink,
}
}
}
impl<Io, St, Codec> futures::Stream for Connect<Io, St, Codec>
where
Codec: Encoder + Decoder,
Io: AsyncRead + AsyncWrite,
{
type Item = <Codec as Decoder>::Item;
type Error = <Codec as Decoder>::Error;
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
self.io.as_framed().poll()
}
}
impl<Io, St, Codec> futures::Sink for Connect<Io, St, Codec>
where
Codec: Encoder + Decoder,
Io: AsyncRead + AsyncWrite,
{
type SinkItem = <Codec as Encoder>::Item;
type SinkError = <Codec as Encoder>::Error;
fn start_send(
&mut self,
item: Self::SinkItem,
) -> futures::StartSend<Self::SinkItem, Self::SinkError> {
self.io.as_framed().start_send(item)
}
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
self.io.as_framed().poll_complete()
}
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
self.io.as_framed().close()
}
}
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
pub(crate) state: Cell<St>,
pub(crate) framed: Framed<Io, Codec>,

View File

@ -193,7 +193,6 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
where
// St: 'static,
Io: AsyncRead + AsyncWrite,
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
C::Error: 'static,