2019-07-01 07:20:24 +02:00
|
|
|
use std::cell::{Ref, RefMut};
|
2019-06-26 11:19:40 +02:00
|
|
|
use std::fmt;
|
|
|
|
use std::ops::{Deref, DerefMut};
|
|
|
|
|
|
|
|
use actix_codec::{Decoder, Encoder};
|
|
|
|
|
|
|
|
use crate::sink::Sink;
|
2019-07-01 07:20:24 +02:00
|
|
|
use crate::state::State;
|
2019-06-26 11:19:40 +02:00
|
|
|
|
2019-07-01 07:20:24 +02:00
|
|
|
pub struct Item<St, Codec: Encoder + Decoder> {
|
|
|
|
state: State<St>,
|
2019-06-26 11:19:40 +02:00
|
|
|
sink: Sink<<Codec as Encoder>::Item>,
|
|
|
|
item: <Codec as Decoder>::Item,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<St, Codec> Item<St, Codec>
|
|
|
|
where
|
|
|
|
Codec: Encoder + Decoder,
|
|
|
|
{
|
|
|
|
pub(crate) fn new(
|
2019-07-01 07:20:24 +02:00
|
|
|
state: State<St>,
|
2019-06-26 11:19:40 +02:00
|
|
|
sink: Sink<<Codec as Encoder>::Item>,
|
|
|
|
item: <Codec as Decoder>::Item,
|
|
|
|
) -> Self {
|
|
|
|
Item { state, sink, item }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-01 07:20:24 +02:00
|
|
|
pub fn state(&self) -> Ref<St> {
|
2019-06-26 11:19:40 +02:00
|
|
|
self.state.get_ref()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2019-07-01 07:20:24 +02:00
|
|
|
pub fn state_mut(&mut self) -> RefMut<St> {
|
2019-06-26 11:19:40 +02:00
|
|
|
self.state.get_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
|
|
|
|
&self.sink
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn into_inner(self) -> <Codec as Decoder>::Item {
|
|
|
|
self.item
|
|
|
|
}
|
2019-07-01 07:20:24 +02:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn into_parts(
|
|
|
|
self,
|
|
|
|
) -> (
|
|
|
|
State<St>,
|
|
|
|
Sink<<Codec as Encoder>::Item>,
|
|
|
|
<Codec as Decoder>::Item,
|
|
|
|
) {
|
|
|
|
(self.state, self.sink, self.item)
|
|
|
|
}
|
2019-06-26 11:19:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<St, Codec> Deref for Item<St, Codec>
|
|
|
|
where
|
|
|
|
Codec: Encoder + Decoder,
|
|
|
|
{
|
|
|
|
type Target = <Codec as Decoder>::Item;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn deref(&self) -> &<Codec as Decoder>::Item {
|
|
|
|
&self.item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<St, Codec> DerefMut for Item<St, Codec>
|
|
|
|
where
|
|
|
|
Codec: Encoder + Decoder,
|
|
|
|
{
|
|
|
|
#[inline]
|
|
|
|
fn deref_mut(&mut self) -> &mut <Codec as Decoder>::Item {
|
|
|
|
&mut self.item
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<St, Codec> fmt::Debug for Item<St, Codec>
|
|
|
|
where
|
|
|
|
Codec: Encoder + Decoder,
|
|
|
|
<Codec as Decoder>::Item: fmt::Debug,
|
|
|
|
{
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("FramedItem").field(&self.item).finish()
|
|
|
|
}
|
|
|
|
}
|