mirror of
https://github.com/fafhrd91/actix-net
synced 2025-06-28 05:10:36 +02:00
use owned value for service factory config
This commit is contained in:
@ -17,7 +17,6 @@ use crate::cell::Cell;
|
||||
use crate::error::ServiceError;
|
||||
use crate::item::Item;
|
||||
use crate::sink::Sink;
|
||||
use crate::state::State;
|
||||
|
||||
type Request<S, U> = Item<S, U>;
|
||||
type Response<U> = <U as Encoder>::Item;
|
||||
@ -33,6 +32,7 @@ pub(crate) enum FramedMessage<T> {
|
||||
#[pin_project::pin_project]
|
||||
pub(crate) struct FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -43,7 +43,7 @@ where
|
||||
{
|
||||
service: S,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
state: State<St>,
|
||||
state: St,
|
||||
dispatch_state: FramedState<S, U>,
|
||||
framed: Framed<T, U>,
|
||||
rx: Option<mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||
@ -53,6 +53,7 @@ where
|
||||
|
||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -63,7 +64,7 @@ where
|
||||
{
|
||||
pub(crate) fn new<F: IntoService<S>>(
|
||||
framed: Framed<T, U>,
|
||||
state: State<St>,
|
||||
state: St,
|
||||
service: F,
|
||||
rx: mpsc::Receiver<FramedMessage<<U as Encoder>::Item>>,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
@ -124,6 +125,7 @@ struct FramedDispatcherInner<I, E> {
|
||||
|
||||
impl<St, S, T, U> FramedDispatcher<St, S, T, U>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -156,7 +158,7 @@ where
|
||||
fn poll<St, S, T, U>(
|
||||
cx: &mut Context,
|
||||
srv: &mut S,
|
||||
state: &mut State<St>,
|
||||
state: &mut St,
|
||||
sink: &mut Sink<<U as Encoder>::Item>,
|
||||
framed: &mut Framed<T, U>,
|
||||
dispatch_state: &mut FramedState<S, U>,
|
||||
@ -165,6 +167,7 @@ fn poll<St, S, T, U>(
|
||||
disconnect: &mut Option<Rc<dyn Fn(&mut St, bool)>>,
|
||||
) -> Poll<Result<(), ServiceError<S::Error, U>>>
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
@ -199,7 +202,7 @@ where
|
||||
|| framed.is_write_buf_empty())
|
||||
{
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), true);
|
||||
(&*disconnect)(&mut *state, true);
|
||||
}
|
||||
Poll::Ready(Err(err))
|
||||
} else {
|
||||
@ -224,19 +227,19 @@ where
|
||||
let _ = tx.send(());
|
||||
}
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), false);
|
||||
(&*disconnect)(&mut *state, false);
|
||||
}
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
FramedState::FramedError(err) => {
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), true);
|
||||
(&*disconnect)(&mut *state, true);
|
||||
}
|
||||
Poll::Ready(Err(err))
|
||||
}
|
||||
FramedState::Stopping => {
|
||||
if let Some(ref disconnect) = disconnect {
|
||||
(&*disconnect)(&mut *state.get_mut(), false);
|
||||
(&*disconnect)(&mut *state, false);
|
||||
}
|
||||
Poll::Ready(Ok(()))
|
||||
}
|
||||
@ -246,13 +249,14 @@ where
|
||||
fn poll_read<St, S, T, U>(
|
||||
cx: &mut Context,
|
||||
srv: &mut S,
|
||||
state: &mut State<St>,
|
||||
state: &mut St,
|
||||
sink: &mut Sink<<U as Encoder>::Item>,
|
||||
framed: &mut Framed<T, U>,
|
||||
dispatch_state: &mut FramedState<S, U>,
|
||||
inner: &mut Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||
) -> bool
|
||||
where
|
||||
St: Clone,
|
||||
S: Service<Request = Request<St, U>, Response = Option<Response<U>>>,
|
||||
S::Error: 'static,
|
||||
S::Future: 'static,
|
||||
|
@ -1,14 +1,12 @@
|
||||
use std::cell::{Ref, RefMut};
|
||||
use std::fmt;
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
use actix_codec::{Decoder, Encoder};
|
||||
|
||||
use crate::sink::Sink;
|
||||
use crate::state::State;
|
||||
|
||||
pub struct Item<St, Codec: Encoder + Decoder> {
|
||||
state: State<St>,
|
||||
state: St,
|
||||
sink: Sink<<Codec as Encoder>::Item>,
|
||||
item: <Codec as Decoder>::Item,
|
||||
}
|
||||
@ -18,7 +16,7 @@ where
|
||||
Codec: Encoder + Decoder,
|
||||
{
|
||||
pub(crate) fn new(
|
||||
state: State<St>,
|
||||
state: St,
|
||||
sink: Sink<<Codec as Encoder>::Item>,
|
||||
item: <Codec as Decoder>::Item,
|
||||
) -> Self {
|
||||
@ -26,13 +24,13 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state(&self) -> Ref<St> {
|
||||
self.state.get_ref()
|
||||
pub fn state(&self) -> &St {
|
||||
&self.state
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state_mut(&mut self) -> RefMut<St> {
|
||||
self.state.get_mut()
|
||||
pub fn state_mut(&mut self) -> &mut St {
|
||||
&mut self.state
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@ -46,13 +44,7 @@ where
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_parts(
|
||||
self,
|
||||
) -> (
|
||||
State<St>,
|
||||
Sink<<Codec as Encoder>::Item>,
|
||||
<Codec as Decoder>::Item,
|
||||
) {
|
||||
pub fn into_parts(self) -> (St, Sink<<Codec as Encoder>::Item>, <Codec as Decoder>::Item) {
|
||||
(self.state, self.sink, self.item)
|
||||
}
|
||||
}
|
||||
|
@ -5,11 +5,9 @@ mod error;
|
||||
mod item;
|
||||
mod service;
|
||||
mod sink;
|
||||
mod state;
|
||||
|
||||
pub use self::connect::{Connect, ConnectResult};
|
||||
pub use self::error::ServiceError;
|
||||
pub use self::item::Item;
|
||||
pub use self::service::{Builder, NewServiceBuilder, ServiceBuilder};
|
||||
pub use self::sink::Sink;
|
||||
pub use self::state::State;
|
||||
|
@ -14,7 +14,6 @@ use crate::connect::{Connect, ConnectResult};
|
||||
use crate::dispatcher::FramedDispatcher;
|
||||
use crate::error::ServiceError;
|
||||
use crate::item::Item;
|
||||
use crate::state::State;
|
||||
|
||||
type RequestItem<S, U> = Item<S, U>;
|
||||
type ResponseItem<U> = Option<<U as Encoder>::Item>;
|
||||
@ -23,7 +22,7 @@ type ResponseItem<U> = Option<<U as Encoder>::Item>;
|
||||
/// for building instances for framed services.
|
||||
pub struct Builder<St, Codec>(PhantomData<(St, Codec)>);
|
||||
|
||||
impl<St, Codec> Builder<St, Codec> {
|
||||
impl<St: Clone, Codec> Builder<St, Codec> {
|
||||
pub fn new() -> Builder<St, Codec> {
|
||||
Builder(PhantomData)
|
||||
}
|
||||
@ -73,7 +72,7 @@ pub struct ServiceBuilder<St, C, Io, Codec> {
|
||||
|
||||
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
@ -121,7 +120,7 @@ pub struct NewServiceBuilder<St, C, Io, Codec> {
|
||||
|
||||
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
St: Clone,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: ServiceFactory<
|
||||
Config = (),
|
||||
@ -174,7 +173,7 @@ 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,
|
||||
St: Clone + 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: ServiceFactory<
|
||||
Config = (),
|
||||
@ -203,13 +202,13 @@ where
|
||||
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
||||
type Future = LocalBoxFuture<'static, Result<Self::Service, Self::InitError>>;
|
||||
|
||||
fn new_service(&self, _: &Cfg) -> Self::Future {
|
||||
fn new_service(&self, _: Cfg) -> Self::Future {
|
||||
let handler = self.handler.clone();
|
||||
let disconnect = self.disconnect.clone();
|
||||
|
||||
// create connect service and then create service impl
|
||||
self.connect
|
||||
.new_service(&())
|
||||
.new_service(())
|
||||
.map(move |result| {
|
||||
result.map(move |connect| FramedServiceImpl {
|
||||
connect,
|
||||
@ -231,6 +230,7 @@ pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
||||
|
||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
||||
where
|
||||
St: Clone,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
@ -269,6 +269,7 @@ where
|
||||
#[pin_project::pin_project]
|
||||
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -290,6 +291,7 @@ where
|
||||
|
||||
impl<St, Io, Codec, C, T> Future for FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -325,6 +327,7 @@ where
|
||||
#[pin_project::pin_project]
|
||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -351,6 +354,7 @@ where
|
||||
|
||||
impl<St, Io, Codec, C, T> FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||
where
|
||||
St: Clone,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: ServiceFactory<
|
||||
@ -380,7 +384,7 @@ where
|
||||
match fut.poll(cx) {
|
||||
Poll::Ready(Ok(res)) => {
|
||||
Either::Left(FramedServiceImplResponseInner::Handler(
|
||||
handler.new_service(&res.state),
|
||||
handler.new_service(res.state.clone()),
|
||||
Some(res),
|
||||
disconnect.take(),
|
||||
))
|
||||
@ -396,7 +400,7 @@ where
|
||||
Either::Left(FramedServiceImplResponseInner::Dispatcher(
|
||||
FramedDispatcher::new(
|
||||
res.framed,
|
||||
State::new(res.state),
|
||||
res.state,
|
||||
handler,
|
||||
res.rx,
|
||||
res.sink,
|
||||
|
@ -1,30 +0,0 @@
|
||||
use std::cell::{Ref, RefCell, RefMut};
|
||||
use std::rc::Rc;
|
||||
|
||||
/// Connection state
|
||||
///
|
||||
/// Connection state is an arbitrary data attached to the each incoming message.
|
||||
#[derive(Debug)]
|
||||
pub struct State<T>(Rc<RefCell<T>>);
|
||||
|
||||
impl<T> State<T> {
|
||||
pub(crate) fn new(st: T) -> Self {
|
||||
State(Rc::new(RefCell::new(st)))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_ref(&self) -> Ref<T> {
|
||||
self.0.borrow()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self) -> RefMut<T> {
|
||||
self.0.borrow_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Clone for State<T> {
|
||||
fn clone(&self) -> Self {
|
||||
State(self.0.clone())
|
||||
}
|
||||
}
|
@ -10,6 +10,7 @@ use futures::future::ok;
|
||||
|
||||
use actix_ioframe::{Builder, Connect};
|
||||
|
||||
#[derive(Clone)]
|
||||
struct State;
|
||||
|
||||
#[actix_rt::test]
|
||||
|
Reference in New Issue
Block a user