mirror of
https://github.com/fafhrd91/actix-net
synced 2025-08-12 21:17:06 +02:00
Compare commits
41 Commits
test-serve
...
rt-0.2.4
Author | SHA1 | Date | |
---|---|---|---|
|
411e31786f | ||
|
b491d373b1 | ||
|
9271b95c87 | ||
|
1b3cd0d88c | ||
|
da302d4b7a | ||
|
922a919572 | ||
|
5a62175b6e | ||
|
5445e341c3 | ||
|
1b17d274a0 | ||
|
9d8b3e6275 | ||
|
27baf03f64 | ||
|
205cac82ce | ||
|
07708c5e9a | ||
|
1c04ad3238 | ||
|
66aa21740c | ||
|
b183cb3324 | ||
|
158482cd2f | ||
|
9e61f62871 | ||
|
7051888289 | ||
|
0caa47fc47 | ||
|
6d1cbb2d2f | ||
|
ca289ddf7f | ||
|
ad9a197916 | ||
|
c4f05e033f | ||
|
048314913c | ||
|
c1b183e1ce | ||
|
87bc3dacd9 | ||
|
0156f479a0 | ||
|
139fa3b9a2 | ||
|
a14f612382 | ||
|
059e2ad042 | ||
|
fdf2a6f422 | ||
|
fc2631c852 | ||
|
d51b210ae7 | ||
|
0a6cded975 | ||
|
14e3933d8b | ||
|
837504c10f | ||
|
802d808aca | ||
|
c7676df697 | ||
|
ecf7a11a20 | ||
|
686958fe0c |
@@ -24,15 +24,16 @@ members = [
|
||||
"actix-test-server",
|
||||
"actix-threadpool",
|
||||
"actix-tower",
|
||||
"actix-ioframe",
|
||||
"actix-utils",
|
||||
"router",
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
actix-service = "0.3.3"
|
||||
actix-service = "0.4.0"
|
||||
actix-codec = "0.1.1"
|
||||
actix-rt = "0.2.0"
|
||||
actix-server = { path="actix-server", features=["ssl"] }
|
||||
actix-server = { version="0.5.0", features=["ssl"] }
|
||||
env_logger = "0.6"
|
||||
futures = "0.1.25"
|
||||
openssl = "0.10"
|
||||
|
1
actix-codec/LICENSE-APACHE
Symbolic link
1
actix-codec/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-codec/LICENSE-MIT
Symbolic link
1
actix-codec/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -1,6 +1,17 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.0] - 2019-05-11
|
||||
## [0.2.1] - 2019-07-17
|
||||
|
||||
### Added
|
||||
|
||||
* Expose Connect addrs #30
|
||||
|
||||
### Changed
|
||||
|
||||
* Update `derive_more` to 0.15
|
||||
|
||||
|
||||
## [0.2.0] - 2019-05-12
|
||||
|
||||
### Changed
|
||||
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-connect"
|
||||
version = "0.1.5"
|
||||
version = "0.2.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Connector - tcp connector service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -30,10 +30,10 @@ ssl = ["openssl", "tokio-openssl"]
|
||||
uri = ["http"]
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.3.6"
|
||||
actix-service = "0.4.0"
|
||||
actix-codec = "0.1.2"
|
||||
actix-utils = "0.3.5"
|
||||
derive_more = "0.14.0"
|
||||
actix-utils = "0.4.0"
|
||||
derive_more = "0.15"
|
||||
either = "1.5.2"
|
||||
futures = "0.1.25"
|
||||
http = { version = "0.1.17", optional = true }
|
||||
@@ -48,7 +48,5 @@ tokio-openssl = { version="0.3", optional = true }
|
||||
|
||||
[dev-dependencies]
|
||||
bytes = "0.4"
|
||||
actix-test-server = { version="0.2.1", features=["ssl"] }
|
||||
actix-test-server = { version="0.2.2", features=["ssl"] }
|
||||
actix-server-config = "0.1.0"
|
||||
actix-utils = "0.3.4"
|
||||
tokio-tcp = "0.1"
|
1
actix-connect/LICENSE-APACHE
Symbolic link
1
actix-connect/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-connect/LICENSE-MIT
Symbolic link
1
actix-connect/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -1,5 +1,6 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::collections::{vec_deque, VecDeque};
|
||||
use std::fmt;
|
||||
use std::iter::{FromIterator, FusedIterator};
|
||||
use std::net::SocketAddr;
|
||||
|
||||
use either::Either;
|
||||
@@ -77,6 +78,20 @@ impl<T: Address> Connect<T> {
|
||||
self
|
||||
}
|
||||
|
||||
/// Use addresses.
|
||||
pub fn set_addrs<I>(mut self, addrs: I) -> Self
|
||||
where
|
||||
I: IntoIterator<Item = SocketAddr>,
|
||||
{
|
||||
let mut addrs = VecDeque::from_iter(addrs);
|
||||
self.addr = if addrs.len() < 2 {
|
||||
addrs.pop_front().map(Either::Left)
|
||||
} else {
|
||||
Some(Either::Right(addrs))
|
||||
};
|
||||
self
|
||||
}
|
||||
|
||||
/// Host name
|
||||
pub fn host(&self) -> &str {
|
||||
self.req.host()
|
||||
@@ -86,6 +101,28 @@ impl<T: Address> Connect<T> {
|
||||
pub fn port(&self) -> u16 {
|
||||
self.req.port().unwrap_or(self.port)
|
||||
}
|
||||
|
||||
/// Preresolved addresses of the request.
|
||||
pub fn addrs(&self) -> ConnectAddrsIter<'_> {
|
||||
let inner = match self.addr {
|
||||
None => Either::Left(None),
|
||||
Some(Either::Left(addr)) => Either::Left(Some(addr)),
|
||||
Some(Either::Right(ref addrs)) => Either::Right(addrs.iter()),
|
||||
};
|
||||
|
||||
ConnectAddrsIter { inner }
|
||||
}
|
||||
|
||||
/// Takes preresolved addresses of the request.
|
||||
pub fn take_addrs(&mut self) -> ConnectTakeAddrsIter {
|
||||
let inner = match self.addr.take() {
|
||||
None => Either::Left(None),
|
||||
Some(Either::Left(addr)) => Either::Left(Some(addr)),
|
||||
Some(Either::Right(addrs)) => Either::Right(addrs.into_iter()),
|
||||
};
|
||||
|
||||
ConnectTakeAddrsIter { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Address> From<T> for Connect<T> {
|
||||
@@ -100,6 +137,70 @@ impl<T: Address> fmt::Display for Connect<T> {
|
||||
}
|
||||
}
|
||||
|
||||
/// Iterator over addresses in a [`Connect`](struct.Connect.html) request.
|
||||
#[derive(Clone)]
|
||||
pub struct ConnectAddrsIter<'a> {
|
||||
inner: Either<Option<SocketAddr>, vec_deque::Iter<'a, SocketAddr>>,
|
||||
}
|
||||
|
||||
impl Iterator for ConnectAddrsIter<'_> {
|
||||
type Item = SocketAddr;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.inner {
|
||||
Either::Left(ref mut opt) => opt.take(),
|
||||
Either::Right(ref mut iter) => iter.next().copied(),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self.inner {
|
||||
Either::Left(Some(_)) => (1, Some(1)),
|
||||
Either::Left(None) => (0, Some(0)),
|
||||
Either::Right(ref iter) => iter.size_hint(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ConnectAddrsIter<'_> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.debug_list().entries(self.clone()).finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for ConnectAddrsIter<'_> {}
|
||||
|
||||
impl FusedIterator for ConnectAddrsIter<'_> {}
|
||||
|
||||
/// Owned iterator over addresses in a [`Connect`](struct.Connect.html) request.
|
||||
#[derive(Debug)]
|
||||
pub struct ConnectTakeAddrsIter {
|
||||
inner: Either<Option<SocketAddr>, vec_deque::IntoIter<SocketAddr>>,
|
||||
}
|
||||
|
||||
impl Iterator for ConnectTakeAddrsIter {
|
||||
type Item = SocketAddr;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
match self.inner {
|
||||
Either::Left(ref mut opt) => opt.take(),
|
||||
Either::Right(ref mut iter) => iter.next(),
|
||||
}
|
||||
}
|
||||
|
||||
fn size_hint(&self) -> (usize, Option<usize>) {
|
||||
match self.inner {
|
||||
Either::Left(Some(_)) => (1, Some(1)),
|
||||
Either::Left(None) => (0, Some(0)),
|
||||
Either::Right(ref iter) => iter.size_hint(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ExactSizeIterator for ConnectTakeAddrsIter {}
|
||||
|
||||
impl FusedIterator for ConnectTakeAddrsIter {}
|
||||
|
||||
fn parse(host: &str) -> (&str, Option<u16>) {
|
||||
let mut parts_iter = host.splitn(2, ':');
|
||||
if let Some(host) = parts_iter.next() {
|
||||
|
@@ -1,4 +1,3 @@
|
||||
use std::collections::VecDeque;
|
||||
use std::marker::PhantomData;
|
||||
use std::net::SocketAddr;
|
||||
|
||||
@@ -162,23 +161,20 @@ impl<T: Address> Future for ResolverFuture<T> {
|
||||
})? {
|
||||
Async::NotReady => Ok(Async::NotReady),
|
||||
Async::Ready(ips) => {
|
||||
let mut req = self.req.take().unwrap();
|
||||
let mut addrs: VecDeque<_> = ips
|
||||
.iter()
|
||||
.map(|ip| SocketAddr::new(ip, req.port()))
|
||||
.collect();
|
||||
let req = self.req.take().unwrap();
|
||||
|
||||
let port = req.port();
|
||||
let req = req.set_addrs(ips.iter().map(|ip| SocketAddr::new(ip, port)));
|
||||
|
||||
trace!(
|
||||
"DNS resolver: host {:?} resolved to {:?}",
|
||||
req.host(),
|
||||
addrs
|
||||
req.addrs()
|
||||
);
|
||||
if addrs.is_empty() {
|
||||
|
||||
if req.addr.is_none() {
|
||||
Err(ConnectError::NoRecords)
|
||||
} else if addrs.len() == 1 {
|
||||
req.addr = Some(either::Either::Left(addrs.pop_front().unwrap()));
|
||||
Ok(Async::Ready(req))
|
||||
} else {
|
||||
req.addr = Some(either::Either::Right(addrs));
|
||||
Ok(Async::Ready(req))
|
||||
}
|
||||
}
|
||||
|
5
actix-ioframe/CHANGES.md
Normal file
5
actix-ioframe/CHANGES.md
Normal file
@@ -0,0 +1,5 @@
|
||||
# Changes
|
||||
|
||||
## [0.1.0] - 2019-xx-xx
|
||||
|
||||
* Initial release
|
35
actix-ioframe/Cargo.toml
Normal file
35
actix-ioframe/Cargo.toml
Normal file
@@ -0,0 +1,35 @@
|
||||
[package]
|
||||
name = "actix-ioframe"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix framed service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-net.git"
|
||||
documentation = "https://docs.rs/actix-ioframed/"
|
||||
categories = ["network-programming", "asynchronous"]
|
||||
license = "MIT/Apache-2.0"
|
||||
exclude = [".gitignore", ".travis.yml", ".cargo/config", "appveyor.yml"]
|
||||
edition = "2018"
|
||||
workspace = ".."
|
||||
|
||||
[lib]
|
||||
name = "actix_ioframe"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.4.1"
|
||||
actix-codec = "0.1.2"
|
||||
bytes = "0.4"
|
||||
either = "1.5.2"
|
||||
futures = "0.1.25"
|
||||
tokio-current-thread = "0.1.4"
|
||||
log = "0.4"
|
||||
|
||||
[dev-dependencies]
|
||||
actix-rt = "0.2.2"
|
||||
actix-connect = "0.2.0"
|
||||
actix-test-server = "0.2.2"
|
||||
actix-server-config = "0.1.1"
|
||||
tokio-tcp = "0.1"
|
||||
tokio-timer = "0.2"
|
1
actix-ioframe/LICENSE-APACHE
Symbolic link
1
actix-ioframe/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-ioframe/LICENSE-MIT
Symbolic link
1
actix-ioframe/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
35
actix-ioframe/src/cell.rs
Normal file
35
actix-ioframe/src/cell.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
//! Custom cell impl
|
||||
|
||||
use std::cell::UnsafeCell;
|
||||
use std::fmt;
|
||||
use std::rc::Rc;
|
||||
|
||||
pub(crate) struct Cell<T> {
|
||||
inner: Rc<UnsafeCell<T>>,
|
||||
}
|
||||
|
||||
impl<T> Clone for Cell<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: fmt::Debug> fmt::Debug for Cell<T> {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
self.inner.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Cell<T> {
|
||||
pub fn new(inner: T) -> Self {
|
||||
Self {
|
||||
inner: Rc::new(UnsafeCell::new(inner)),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut(&mut self) -> &mut T {
|
||||
unsafe { &mut *self.inner.as_ref().get() }
|
||||
}
|
||||
}
|
110
actix-ioframe/src/connect.rs
Normal file
110
actix-ioframe/src/connect.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||
use futures::unsync::mpsc;
|
||||
|
||||
use crate::dispatcher::FramedMessage;
|
||||
use crate::sink::Sink;
|
||||
|
||||
pub struct Connect<Io, St = (), Codec = ()> {
|
||||
io: Io,
|
||||
_t: PhantomData<(St, Codec)>,
|
||||
}
|
||||
|
||||
impl<Io> Connect<Io>
|
||||
where
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
{
|
||||
pub(crate) fn new(io: Io) -> Self {
|
||||
Self {
|
||||
io,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn codec<Codec>(self, codec: Codec) -> ConnectResult<Io, (), Codec>
|
||||
where
|
||||
Codec: Encoder + Decoder,
|
||||
{
|
||||
let (tx, rx) = mpsc::unbounded();
|
||||
let sink = Sink::new(tx);
|
||||
|
||||
ConnectResult {
|
||||
state: (),
|
||||
framed: Framed::new(self.io, codec),
|
||||
rx,
|
||||
sink,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ConnectResult<Io, St, Codec: Encoder + Decoder> {
|
||||
pub(crate) state: St,
|
||||
pub(crate) framed: Framed<Io, Codec>,
|
||||
pub(crate) rx: mpsc::UnboundedReceiver<FramedMessage<<Codec as Encoder>::Item>>,
|
||||
pub(crate) sink: Sink<<Codec as Encoder>::Item>,
|
||||
}
|
||||
|
||||
impl<Io, St, Codec: Encoder + Decoder> ConnectResult<Io, St, Codec> {
|
||||
#[inline]
|
||||
pub fn sink(&self) -> &Sink<<Codec as Encoder>::Item> {
|
||||
&self.sink
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_ref(&self) -> &Io {
|
||||
self.framed.get_ref()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn get_mut(&mut self) -> &mut Io {
|
||||
self.framed.get_mut()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state<S>(self, state: S) -> ConnectResult<Io, S, Codec> {
|
||||
ConnectResult {
|
||||
state,
|
||||
framed: self.framed,
|
||||
rx: self.rx,
|
||||
sink: self.sink,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Io, St, Codec> futures::Stream for ConnectResult<Io, St, Codec>
|
||||
where
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
Codec: Encoder + Decoder,
|
||||
{
|
||||
type Item = <Codec as Decoder>::Item;
|
||||
type Error = <Codec as Decoder>::Error;
|
||||
|
||||
fn poll(&mut self) -> futures::Poll<Option<Self::Item>, Self::Error> {
|
||||
self.framed.poll()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Io, St, Codec> futures::Sink for ConnectResult<Io, St, Codec>
|
||||
where
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
Codec: Encoder + Decoder,
|
||||
{
|
||||
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.framed.start_send(item)
|
||||
}
|
||||
|
||||
fn poll_complete(&mut self) -> futures::Poll<(), Self::SinkError> {
|
||||
self.framed.poll_complete()
|
||||
}
|
||||
|
||||
fn close(&mut self) -> futures::Poll<(), Self::SinkError> {
|
||||
self.framed.close()
|
||||
}
|
||||
}
|
323
actix-ioframe/src/dispatcher.rs
Normal file
323
actix-ioframe/src/dispatcher.rs
Normal file
@@ -0,0 +1,323 @@
|
||||
//! Framed dispatcher service and related utilities
|
||||
use std::collections::VecDeque;
|
||||
use std::mem;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder, Framed};
|
||||
use actix_service::{IntoService, Service};
|
||||
use futures::task::AtomicTask;
|
||||
use futures::unsync::{mpsc, oneshot};
|
||||
use futures::{Async, Future, Poll, Sink as FutureSink, Stream};
|
||||
use log::debug;
|
||||
|
||||
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;
|
||||
|
||||
pub(crate) enum FramedMessage<T> {
|
||||
Message(T),
|
||||
Close,
|
||||
WaitClose(oneshot::Sender<()>),
|
||||
}
|
||||
|
||||
/// FramedTransport - is a future that reads frames from Framed object
|
||||
/// and pass then to the service.
|
||||
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,
|
||||
U: Encoder + Decoder,
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
service: S,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
state: State<St>,
|
||||
dispatch_state: FramedState<S, U>,
|
||||
framed: Framed<T, U>,
|
||||
rx: Option<mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>>,
|
||||
inner: Cell<FramedDispatcherInner<<U as Encoder>::Item, S::Error>>,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
}
|
||||
|
||||
impl<St, S, T, U> 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,
|
||||
U: Decoder + Encoder,
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
pub(crate) fn new<F: IntoService<S>>(
|
||||
framed: Framed<T, U>,
|
||||
state: State<St>,
|
||||
service: F,
|
||||
rx: mpsc::UnboundedReceiver<FramedMessage<<U as Encoder>::Item>>,
|
||||
sink: Sink<<U as Encoder>::Item>,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
) -> Self {
|
||||
FramedDispatcher {
|
||||
framed,
|
||||
state,
|
||||
sink,
|
||||
disconnect,
|
||||
rx: Some(rx),
|
||||
service: service.into_service(),
|
||||
dispatch_state: FramedState::Processing,
|
||||
inner: Cell::new(FramedDispatcherInner {
|
||||
buf: VecDeque::new(),
|
||||
task: AtomicTask::new(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum FramedState<S: Service, U: Encoder + Decoder> {
|
||||
Processing,
|
||||
Error(ServiceError<S::Error, U>),
|
||||
FramedError(ServiceError<S::Error, U>),
|
||||
FlushAndStop(Vec<oneshot::Sender<()>>),
|
||||
Stopping,
|
||||
}
|
||||
|
||||
impl<S: Service, U: Encoder + Decoder> FramedState<S, U> {
|
||||
fn stop(&mut self, tx: Option<oneshot::Sender<()>>) {
|
||||
match self {
|
||||
FramedState::FlushAndStop(ref mut vec) => {
|
||||
if let Some(tx) = tx {
|
||||
vec.push(tx)
|
||||
}
|
||||
}
|
||||
FramedState::Processing => {
|
||||
*self = FramedState::FlushAndStop(if let Some(tx) = tx {
|
||||
vec![tx]
|
||||
} else {
|
||||
Vec::new()
|
||||
})
|
||||
}
|
||||
FramedState::Error(_) | FramedState::FramedError(_) | FramedState::Stopping => {
|
||||
if let Some(tx) = tx {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct FramedDispatcherInner<I, E> {
|
||||
buf: VecDeque<Result<I, E>>,
|
||||
task: AtomicTask,
|
||||
}
|
||||
|
||||
impl<St, S, T, U> 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,
|
||||
U: Decoder + Encoder,
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
fn disconnect(&mut self, error: bool) {
|
||||
if let Some(ref disconnect) = self.disconnect {
|
||||
(&*disconnect)(&mut *self.state.get_mut(), error);
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_read(&mut self) -> bool {
|
||||
loop {
|
||||
match self.service.poll_ready() {
|
||||
Ok(Async::Ready(_)) => {
|
||||
let item = match self.framed.poll() {
|
||||
Ok(Async::Ready(Some(el))) => el,
|
||||
Err(err) => {
|
||||
self.dispatch_state =
|
||||
FramedState::FramedError(ServiceError::Decoder(err));
|
||||
return true;
|
||||
}
|
||||
Ok(Async::NotReady) => return false,
|
||||
Ok(Async::Ready(None)) => {
|
||||
self.dispatch_state = FramedState::Stopping;
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
let mut cell = self.inner.clone();
|
||||
cell.get_mut().task.register();
|
||||
tokio_current_thread::spawn(
|
||||
self.service
|
||||
.call(Item::new(self.state.clone(), self.sink.clone(), item))
|
||||
.then(move |item| {
|
||||
let item = match item {
|
||||
Ok(Some(item)) => Ok(item),
|
||||
Ok(None) => return Ok(()),
|
||||
Err(err) => Err(err),
|
||||
};
|
||||
let inner = cell.get_mut();
|
||||
inner.buf.push_back(item);
|
||||
inner.task.notify();
|
||||
Ok(())
|
||||
}),
|
||||
);
|
||||
}
|
||||
Ok(Async::NotReady) => return false,
|
||||
Err(err) => {
|
||||
self.dispatch_state = FramedState::Error(ServiceError::Service(err));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// write to framed object
|
||||
fn poll_write(&mut self) -> bool {
|
||||
let inner = self.inner.get_mut();
|
||||
let mut rx_done = self.rx.is_none();
|
||||
let mut buf_empty = inner.buf.is_empty();
|
||||
loop {
|
||||
while !self.framed.is_write_buf_full() {
|
||||
if !buf_empty {
|
||||
match inner.buf.pop_front().unwrap() {
|
||||
Ok(msg) => {
|
||||
if let Err(err) = self.framed.force_send(msg) {
|
||||
self.dispatch_state =
|
||||
FramedState::FramedError(ServiceError::Encoder(err));
|
||||
return true;
|
||||
}
|
||||
buf_empty = inner.buf.is_empty();
|
||||
}
|
||||
Err(err) => {
|
||||
self.dispatch_state =
|
||||
FramedState::Error(ServiceError::Service(err));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !rx_done && self.rx.is_some() {
|
||||
match self.rx.as_mut().unwrap().poll() {
|
||||
Ok(Async::Ready(Some(FramedMessage::Message(msg)))) => {
|
||||
if let Err(err) = self.framed.force_send(msg) {
|
||||
self.dispatch_state =
|
||||
FramedState::FramedError(ServiceError::Encoder(err));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Ok(Async::Ready(Some(FramedMessage::Close))) => {
|
||||
self.dispatch_state.stop(None);
|
||||
return true;
|
||||
}
|
||||
Ok(Async::Ready(Some(FramedMessage::WaitClose(tx)))) => {
|
||||
self.dispatch_state.stop(Some(tx));
|
||||
return true;
|
||||
}
|
||||
Ok(Async::Ready(None)) => {
|
||||
rx_done = true;
|
||||
let _ = self.rx.take();
|
||||
}
|
||||
Ok(Async::NotReady) => rx_done = true,
|
||||
Err(_e) => {
|
||||
rx_done = true;
|
||||
let _ = self.rx.take();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if rx_done && buf_empty {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if !self.framed.is_write_buf_empty() {
|
||||
match self.framed.poll_complete() {
|
||||
Ok(Async::NotReady) => break,
|
||||
Err(err) => {
|
||||
debug!("Error sending data: {:?}", err);
|
||||
self.dispatch_state =
|
||||
FramedState::FramedError(ServiceError::Encoder(err));
|
||||
return true;
|
||||
}
|
||||
Ok(Async::Ready(_)) => (),
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
impl<St, S, T, U> Future for 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,
|
||||
U: Decoder + Encoder,
|
||||
<U as Encoder>::Item: 'static,
|
||||
<U as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
type Item = ();
|
||||
type Error = ServiceError<S::Error, U>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
match mem::replace(&mut self.dispatch_state, FramedState::Processing) {
|
||||
FramedState::Processing => {
|
||||
if self.poll_read() || self.poll_write() {
|
||||
self.poll()
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
FramedState::Error(err) => {
|
||||
if self.framed.is_write_buf_empty()
|
||||
|| (self.poll_write() || self.framed.is_write_buf_empty())
|
||||
{
|
||||
self.disconnect(true);
|
||||
Err(err)
|
||||
} else {
|
||||
self.dispatch_state = FramedState::Error(err);
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
FramedState::FlushAndStop(mut vec) => {
|
||||
if !self.framed.is_write_buf_empty() {
|
||||
match self.framed.poll_complete() {
|
||||
Err(err) => {
|
||||
debug!("Error sending data: {:?}", err);
|
||||
}
|
||||
Ok(Async::NotReady) => {
|
||||
self.dispatch_state = FramedState::FlushAndStop(vec);
|
||||
return Ok(Async::NotReady);
|
||||
}
|
||||
Ok(Async::Ready(_)) => (),
|
||||
}
|
||||
};
|
||||
for tx in vec.drain(..) {
|
||||
let _ = tx.send(());
|
||||
}
|
||||
self.disconnect(false);
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
FramedState::FramedError(err) => {
|
||||
self.disconnect(true);
|
||||
Err(err)
|
||||
}
|
||||
FramedState::Stopping => {
|
||||
self.disconnect(false);
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
49
actix-ioframe/src/error.rs
Normal file
49
actix-ioframe/src/error.rs
Normal file
@@ -0,0 +1,49 @@
|
||||
use std::fmt;
|
||||
|
||||
use actix_codec::{Decoder, Encoder};
|
||||
|
||||
/// Framed service errors
|
||||
pub enum ServiceError<E, U: Encoder + Decoder> {
|
||||
/// Inner service error
|
||||
Service(E),
|
||||
/// Encoder parse error
|
||||
Encoder(<U as Encoder>::Error),
|
||||
/// Decoder parse error
|
||||
Decoder(<U as Decoder>::Error),
|
||||
}
|
||||
|
||||
impl<E, U: Encoder + Decoder> From<E> for ServiceError<E, U> {
|
||||
fn from(err: E) -> Self {
|
||||
ServiceError::Service(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, U: Encoder + Decoder> fmt::Debug for ServiceError<E, U>
|
||||
where
|
||||
E: fmt::Debug,
|
||||
<U as Encoder>::Error: fmt::Debug,
|
||||
<U as Decoder>::Error: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ServiceError::Service(ref e) => write!(fmt, "ServiceError::Service({:?})", e),
|
||||
ServiceError::Encoder(ref e) => write!(fmt, "ServiceError::Encoder({:?})", e),
|
||||
ServiceError::Decoder(ref e) => write!(fmt, "ServiceError::Encoder({:?})", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<E, U: Encoder + Decoder> fmt::Display for ServiceError<E, U>
|
||||
where
|
||||
E: fmt::Display,
|
||||
<U as Encoder>::Error: fmt::Debug,
|
||||
<U as Decoder>::Error: fmt::Debug,
|
||||
{
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
match *self {
|
||||
ServiceError::Service(ref e) => write!(fmt, "{}", e),
|
||||
ServiceError::Encoder(ref e) => write!(fmt, "{:?}", e),
|
||||
ServiceError::Decoder(ref e) => write!(fmt, "{:?}", e),
|
||||
}
|
||||
}
|
||||
}
|
90
actix-ioframe/src/item.rs
Normal file
90
actix-ioframe/src/item.rs
Normal file
@@ -0,0 +1,90 @@
|
||||
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>,
|
||||
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(
|
||||
state: State<St>,
|
||||
sink: Sink<<Codec as Encoder>::Item>,
|
||||
item: <Codec as Decoder>::Item,
|
||||
) -> Self {
|
||||
Item { state, sink, item }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state(&self) -> Ref<St> {
|
||||
self.state.get_ref()
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn state_mut(&mut self) -> RefMut<St> {
|
||||
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
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn into_parts(
|
||||
self,
|
||||
) -> (
|
||||
State<St>,
|
||||
Sink<<Codec as Encoder>::Item>,
|
||||
<Codec as Decoder>::Item,
|
||||
) {
|
||||
(self.state, self.sink, self.item)
|
||||
}
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
15
actix-ioframe/src/lib.rs
Normal file
15
actix-ioframe/src/lib.rs
Normal file
@@ -0,0 +1,15 @@
|
||||
mod cell;
|
||||
mod connect;
|
||||
mod dispatcher;
|
||||
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;
|
363
actix-ioframe/src/service.rs
Normal file
363
actix-ioframe/src/service.rs
Normal file
@@ -0,0 +1,363 @@
|
||||
use std::marker::PhantomData;
|
||||
use std::rc::Rc;
|
||||
|
||||
use actix_codec::{AsyncRead, AsyncWrite, Decoder, Encoder};
|
||||
use actix_service::{IntoNewService, IntoService, NewService, Service};
|
||||
use futures::{Async, Future, Poll};
|
||||
|
||||
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>;
|
||||
|
||||
/// Service builder - structure that follows the builder pattern
|
||||
/// for building instances for framed services.
|
||||
pub struct Builder<St, Codec>(PhantomData<(St, Codec)>);
|
||||
|
||||
impl<St, Codec> Builder<St, Codec> {
|
||||
pub fn new() -> Builder<St, Codec> {
|
||||
Builder(PhantomData)
|
||||
}
|
||||
|
||||
/// Construct framed handler service with specified connect service
|
||||
pub fn service<Io, C, F>(self, connect: F) -> ServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
F: IntoService<C>,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
Codec: Decoder + Encoder,
|
||||
{
|
||||
ServiceBuilder {
|
||||
connect: connect.into_service(),
|
||||
disconnect: None,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Construct framed handler new service with specified connect service
|
||||
pub fn factory<Io, C, F>(self, connect: F) -> NewServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
F: IntoNewService<C>,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: NewService<
|
||||
Config = (),
|
||||
Request = Connect<Io>,
|
||||
Response = ConnectResult<Io, St, Codec>,
|
||||
>,
|
||||
C::Error: 'static,
|
||||
C::Future: 'static,
|
||||
Codec: Decoder + Encoder,
|
||||
{
|
||||
NewServiceBuilder {
|
||||
connect: connect.into_new_service(),
|
||||
disconnect: None,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ServiceBuilder<St, C, Io, Codec> {
|
||||
connect: C,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
_t: PhantomData<(St, Io, Codec)>,
|
||||
}
|
||||
|
||||
impl<St, C, Io, Codec> ServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
Codec: Decoder + Encoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
/// Callback to execute on disconnect
|
||||
///
|
||||
/// Second parameter indicates error occured during disconnect.
|
||||
pub fn disconnect<F, Out>(mut self, disconnect: F) -> Self
|
||||
where
|
||||
F: Fn(&mut St, bool) + 'static,
|
||||
{
|
||||
self.disconnect = Some(Rc::new(disconnect));
|
||||
self
|
||||
}
|
||||
|
||||
/// Provide stream items handler service and construct service factory.
|
||||
pub fn finish<F, T>(
|
||||
self,
|
||||
service: F,
|
||||
) -> impl Service<Request = Io, Response = (), Error = ServiceError<C::Error, Codec>>
|
||||
where
|
||||
F: IntoNewService<T>,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
> + 'static,
|
||||
{
|
||||
FramedServiceImpl {
|
||||
connect: self.connect,
|
||||
handler: Rc::new(service.into_new_service()),
|
||||
disconnect: self.disconnect.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NewServiceBuilder<St, C, Io, Codec> {
|
||||
connect: C,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
_t: PhantomData<(St, Io, Codec)>,
|
||||
}
|
||||
|
||||
impl<St, C, Io, Codec> NewServiceBuilder<St, C, Io, Codec>
|
||||
where
|
||||
St: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: NewService<Config = (), Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
C::Future: 'static,
|
||||
Codec: Decoder + Encoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
/// Callback to execute on disconnect
|
||||
///
|
||||
/// Second parameter indicates error occured during disconnect.
|
||||
pub fn disconnect<F>(mut self, disconnect: F) -> Self
|
||||
where
|
||||
F: Fn(&mut St, bool) + 'static,
|
||||
{
|
||||
self.disconnect = Some(Rc::new(disconnect));
|
||||
self
|
||||
}
|
||||
|
||||
pub fn finish<F, T, Cfg>(
|
||||
self,
|
||||
service: F,
|
||||
) -> impl NewService<
|
||||
Config = Cfg,
|
||||
Request = Io,
|
||||
Response = (),
|
||||
Error = ServiceError<C::Error, Codec>,
|
||||
>
|
||||
where
|
||||
F: IntoNewService<T>,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
> + 'static,
|
||||
{
|
||||
FramedService {
|
||||
connect: self.connect,
|
||||
handler: Rc::new(service.into_new_service()),
|
||||
disconnect: self.disconnect,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct FramedService<St, C, T, Io, Codec, Cfg> {
|
||||
connect: C,
|
||||
handler: Rc<T>,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
_t: PhantomData<(St, Io, Codec, Cfg)>,
|
||||
}
|
||||
|
||||
impl<St, C, T, Io, Codec, Cfg> NewService for FramedService<St, C, T, Io, Codec, Cfg>
|
||||
where
|
||||
St: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: NewService<Config = (), Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
C::Future: 'static,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
> + 'static,
|
||||
Codec: Decoder + Encoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
type Config = Cfg;
|
||||
type Request = Io;
|
||||
type Response = ();
|
||||
type Error = ServiceError<C::Error, Codec>;
|
||||
type InitError = C::InitError;
|
||||
type Service = FramedServiceImpl<St, C::Service, T, Io, Codec>;
|
||||
type Future = Box<Future<Item = Self::Service, Error = Self::InitError>>;
|
||||
|
||||
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
|
||||
Box::new(
|
||||
self.connect
|
||||
.new_service(&())
|
||||
.map(move |connect| FramedServiceImpl {
|
||||
connect,
|
||||
handler,
|
||||
disconnect,
|
||||
_t: PhantomData,
|
||||
}),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FramedServiceImpl<St, C, T, Io, Codec> {
|
||||
connect: C,
|
||||
handler: Rc<T>,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
_t: PhantomData<(St, Io, Codec)>,
|
||||
}
|
||||
|
||||
impl<St, C, T, Io, Codec> Service for FramedServiceImpl<St, C, T, Io, Codec>
|
||||
where
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
>,
|
||||
<<T as NewService>::Service as Service>::Future: 'static,
|
||||
Codec: Decoder + Encoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
type Request = Io;
|
||||
type Response = ();
|
||||
type Error = ServiceError<C::Error, Codec>;
|
||||
type Future = FramedServiceImplResponse<St, Io, Codec, C, T>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.connect.poll_ready().map_err(|e| e.into())
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Io) -> Self::Future {
|
||||
FramedServiceImplResponse {
|
||||
inner: FramedServiceImplResponseInner::Connect(
|
||||
self.connect.call(Connect::new(req)),
|
||||
self.handler.clone(),
|
||||
),
|
||||
disconnect: self.disconnect.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FramedServiceImplResponse<St, Io, Codec, C, T>
|
||||
where
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
>,
|
||||
<<T as NewService>::Service as Service>::Future: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
Codec: Encoder + Decoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
inner: FramedServiceImplResponseInner<St, Io, Codec, C, T>,
|
||||
disconnect: Option<Rc<Fn(&mut St, bool)>>,
|
||||
}
|
||||
|
||||
enum FramedServiceImplResponseInner<St, Io, Codec, C, T>
|
||||
where
|
||||
C: Service<Request = Connect<Io>, Response = ConnectResult<Io, St, Codec>>,
|
||||
C::Error: 'static,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
>,
|
||||
<<T as NewService>::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>),
|
||||
Handler(T::Future, Option<ConnectResult<Io, St, Codec>>),
|
||||
Dispatcher(FramedDispatcher<St, T::Service, Io, Codec>),
|
||||
}
|
||||
|
||||
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::Error: 'static,
|
||||
T: NewService<
|
||||
Config = St,
|
||||
Request = RequestItem<St, Codec>,
|
||||
Response = ResponseItem<Codec>,
|
||||
Error = C::Error,
|
||||
InitError = C::Error,
|
||||
>,
|
||||
<<T as NewService>::Service as Service>::Future: 'static,
|
||||
Io: AsyncRead + AsyncWrite,
|
||||
Codec: Encoder + Decoder,
|
||||
<Codec as Encoder>::Item: 'static,
|
||||
<Codec as Encoder>::Error: std::fmt::Debug,
|
||||
{
|
||||
type Item = ();
|
||||
type Error = ServiceError<C::Error, Codec>;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
match self.inner {
|
||||
FramedServiceImplResponseInner::Connect(ref mut fut, ref handler) => {
|
||||
match fut.poll()? {
|
||||
Async::Ready(res) => {
|
||||
self.inner = FramedServiceImplResponseInner::Handler(
|
||||
handler.new_service(&res.state),
|
||||
Some(res),
|
||||
);
|
||||
self.poll()
|
||||
}
|
||||
Async::NotReady => Ok(Async::NotReady),
|
||||
}
|
||||
}
|
||||
FramedServiceImplResponseInner::Handler(ref mut fut, ref mut res) => {
|
||||
match fut.poll()? {
|
||||
Async::Ready(handler) => {
|
||||
let res = res.take().unwrap();
|
||||
self.inner =
|
||||
FramedServiceImplResponseInner::Dispatcher(FramedDispatcher::new(
|
||||
res.framed,
|
||||
State::new(res.state),
|
||||
handler,
|
||||
res.rx,
|
||||
res.sink,
|
||||
self.disconnect.clone(),
|
||||
));
|
||||
self.poll()
|
||||
}
|
||||
Async::NotReady => Ok(Async::NotReady),
|
||||
}
|
||||
}
|
||||
FramedServiceImplResponseInner::Dispatcher(ref mut fut) => fut.poll(),
|
||||
}
|
||||
}
|
||||
}
|
44
actix-ioframe/src/sink.rs
Normal file
44
actix-ioframe/src/sink.rs
Normal file
@@ -0,0 +1,44 @@
|
||||
use std::fmt;
|
||||
|
||||
use futures::unsync::{mpsc, oneshot};
|
||||
use futures::Future;
|
||||
|
||||
use crate::dispatcher::FramedMessage;
|
||||
|
||||
pub struct Sink<T>(mpsc::UnboundedSender<FramedMessage<T>>);
|
||||
|
||||
impl<T> Clone for Sink<T> {
|
||||
fn clone(&self) -> Self {
|
||||
Sink(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Sink<T> {
|
||||
pub(crate) fn new(tx: mpsc::UnboundedSender<FramedMessage<T>>) -> Self {
|
||||
Sink(tx)
|
||||
}
|
||||
|
||||
/// Close connection
|
||||
pub fn close(&self) {
|
||||
let _ = self.0.unbounded_send(FramedMessage::Close);
|
||||
}
|
||||
|
||||
/// Close connection
|
||||
pub fn wait_close(&self) -> impl Future<Item = (), Error = ()> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.0.unbounded_send(FramedMessage::WaitClose(tx));
|
||||
|
||||
rx.map_err(|_| ())
|
||||
}
|
||||
|
||||
/// Send item
|
||||
pub fn send(&self, item: T) {
|
||||
let _ = self.0.unbounded_send(FramedMessage::Message(item));
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> fmt::Debug for Sink<T> {
|
||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
||||
fmt.debug_struct("Sink").finish()
|
||||
}
|
||||
}
|
30
actix-ioframe/src/state.rs
Normal file
30
actix-ioframe/src/state.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
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())
|
||||
}
|
||||
}
|
60
actix-ioframe/tests/test_server.rs
Normal file
60
actix-ioframe/tests/test_server.rs
Normal file
@@ -0,0 +1,60 @@
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::time::Duration;
|
||||
|
||||
use actix_codec::BytesCodec;
|
||||
use actix_server_config::Io;
|
||||
use actix_service::{new_apply_fn, Service};
|
||||
use actix_test_server::TestServer;
|
||||
use futures::Future;
|
||||
use tokio_tcp::TcpStream;
|
||||
use tokio_timer::sleep;
|
||||
|
||||
use actix_ioframe::{Builder, Connect};
|
||||
|
||||
struct State;
|
||||
|
||||
#[test]
|
||||
fn test_disconnect() -> std::io::Result<()> {
|
||||
let disconnect = Arc::new(AtomicBool::new(false));
|
||||
let disconnect1 = disconnect.clone();
|
||||
|
||||
let mut srv = TestServer::with(move || {
|
||||
let disconnect1 = disconnect1.clone();
|
||||
|
||||
new_apply_fn(
|
||||
Builder::new()
|
||||
.factory(|conn: Connect<_>| Ok(conn.codec(BytesCodec).state(State)))
|
||||
.disconnect(move |_, _| {
|
||||
disconnect1.store(true, Ordering::Relaxed);
|
||||
})
|
||||
.finish(|_t| Ok(None)),
|
||||
|io: Io<TcpStream>, srv| srv.call(io.into_parts().0),
|
||||
)
|
||||
});
|
||||
|
||||
let mut client = Builder::new()
|
||||
.service(|conn: Connect<_>| {
|
||||
let conn = conn.codec(BytesCodec).state(State);
|
||||
conn.sink().close();
|
||||
Ok(conn)
|
||||
})
|
||||
.finish(|_t| Ok(None));
|
||||
|
||||
let conn = srv
|
||||
.block_on(
|
||||
actix_connect::default_connector()
|
||||
.call(actix_connect::Connect::with(String::new(), srv.addr())),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
srv.block_on(client.call(conn.into_parts().0)).unwrap();
|
||||
let _ = srv.block_on(
|
||||
sleep(Duration::from_millis(100))
|
||||
.map(|_| ())
|
||||
.map_err(|_| ()),
|
||||
);
|
||||
assert!(disconnect.load(Ordering::Relaxed));
|
||||
|
||||
Ok(())
|
||||
}
|
@@ -1,11 +1,26 @@
|
||||
# Changes
|
||||
|
||||
## [0.2.4] - 2019-07-17
|
||||
|
||||
### Changed
|
||||
|
||||
* Avoid a copy of the Future when initializing the Box. #29
|
||||
|
||||
|
||||
## [0.2.3] - 2019-06-22
|
||||
|
||||
### Added
|
||||
|
||||
* Allow to start System using exsiting CurrentThread Handle #22
|
||||
|
||||
|
||||
## [0.2.2] - 2019-03-28
|
||||
|
||||
### Changed
|
||||
|
||||
* Moved `blocking` module to `actix-threadpool` crate
|
||||
|
||||
|
||||
## [0.2.1] - 2019-03-11
|
||||
|
||||
### Added
|
||||
@@ -16,12 +31,14 @@
|
||||
|
||||
* Arbiter::exec - execute fn on the arbiter's thread and wait result
|
||||
|
||||
|
||||
## [0.2.0] - 2019-03-06
|
||||
|
||||
* `run` method returns `io::Result<()>`
|
||||
|
||||
* Removed `Handle`
|
||||
|
||||
|
||||
## [0.1.0] - 2018-12-09
|
||||
|
||||
* Initial release
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-rt"
|
||||
version = "0.2.2"
|
||||
version = "0.2.4"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix runtime"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -18,9 +18,10 @@ name = "actix_rt"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-threadpool = "0.1.0"
|
||||
actix-threadpool = "0.1.1"
|
||||
futures = "0.1.25"
|
||||
tokio-current-thread = "0.1"
|
||||
tokio-executor = "0.1.5"
|
||||
tokio-reactor = "0.1.7"
|
||||
tokio-timer = "0.2.8"
|
||||
copyless = "0.1.4"
|
||||
|
1
actix-rt/LICENSE-APACHE
Symbolic link
1
actix-rt/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-rt/LICENSE-MIT
Symbolic link
1
actix-rt/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -11,6 +11,8 @@ use tokio_current_thread::spawn;
|
||||
use crate::builder::Builder;
|
||||
use crate::system::System;
|
||||
|
||||
use copyless::BoxHelper;
|
||||
|
||||
thread_local!(
|
||||
static ADDR: RefCell<Option<Arbiter>> = RefCell::new(None);
|
||||
static RUNNING: Cell<bool> = Cell::new(false);
|
||||
@@ -141,9 +143,9 @@ impl Arbiter {
|
||||
{
|
||||
RUNNING.with(move |cell| {
|
||||
if cell.get() {
|
||||
spawn(Box::new(future));
|
||||
spawn(Box::alloc().init(future));
|
||||
} else {
|
||||
Q.with(move |cell| cell.borrow_mut().push(Box::new(future)));
|
||||
Q.with(move |cell| cell.borrow_mut().push(Box::alloc().init(future)));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@@ -1,11 +1,12 @@
|
||||
use std::borrow::Cow;
|
||||
use std::io;
|
||||
|
||||
use futures::future;
|
||||
use futures::future::{lazy, Future};
|
||||
use futures::sync::mpsc::unbounded;
|
||||
use futures::sync::oneshot::{channel, Receiver};
|
||||
|
||||
use tokio_current_thread::CurrentThread;
|
||||
use tokio_current_thread::{CurrentThread, Handle};
|
||||
use tokio_reactor::Reactor;
|
||||
use tokio_timer::clock::Clock;
|
||||
use tokio_timer::timer::Timer;
|
||||
@@ -69,6 +70,13 @@ impl Builder {
|
||||
self.create_runtime(|| {})
|
||||
}
|
||||
|
||||
/// Create new System that can run asynchronously.
|
||||
///
|
||||
/// This method panics if it cannot start the system arbiter
|
||||
pub(crate) fn build_async(self, executor: Handle) -> AsyncSystemRunner {
|
||||
self.create_async_runtime(executor)
|
||||
}
|
||||
|
||||
/// This function will start tokio runtime and will finish once the
|
||||
/// `System::stop()` message get called.
|
||||
/// Function `f` get called within tokio runtime context.
|
||||
@@ -79,6 +87,21 @@ impl Builder {
|
||||
self.create_runtime(f).run()
|
||||
}
|
||||
|
||||
fn create_async_runtime(self, executor: Handle) -> AsyncSystemRunner {
|
||||
let (stop_tx, stop) = channel();
|
||||
let (sys_sender, sys_receiver) = unbounded();
|
||||
|
||||
let system = System::construct(sys_sender, Arbiter::new_system(), self.stop_on_panic);
|
||||
|
||||
// system arbiter
|
||||
let arb = SystemArbiter::new(stop_tx, sys_receiver);
|
||||
|
||||
// start the system arbiter
|
||||
executor.spawn(arb).expect("could not start system arbiter");
|
||||
|
||||
AsyncSystemRunner { stop, system }
|
||||
}
|
||||
|
||||
fn create_runtime<F>(self, f: F) -> SystemRunner
|
||||
where
|
||||
F: FnOnce() + 'static,
|
||||
@@ -127,6 +150,42 @@ impl Builder {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct AsyncSystemRunner {
|
||||
stop: Receiver<i32>,
|
||||
system: System,
|
||||
}
|
||||
|
||||
impl AsyncSystemRunner {
|
||||
/// This function will start event loop and returns a future that
|
||||
/// resolves once the `System::stop()` function is called.
|
||||
pub(crate) fn run_nonblocking(self) -> impl Future<Item = (), Error = io::Error> + Send {
|
||||
let AsyncSystemRunner { stop, .. } = self;
|
||||
|
||||
// run loop
|
||||
future::lazy(|| {
|
||||
Arbiter::run_system();
|
||||
stop.then(|res| match res {
|
||||
Ok(code) => {
|
||||
if code != 0 {
|
||||
Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!("Non-zero exit code: {}", code),
|
||||
))
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Err(e) => Err(io::Error::new(io::ErrorKind::Other, e)),
|
||||
})
|
||||
.then(|result| {
|
||||
Arbiter::stop_system();
|
||||
result
|
||||
})
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper object that runs System's event loop
|
||||
#[must_use = "SystemRunner must be run"]
|
||||
#[derive(Debug)]
|
||||
|
@@ -3,6 +3,8 @@ use std::io;
|
||||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
|
||||
use futures::sync::mpsc::UnboundedSender;
|
||||
use futures::Future;
|
||||
use tokio_current_thread::Handle;
|
||||
|
||||
use crate::arbiter::{Arbiter, SystemCommand};
|
||||
use crate::builder::{Builder, SystemRunner};
|
||||
@@ -55,6 +57,20 @@ impl System {
|
||||
Self::builder().name(name).build()
|
||||
}
|
||||
|
||||
#[allow(clippy::new_ret_no_self)]
|
||||
/// Create new system using provided CurrentThread Handle.
|
||||
///
|
||||
/// This method panics if it can not spawn system arbiter
|
||||
pub fn run_in_executor<T: Into<String>>(
|
||||
name: T,
|
||||
executor: Handle,
|
||||
) -> impl Future<Item = (), Error = io::Error> + Send {
|
||||
Self::builder()
|
||||
.name(name)
|
||||
.build_async(executor)
|
||||
.run_nonblocking()
|
||||
}
|
||||
|
||||
/// Get current running system.
|
||||
pub fn current() -> System {
|
||||
CURRENT.with(|cell| match *cell.borrow() {
|
||||
|
1
actix-server-config/LICENSE-APACHE
Symbolic link
1
actix-server-config/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-server-config/LICENSE-MIT
Symbolic link
1
actix-server-config/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -1,5 +1,12 @@
|
||||
# Changes
|
||||
|
||||
## [0.5.1] - 2019-05-18
|
||||
|
||||
### Changed
|
||||
|
||||
* ServerBuilder::shutdown_timeout() accepts u64
|
||||
|
||||
|
||||
## [0.5.0] - 2019-05-12
|
||||
|
||||
### Added
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-server"
|
||||
version = "0.5.0"
|
||||
version = "0.5.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix server - General purpose tcp server"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
1
actix-server/LICENSE-APACHE
Symbolic link
1
actix-server/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-server/LICENSE-MIT
Symbolic link
1
actix-server/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -128,8 +128,8 @@ impl ServerBuilder {
|
||||
/// dropped.
|
||||
///
|
||||
/// By default shutdown timeout sets to 30 seconds.
|
||||
pub fn shutdown_timeout(mut self, sec: u16) -> Self {
|
||||
self.shutdown_timeout = Duration::from_secs(u64::from(sec));
|
||||
pub fn shutdown_timeout(mut self, sec: u64) -> Self {
|
||||
self.shutdown_timeout = Duration::from_secs(sec);
|
||||
self
|
||||
}
|
||||
|
||||
|
@@ -1,5 +1,11 @@
|
||||
# Changes
|
||||
|
||||
## [0.4.1] - 2019-06-06
|
||||
|
||||
### Added
|
||||
|
||||
* Add `new_apply_cfg` function
|
||||
|
||||
## [0.4.0] - 2019-05-12
|
||||
|
||||
### Changed
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-service"
|
||||
version = "0.4.0"
|
||||
version = "0.4.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix Service"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
|
1
actix-service/LICENSE-APACHE
Symbolic link
1
actix-service/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-service/LICENSE-MIT
Symbolic link
1
actix-service/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -7,7 +7,17 @@ use crate::cell::Cell;
|
||||
use crate::{IntoService, NewService, Service};
|
||||
|
||||
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
||||
pub fn apply_cfg<F, C, T, R, S>(srv: T, f: F) -> ApplyConfigService<F, C, T, R, S>
|
||||
pub fn apply_cfg<F, C, T, R, S>(
|
||||
srv: T,
|
||||
f: F,
|
||||
) -> impl NewService<
|
||||
Config = C,
|
||||
Request = S::Request,
|
||||
Response = S::Response,
|
||||
Error = S::Error,
|
||||
Service = S,
|
||||
InitError = R::Error,
|
||||
> + Clone
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
T: Service,
|
||||
@@ -22,8 +32,36 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(&Config, &mut Service) -> Future<Service>` fn to a NewService
|
||||
/// Service get constructor from NewService.
|
||||
pub fn new_apply_cfg<F, C, T, R, S>(
|
||||
srv: T,
|
||||
f: F,
|
||||
) -> impl NewService<
|
||||
Config = C,
|
||||
Request = S::Request,
|
||||
Response = S::Response,
|
||||
Error = S::Error,
|
||||
Service = S,
|
||||
InitError = T::InitError,
|
||||
> + Clone
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
ApplyConfigNewService {
|
||||
f: Cell::new(f),
|
||||
srv: Cell::new(srv),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||
pub struct ApplyConfigService<F, C, T, R, S>
|
||||
struct ApplyConfigService<F, C, T, R, S>
|
||||
where
|
||||
F: FnMut(&C, &mut T) -> R,
|
||||
T: Service,
|
||||
@@ -79,7 +117,7 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnNewServiceConfigFut<R, S>
|
||||
struct FnNewServiceConfigFut<R, S>
|
||||
where
|
||||
R: IntoFuture,
|
||||
R::Item: IntoService<S>,
|
||||
@@ -102,3 +140,113 @@ where
|
||||
Ok(Async::Ready(try_ready!(self.fut.poll()).into_service()))
|
||||
}
|
||||
}
|
||||
|
||||
/// Convert `Fn(&Config) -> Future<Service>` fn to NewService
|
||||
struct ApplyConfigNewService<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
f: Cell<F>,
|
||||
srv: Cell<T>,
|
||||
_t: PhantomData<(C, R, S)>,
|
||||
}
|
||||
|
||||
impl<F, C, T, R, S> Clone for ApplyConfigNewService<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
fn clone(&self) -> Self {
|
||||
ApplyConfigNewService {
|
||||
f: self.f.clone(),
|
||||
srv: self.srv.clone(),
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<F, C, T, R, S> NewService for ApplyConfigNewService<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
type Config = C;
|
||||
type Request = S::Request;
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Service = S;
|
||||
|
||||
type InitError = R::Error;
|
||||
type Future = ApplyConfigNewServiceFut<F, C, T, R, S>;
|
||||
|
||||
fn new_service(&self, cfg: &C) -> Self::Future {
|
||||
ApplyConfigNewServiceFut {
|
||||
f: self.f.clone(),
|
||||
cfg: cfg.clone(),
|
||||
srv: Some(self.srv.get_ref().new_service(&())),
|
||||
fut: None,
|
||||
_t: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct ApplyConfigNewServiceFut<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
cfg: C,
|
||||
f: Cell<F>,
|
||||
srv: Option<T::Future>,
|
||||
fut: Option<R::Future>,
|
||||
_t: PhantomData<(S,)>,
|
||||
}
|
||||
|
||||
impl<F, C, T, R, S> Future for ApplyConfigNewServiceFut<F, C, T, R, S>
|
||||
where
|
||||
C: Clone,
|
||||
F: FnMut(&C, &mut T::Service) -> R,
|
||||
T: NewService<Config = ()>,
|
||||
R: IntoFuture<Error = T::InitError>,
|
||||
R::Item: IntoService<S>,
|
||||
S: Service,
|
||||
{
|
||||
type Item = S;
|
||||
type Error = R::Error;
|
||||
|
||||
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
||||
if let Some(ref mut fut) = self.srv {
|
||||
match fut.poll()? {
|
||||
Async::NotReady => return Ok(Async::NotReady),
|
||||
Async::Ready(mut srv) => {
|
||||
let _ = self.srv.take();
|
||||
self.fut = Some(self.f.get_mut()(&self.cfg, &mut srv).into_future());
|
||||
return self.poll();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(ref mut fut) = self.fut {
|
||||
Ok(Async::Ready(try_ready!(fut.poll()).into_service()))
|
||||
} else {
|
||||
Ok(Async::NotReady)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -26,6 +26,10 @@ impl<T> Cell<T> {
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn get_ref(&self) -> &T {
|
||||
unsafe { &*self.inner.as_ref().get() }
|
||||
}
|
||||
|
||||
pub(crate) fn get_mut(&mut self) -> &mut T {
|
||||
unsafe { &mut *self.inner.as_ref().get() }
|
||||
}
|
||||
|
@@ -25,7 +25,7 @@ mod transform_err;
|
||||
|
||||
pub use self::and_then::{AndThen, AndThenNewService};
|
||||
pub use self::apply::{apply_fn, new_apply_fn, Apply, ApplyNewService};
|
||||
pub use self::apply_cfg::apply_cfg;
|
||||
pub use self::apply_cfg::{apply_cfg, new_apply_cfg};
|
||||
pub use self::fn_service::{new_service_cfg, new_service_fn, service_fn, ServiceFn};
|
||||
pub use self::fn_transform::transform_fn;
|
||||
pub use self::from_err::{FromErr, FromErrNewService};
|
||||
|
@@ -6,17 +6,11 @@ use futures::{Async, Future, IntoFuture, Poll};
|
||||
use crate::transform_err::{TransformFromErr, TransformMapInitErr};
|
||||
use crate::{IntoNewService, NewService, Service};
|
||||
|
||||
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
||||
/// is often implemented for middleware, defining how to manufacture a
|
||||
/// middleware Service. A Service that is manufactured by the factory takes
|
||||
/// The `Transform` trait defines the interface of a Service factory. `Transform`
|
||||
/// is often implemented for middleware, defining how to construct a
|
||||
/// middleware Service. A Service that is constructed by the factory takes
|
||||
/// the Service that follows it during execution as a parameter, assuming
|
||||
/// ownership of the next Service. A Service can be a variety of types, such
|
||||
/// as (but not limited to) another middleware Service, an extractor Service,
|
||||
/// other helper Services, or the request handler endpoint Service.
|
||||
///
|
||||
/// A Service is created by the factory during server initialization.
|
||||
///
|
||||
/// `Config` is a service factory configuration type.
|
||||
/// ownership of the next Service.
|
||||
pub trait Transform<S> {
|
||||
/// Requests handled by the service.
|
||||
type Request;
|
||||
|
1
actix-test-server/LICENSE-APACHE
Symbolic link
1
actix-test-server/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-test-server/LICENSE-MIT
Symbolic link
1
actix-test-server/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -1,5 +1,13 @@
|
||||
# Changes
|
||||
|
||||
### Changed
|
||||
|
||||
* Update `derive_more` to 0.15
|
||||
|
||||
## [0.1.1] - 2019-06-05
|
||||
|
||||
* Update parking_lot
|
||||
|
||||
## [0.1.0] - 2019-03-28
|
||||
|
||||
* Move threadpool to separate crate
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-threadpool"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix thread pool for sync code"
|
||||
keywords = ["actix", "network", "framework", "async", "futures"]
|
||||
@@ -18,9 +18,9 @@ name = "actix_threadpool"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
derive_more = "0.14"
|
||||
derive_more = "0.15"
|
||||
futures = "0.1.25"
|
||||
parking_lot = "0.7"
|
||||
parking_lot = "0.8"
|
||||
lazy_static = "1.2"
|
||||
log = "0.4"
|
||||
num_cpus = "1.10"
|
||||
|
1
actix-threadpool/LICENSE-APACHE
Symbolic link
1
actix-threadpool/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-threadpool/LICENSE-MIT
Symbolic link
1
actix-threadpool/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
1
actix-tower/LICENSE-APACHE
Symbolic link
1
actix-tower/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-tower/LICENSE-MIT
Symbolic link
1
actix-tower/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -1,4 +1,60 @@
|
||||
//! Utilities to ease interoperability with services based on the `tower-service` crate.
|
||||
//! Utilities to provide interoperability between services based on the
|
||||
//! `actix-service` and `tower-service` crates.
|
||||
//!
|
||||
//! ## Example
|
||||
//!
|
||||
//! In the following example, we take a `RandomService`—which will always
|
||||
//! return 4—and wraps it with a middleware that will always add 1 to the
|
||||
//! result. This pattern can be further used to wrap services from either
|
||||
//! `tower-service` or `actix-service` with middleware provided by the other.
|
||||
//!
|
||||
//! ```
|
||||
//! use actix_tower::ActixServiceExt;
|
||||
//! # use futures::{Async, Future};
|
||||
//! use actix_service::Service;
|
||||
//!
|
||||
//! struct RandomService;
|
||||
//! impl Service for RandomService {
|
||||
//! // …
|
||||
//! # type Request = ();
|
||||
//! # type Response = u32;
|
||||
//! # type Error = ();
|
||||
//! # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
//! #
|
||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
//! # Ok(Async::Ready(()))
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
//! # futures::finished(4)
|
||||
//! # }
|
||||
//! }
|
||||
//!
|
||||
//! struct AddOneMiddleware<S>(S);
|
||||
//! impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
||||
//! where
|
||||
//! S: tower_service::Service<R, Response = u32>,
|
||||
//! S::Future: 'static,
|
||||
//! {
|
||||
//! /// …
|
||||
//! # type Response = u32;
|
||||
//! # type Error = S::Error;
|
||||
//! # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
//! #
|
||||
//! # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
//! # self.0.poll_ready()
|
||||
//! # }
|
||||
//! #
|
||||
//! # fn call(&mut self, req: R) -> Self::Future {
|
||||
//! # let fut = self.0.call(req).map(|x| x + 1);
|
||||
//! # Box::new(fut)
|
||||
//! # }
|
||||
//! }
|
||||
//!
|
||||
//! let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
||||
//! assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
//! assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
//! ```
|
||||
|
||||
use actix_service::Service as ActixService;
|
||||
use std::marker::PhantomData;
|
||||
@@ -6,40 +62,120 @@ use tower_service::Service as TowerService;
|
||||
|
||||
/// Compatibility wrapper associating a `tower_service::Service` with a particular
|
||||
/// `Request` type, so that it can be used as an `actix_service::Service`.
|
||||
pub struct TowerCompat<S, R> {
|
||||
///
|
||||
/// Generally created through convenience methods on the `TowerServiceExt<R>` trait.
|
||||
pub struct ActixCompat<S, R> {
|
||||
inner: S,
|
||||
_phantom: PhantomData<R>,
|
||||
}
|
||||
|
||||
impl<S, R> TowerCompat<S, R> {
|
||||
impl<S, R> ActixCompat<S, R> {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
pub fn new(inner: S) -> Self {
|
||||
TowerCompat {
|
||||
ActixCompat {
|
||||
inner,
|
||||
_phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for wrapping `tower_service::Service` instances for use as
|
||||
/// Extension trait for wrapping a `tower_service::Service` instance for use as
|
||||
/// an `actix_service::Service`.
|
||||
pub trait TowerServiceExt {
|
||||
pub trait TowerServiceExt<R>: TowerService<R> + Sized {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
fn compat<R>(self) -> TowerCompat<Self, R>
|
||||
where
|
||||
Self: TowerService<R> + Sized;
|
||||
}
|
||||
///
|
||||
/// ```
|
||||
/// use actix_service::Service;
|
||||
/// use actix_tower::TowerServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl<R> tower_service::Service<R> for RandomService {
|
||||
/// // …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.into_actix_service();
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
/// ```
|
||||
fn into_actix_service(self) -> ActixCompat<Self, R> {
|
||||
ActixCompat::new(self)
|
||||
}
|
||||
|
||||
impl<S> TowerServiceExt for S {
|
||||
fn compat<R>(self) -> TowerCompat<Self, R>
|
||||
/// Takes a function that, when provided with an `actix_service::Service` wraps it
|
||||
/// and returns a new service. Useful for wrapping a `tower_service::Service` with
|
||||
/// middleware built for `actix_service`.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::TowerServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use tower_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl<R> Service<R> for RandomService {
|
||||
/// // …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: R) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// struct AddOneTransform<S>(S);
|
||||
/// impl<S> actix_service::Service for AddOneTransform<S>
|
||||
/// where
|
||||
/// S: actix_service::Service<Response = u32>,
|
||||
/// S::Future: 'static,
|
||||
/// {
|
||||
/// /// …
|
||||
/// # type Request = S::Request;
|
||||
/// # type Response = u32;
|
||||
/// # type Error = S::Error;
|
||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # self.0.poll_ready()
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
||||
/// # Box::new(fut)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.wrap_with_actix_middleware(AddOneTransform);
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
/// ```
|
||||
fn wrap_with_actix_middleware<F, U>(self, f: F) -> TowerCompat<U>
|
||||
where
|
||||
Self: TowerService<R> + Sized
|
||||
F: FnOnce(ActixCompat<Self, R>) -> U,
|
||||
U: ActixService<Request = R>,
|
||||
{
|
||||
TowerCompat::new(self)
|
||||
f(self.into_actix_service()).into_tower_service()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, R> ActixService for TowerCompat<S, R>
|
||||
impl<S, R> TowerServiceExt<R> for S where S: TowerService<R> + Sized {}
|
||||
|
||||
impl<S, R> ActixService for ActixCompat<S, R>
|
||||
where
|
||||
S: TowerService<R>,
|
||||
{
|
||||
@@ -57,135 +193,388 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
/// Compatibility wrapper associating an `actix_service::Service` with a particular
|
||||
/// `Request` type, so that it can be used as a `tower_service::Service`.
|
||||
///
|
||||
/// Generally created through convenience methods on the `ActixServiceExt<R>` trait.
|
||||
pub struct TowerCompat<S> {
|
||||
inner: S,
|
||||
}
|
||||
|
||||
impl<S> TowerCompat<S> {
|
||||
/// Wraps an `actix_service::Service` in a compatibility wrapper.
|
||||
pub fn new(inner: S) -> Self {
|
||||
TowerCompat { inner }
|
||||
}
|
||||
}
|
||||
|
||||
/// Extension trait for wrapping an `actix_service::Service` instance for use as
|
||||
/// a `tower_service::Service`.
|
||||
pub trait ActixServiceExt: ActixService + Sized {
|
||||
/// Wraps a `tower_service::Service` in a compatibility wrapper.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::ActixServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use tower_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl actix_service::Service for RandomService {
|
||||
/// // …
|
||||
/// # type Request = ();
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.into_tower_service();
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
/// ```
|
||||
fn into_tower_service(self) -> TowerCompat<Self> {
|
||||
TowerCompat::new(self)
|
||||
}
|
||||
|
||||
/// Takes a function that, when provided with a `tower_service::Service` wraps it
|
||||
/// and returns a new service. Useful for wrapping an `actix_service::Service` with
|
||||
/// middleware built for `tower_service`.
|
||||
///
|
||||
/// ```
|
||||
/// use actix_tower::ActixServiceExt;
|
||||
/// # use futures::{Async, Future};
|
||||
/// use actix_service::Service;
|
||||
///
|
||||
/// struct RandomService;
|
||||
/// impl Service for RandomService {
|
||||
/// // …
|
||||
/// # type Request = ();
|
||||
/// # type Response = u32;
|
||||
/// # type Error = ();
|
||||
/// # type Future = futures::future::FutureResult<Self::Response, Self::Error>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # Ok(Async::Ready(()))
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
/// # futures::finished(4)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// struct AddOneMiddleware<S>(S);
|
||||
/// impl<S, R> tower_service::Service<R> for AddOneMiddleware<S>
|
||||
/// where
|
||||
/// S: tower_service::Service<R, Response = u32>,
|
||||
/// S::Future: 'static,
|
||||
/// {
|
||||
/// /// …
|
||||
/// # type Response = u32;
|
||||
/// # type Error = S::Error;
|
||||
/// # type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
/// #
|
||||
/// # fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
/// # self.0.poll_ready()
|
||||
/// # }
|
||||
/// #
|
||||
/// # fn call(&mut self, req: R) -> Self::Future {
|
||||
/// # let fut = self.0.call(req).map(|x| x + 1);
|
||||
/// # Box::new(fut)
|
||||
/// # }
|
||||
/// }
|
||||
///
|
||||
/// let mut s = RandomService.wrap_with_tower_middleware(AddOneMiddleware);
|
||||
/// assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
/// assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
/// ```
|
||||
fn wrap_with_tower_middleware<F, U>(self, f: F) -> ActixCompat<U, Self::Request>
|
||||
where
|
||||
F: FnOnce(TowerCompat<Self>) -> U,
|
||||
U: TowerService<Self::Request>,
|
||||
{
|
||||
f(self.into_tower_service()).into_actix_service()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> ActixServiceExt for S where S: ActixService + Sized {}
|
||||
|
||||
impl<S> TowerService<S::Request> for TowerCompat<S>
|
||||
where
|
||||
S: ActixService,
|
||||
{
|
||||
type Response = S::Response;
|
||||
type Error = S::Error;
|
||||
type Future = S::Future;
|
||||
|
||||
fn poll_ready(&mut self) -> futures::Poll<(), Self::Error> {
|
||||
ActixService::poll_ready(&mut self.inner)
|
||||
}
|
||||
|
||||
fn call(&mut self, req: S::Request) -> Self::Future {
|
||||
ActixService::call(&mut self.inner, req)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::TowerServiceExt;
|
||||
use actix_service::{Service as ActixService, ServiceExt, Transform};
|
||||
use futures::{future::FutureResult, Async, Poll, Future};
|
||||
use tower_service::Service as TowerService;
|
||||
mod tower_service_into_actix_service {
|
||||
use crate::TowerServiceExt;
|
||||
use actix_service::{Service as ActixService, ServiceExt, Transform};
|
||||
use futures::{future::FutureResult, Async, Future, Poll};
|
||||
use tower_service::Service as TowerService;
|
||||
|
||||
struct RandomService;
|
||||
impl<R> TowerService<R> for RandomService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
#[test]
|
||||
fn random_service_returns_4() {
|
||||
let mut s = RandomService.into_actix_service();
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, _req: R) -> Self::Future {
|
||||
futures::finished(4)
|
||||
#[test]
|
||||
fn random_service_can_combine() {
|
||||
let mut s = RandomService.into_actix_service().map(|x| x + 1);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_can_use_actix_middleware() {
|
||||
let mut s = RandomService.wrap_with_actix_middleware(DoMathTransform);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_and_add_service_chained() {
|
||||
let s1 = RandomService.into_actix_service();
|
||||
let s2 = AddOneService.into_actix_service();
|
||||
let s3 = AddOneService.into_actix_service();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(6)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
||||
let s1 = RandomService.into_actix_service();
|
||||
let s2 = AddOneService.into_actix_service();
|
||||
let s3 = AddOneService.into_actix_service();
|
||||
let s4 = RandomService.into_actix_service();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3).and_then(s4);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_can_be_transformed_to_do_math() {
|
||||
let transform = DoMath;
|
||||
|
||||
let mut s = transform
|
||||
.new_transform(RandomService.into_actix_service())
|
||||
.wait()
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct RandomService;
|
||||
impl<R> TowerService<R> for RandomService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _req: R) -> Self::Future {
|
||||
futures::finished(4)
|
||||
}
|
||||
}
|
||||
|
||||
struct AddOneService;
|
||||
impl TowerService<u32> for AddOneService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: u32) -> Self::Future {
|
||||
futures::finished(req + 1)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMathTransform<S>(S);
|
||||
impl<S> ActixService for DoMathTransform<S>
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.0.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
let fut = self.0.call(req).map(|x| x * 17);
|
||||
Box::new(fut)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMath;
|
||||
impl<S> Transform<S> for DoMath
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Transform = DoMathTransform<S>;
|
||||
type InitError = ();
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
futures::finished(DoMathTransform(service))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_returns_4() {
|
||||
let mut s = RandomService.compat();
|
||||
mod actix_service_into_tower_service {
|
||||
use crate::{ActixServiceExt, TowerServiceExt};
|
||||
use actix_service::{Service as ActixService, ServiceExt};
|
||||
use futures::{future::FutureResult, Async, Future, Poll};
|
||||
use tower_service::Service as TowerService;
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
#[test]
|
||||
fn random_service_returns_4() {
|
||||
let mut s = RandomService.into_tower_service();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_can_combine() {
|
||||
let mut s = RandomService.compat().map(|x| x + 1);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct AddOneService;
|
||||
impl TowerService<u32> for AddOneService {
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, req: u32) -> Self::Future {
|
||||
futures::finished(req + 1)
|
||||
}
|
||||
}
|
||||
#[test]
|
||||
fn random_service_can_use_tower_middleware() {
|
||||
let mut s =
|
||||
AddOneService::wrap(RandomService.into_tower_service()).into_actix_service();
|
||||
|
||||
#[test]
|
||||
fn tower_services_as_actix_services_chained() {
|
||||
let s1 = RandomService.compat();
|
||||
let s2 = AddOneService.compat();
|
||||
let s3 = AddOneService.compat();
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(6)), s.call(()).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_services_as_actix_services_chained_2() {
|
||||
let s1 = RandomService.compat();
|
||||
let s2 = AddOneService.compat();
|
||||
let s3 = AddOneService.compat();
|
||||
let s4 = RandomService.compat();
|
||||
|
||||
let mut s = s1.and_then(s2).and_then(s3).and_then(s4);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(4)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct DoMathTransform<S>(S);
|
||||
impl<S> ActixService for DoMathTransform<S>
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.0.poll_ready()
|
||||
assert_eq!(Ok(Async::Ready(5)), s.call(()).poll());
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
let fut = self.0.call(req).map(|x| x * 17);
|
||||
Box::new(fut)
|
||||
#[test]
|
||||
fn do_math_service_can_use_tower_middleware() {
|
||||
let mut s =
|
||||
AddOneService::wrap(DoMathService.into_tower_service()).into_actix_service();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(188)), s.call(11).poll());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn random_service_and_add_service_and_ignoring_service_chained() {
|
||||
let s1 = RandomService.wrap_with_tower_middleware(AddOneService::wrap);
|
||||
let s2 = DoMathService.wrap_with_tower_middleware(AddOneService::wrap);
|
||||
|
||||
let mut s = s1.and_then(s2);
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(86)), s.call(()).poll());
|
||||
}
|
||||
|
||||
struct RandomService;
|
||||
impl ActixService for RandomService {
|
||||
type Request = ();
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, _req: Self::Request) -> Self::Future {
|
||||
futures::finished(4)
|
||||
}
|
||||
}
|
||||
|
||||
struct AddOneService<S> {
|
||||
inner: S,
|
||||
}
|
||||
|
||||
impl<S> AddOneService<S> {
|
||||
fn wrap(inner: S) -> Self {
|
||||
AddOneService { inner }
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, R> TowerService<R> for AddOneService<S>
|
||||
where
|
||||
S: TowerService<R, Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Future = Box<dyn Future<Item = Self::Response, Error = Self::Error>>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
self.inner.poll_ready()
|
||||
}
|
||||
|
||||
fn call(&mut self, req: R) -> Self::Future {
|
||||
let fut = self.inner.call(req).map(|x| x + 1);
|
||||
|
||||
Box::new(fut)
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMathService;
|
||||
impl ActixService for DoMathService {
|
||||
type Request = u32;
|
||||
type Response = u32;
|
||||
type Error = ();
|
||||
type Future = FutureResult<Self::Response, Self::Error>;
|
||||
|
||||
fn poll_ready(&mut self) -> Poll<(), Self::Error> {
|
||||
Ok(Async::Ready(()))
|
||||
}
|
||||
|
||||
fn call(&mut self, req: Self::Request) -> Self::Future {
|
||||
futures::finished(req * 17)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct DoMath;
|
||||
impl<S> Transform<S> for DoMath
|
||||
where
|
||||
S: ActixService<Response = u32>,
|
||||
S::Future: 'static,
|
||||
{
|
||||
type Request = S::Request;
|
||||
type Response = u32;
|
||||
type Error = S::Error;
|
||||
type Transform = DoMathTransform<S>;
|
||||
type InitError = ();
|
||||
type Future = FutureResult<Self::Transform, Self::InitError>;
|
||||
|
||||
fn new_transform(&self, service: S) -> Self::Future {
|
||||
futures::finished(DoMathTransform(service))
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tower_service_as_actix_service_can_be_transformed() {
|
||||
let transform = DoMath;
|
||||
|
||||
let mut s = transform.new_transform(RandomService.compat()).wait().unwrap();
|
||||
|
||||
assert_eq!(Ok(Async::Ready(())), s.poll_ready());
|
||||
|
||||
assert_eq!(Ok(Async::Ready(68)), s.call(()).poll());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,19 @@
|
||||
# Changes
|
||||
|
||||
## [0.4.2] - 2019-06-26
|
||||
|
||||
### Fixed
|
||||
|
||||
* Do not block on sink drop for FramedTransport
|
||||
|
||||
|
||||
## [0.4.1] - 2019-05-15
|
||||
|
||||
### Changed
|
||||
|
||||
* Change `Either` constructor
|
||||
|
||||
|
||||
## [0.4.0] - 2019-05-11
|
||||
|
||||
### Changed
|
||||
|
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "actix-utils"
|
||||
version = "0.4.0"
|
||||
version = "0.4.2"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Actix utils - various actix net related services"
|
||||
keywords = ["network", "framework", "async", "futures"]
|
||||
@@ -18,8 +18,8 @@ name = "actix_utils"
|
||||
path = "src/lib.rs"
|
||||
|
||||
[dependencies]
|
||||
actix-service = "0.4.0"
|
||||
actix-codec = "0.1.1"
|
||||
actix-service = "0.4.1"
|
||||
actix-codec = "0.1.2"
|
||||
bytes = "0.4"
|
||||
either = "1.5.2"
|
||||
futures = "0.1.25"
|
||||
|
1
actix-utils/LICENSE-APACHE
Symbolic link
1
actix-utils/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
actix-utils/LICENSE-MIT
Symbolic link
1
actix-utils/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -57,25 +57,21 @@ pub struct Either<A, B> {
|
||||
}
|
||||
|
||||
impl<A, B> Either<A, B> {
|
||||
pub fn new_a<F>(srv: F) -> Either<A, ()>
|
||||
pub fn new<F1, F2>(srv_a: F1, srv_b: F2) -> Either<A, B>
|
||||
where
|
||||
A: NewService,
|
||||
F: IntoNewService<A>,
|
||||
B: NewService<
|
||||
Config = A::Config,
|
||||
Response = A::Response,
|
||||
Error = A::Error,
|
||||
InitError = A::InitError,
|
||||
>,
|
||||
F1: IntoNewService<A>,
|
||||
F2: IntoNewService<B>,
|
||||
{
|
||||
Either {
|
||||
left: srv.into_new_service(),
|
||||
right: (),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_b<F>(srv: F) -> Either<(), B>
|
||||
where
|
||||
B: NewService,
|
||||
F: IntoNewService<B>,
|
||||
{
|
||||
Either {
|
||||
left: (),
|
||||
right: srv.into_new_service(),
|
||||
left: srv_a.into_new_service(),
|
||||
right: srv_b.into_new_service(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -187,10 +187,12 @@ where
|
||||
return true;
|
||||
}
|
||||
Ok(Async::Ready(None)) => {
|
||||
rx_done = true;
|
||||
let _ = self.rx.take();
|
||||
}
|
||||
Ok(Async::NotReady) => rx_done = true,
|
||||
Err(_e) => {
|
||||
rx_done = true;
|
||||
let _ = self.rx.take();
|
||||
}
|
||||
}
|
||||
|
@@ -1,5 +1,13 @@
|
||||
# Changes
|
||||
|
||||
## [0.1.5] - 2019-05-15
|
||||
|
||||
* Remove debug prints
|
||||
|
||||
## [0.1.4] - 2019-05-15
|
||||
|
||||
* Fix checked resource match
|
||||
|
||||
## [0.1.3] - 2019-04-22
|
||||
|
||||
* Added support for `remainder match` (i.e "/path/{tail}*")
|
||||
|
@@ -1,8 +1,8 @@
|
||||
[package]
|
||||
name = "actix-router"
|
||||
version = "0.1.3"
|
||||
version = "0.1.5"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
description = "Path table router"
|
||||
description = "Path router"
|
||||
keywords = ["actix"]
|
||||
homepage = "https://actix.rs"
|
||||
repository = "https://github.com/actix/actix-net.git"
|
||||
|
1
router/LICENSE-APACHE
Symbolic link
1
router/LICENSE-APACHE
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-APACHE
|
1
router/LICENSE-MIT
Symbolic link
1
router/LICENSE-MIT
Symbolic link
@@ -0,0 +1 @@
|
||||
../LICENSE-MIT
|
@@ -5,7 +5,7 @@ use std::rc::Rc;
|
||||
use regex::{escape, Regex};
|
||||
|
||||
use crate::path::{Path, PathItem};
|
||||
use crate::ResourcePath;
|
||||
use crate::{Resource, ResourcePath};
|
||||
|
||||
const MAX_DYNAMIC_SEGMENTS: usize = 16;
|
||||
|
||||
@@ -240,6 +240,90 @@ impl ResourceDef {
|
||||
}
|
||||
}
|
||||
|
||||
/// Is the given path and parameters a match against this pattern?
|
||||
pub fn match_path_checked<R, T, F, U>(
|
||||
&self,
|
||||
res: &mut R,
|
||||
check: &F,
|
||||
user_data: &Option<U>,
|
||||
) -> bool
|
||||
where
|
||||
T: ResourcePath,
|
||||
R: Resource<T>,
|
||||
F: Fn(&R, &Option<U>) -> bool,
|
||||
{
|
||||
match self.tp {
|
||||
PatternType::Static(ref s) => {
|
||||
if s == res.resource_path().path() && check(res, user_data) {
|
||||
let path = res.resource_path();
|
||||
path.skip(path.len() as u16);
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
PatternType::Dynamic(ref re, ref names, len) => {
|
||||
let mut idx = 0;
|
||||
let mut pos = 0;
|
||||
let mut segments: [PathItem; MAX_DYNAMIC_SEGMENTS] =
|
||||
[PathItem::Static(""); MAX_DYNAMIC_SEGMENTS];
|
||||
|
||||
if let Some(captures) = re.captures(res.resource_path().path()) {
|
||||
for (no, name) in names.iter().enumerate() {
|
||||
if let Some(m) = captures.name(&name) {
|
||||
idx += 1;
|
||||
pos = m.end();
|
||||
segments[no] = PathItem::Segment(m.start() as u16, m.end() as u16);
|
||||
} else {
|
||||
log::error!(
|
||||
"Dynamic path match but not all segments found: {}",
|
||||
name
|
||||
);
|
||||
false;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
if !check(res, user_data) {
|
||||
return false;
|
||||
}
|
||||
|
||||
let path = res.resource_path();
|
||||
for idx in 0..idx {
|
||||
path.add(names[idx].clone(), segments[idx]);
|
||||
}
|
||||
path.skip((pos + len) as u16);
|
||||
true
|
||||
}
|
||||
PatternType::Prefix(ref s) => {
|
||||
let len = {
|
||||
let rpath = res.resource_path().path();
|
||||
if s == rpath {
|
||||
s.len()
|
||||
} else if rpath.starts_with(s)
|
||||
&& (s.ends_with('/') || rpath.split_at(s.len()).1.starts_with('/'))
|
||||
{
|
||||
if s.ends_with('/') {
|
||||
s.len() - 1
|
||||
} else {
|
||||
s.len()
|
||||
}
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
if !check(res, user_data) {
|
||||
return false;
|
||||
}
|
||||
let path = res.resource_path();
|
||||
path.skip(min(path.path().len(), len) as u16);
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Build resource path from elements. Returns `true` on success.
|
||||
pub fn resource_path<U, I>(&self, path: &mut String, elements: &mut U) -> bool
|
||||
where
|
||||
|
@@ -19,26 +19,26 @@ impl<T, U> Router<T, U> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn recognize<R, P>(&self, path: &mut R) -> Option<(&T, ResourceId)>
|
||||
pub fn recognize<R, P>(&self, resource: &mut R) -> Option<(&T, ResourceId)>
|
||||
where
|
||||
R: Resource<P>,
|
||||
P: ResourcePath,
|
||||
{
|
||||
for item in self.0.iter() {
|
||||
if item.0.match_path(path.resource_path()) {
|
||||
if item.0.match_path(resource.resource_path()) {
|
||||
return Some((&item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn recognize_mut<R, P>(&mut self, res: &mut R) -> Option<(&mut T, ResourceId)>
|
||||
pub fn recognize_mut<R, P>(&mut self, resource: &mut R) -> Option<(&mut T, ResourceId)>
|
||||
where
|
||||
R: Resource<P>,
|
||||
P: ResourcePath,
|
||||
{
|
||||
for item in self.0.iter_mut() {
|
||||
if item.0.match_path(res.resource_path()) {
|
||||
if item.0.match_path(resource.resource_path()) {
|
||||
return Some((&mut item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
@@ -47,7 +47,7 @@ impl<T, U> Router<T, U> {
|
||||
|
||||
pub fn recognize_mut_checked<R, P, F>(
|
||||
&mut self,
|
||||
res: &mut R,
|
||||
resource: &mut R,
|
||||
check: F,
|
||||
) -> Option<(&mut T, ResourceId)>
|
||||
where
|
||||
@@ -56,7 +56,7 @@ impl<T, U> Router<T, U> {
|
||||
P: ResourcePath,
|
||||
{
|
||||
for item in self.0.iter_mut() {
|
||||
if item.0.match_path(res.resource_path()) && check(res, &item.2) {
|
||||
if item.0.match_path_checked(resource, &check, &item.2) {
|
||||
return Some((&mut item.1, ResourceId(item.0.id())));
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user