Skip to content

Commit 322fb2a

Browse files
author
rusty
committed
Make stopper optional
1 parent c61bb90 commit 322fb2a

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

src/listener/tcp_listener.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use async_std::net::{self, SocketAddr, TcpStream};
99
use async_std::prelude::*;
1010
use async_std::{io, task};
1111

12+
use futures_util::future::Either;
13+
1214
/// This represents a tide [Listener](crate::listener::Listener) that
1315
/// wraps an [async_std::net::TcpListener]. It is implemented as an
1416
/// enum in order to allow creation of a tide::listener::TcpListener
@@ -98,7 +100,12 @@ where
98100
.take()
99101
.expect("`Listener::bind` must be called before `Listener::accept`");
100102

101-
let mut incoming = server.stopper.clone().stop_stream(listener.incoming());
103+
let incoming = listener.incoming();
104+
let mut incoming = if let Some(stopper) = server.stopper.clone() {
105+
Either::Left(stopper.stop_stream(incoming))
106+
} else {
107+
Either::Right(incoming)
108+
};
102109

103110
while let Some(stream) = incoming.next().await {
104111
match stream {

src/listener/unix_listener.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use async_std::path::PathBuf;
1010
use async_std::prelude::*;
1111
use async_std::{io, task};
1212

13+
use futures_util::future::Either;
14+
1315
/// This represents a tide [Listener](crate::listener::Listener) that
1416
/// wraps an [async_std::os::unix::net::UnixListener]. It is implemented as an
1517
/// enum in order to allow creation of a tide::listener::UnixListener
@@ -96,7 +98,12 @@ where
9698
.take()
9799
.expect("`Listener::bind` must be called before `Listener::accept`");
98100

99-
let mut incoming = server.stopper.clone().stop_stream(listener.incoming());
101+
let incoming = listener.incoming();
102+
let mut incoming = if let Some(stopper) = server.stopper.clone() {
103+
Either::Left(stopper.stop_stream(incoming))
104+
} else {
105+
Either::Right(incoming)
106+
};
100107

101108
while let Some(stream) = incoming.next().await {
102109
match stream {

src/server.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ pub struct Server<State> {
4040
/// We don't use a Mutex around the Vec here because adding a middleware during execution should be an error.
4141
#[allow(clippy::rc_buffer)]
4242
middleware: Arc<Vec<Arc<dyn Middleware<State>>>>,
43-
pub(crate) stopper: Stopper,
43+
pub(crate) stopper: Option<Stopper>,
4444
}
4545

4646
impl Server<()> {
@@ -116,7 +116,7 @@ where
116116
Arc::new(log::LogMiddleware::new()),
117117
]),
118118
state,
119-
stopper: Stopper::new(),
119+
stopper: None,
120120
}
121121
}
122122

@@ -342,7 +342,7 @@ where
342342
/// stopper.stop();
343343
/// ```
344344
pub fn with_stopper(&mut self, stopper: Stopper) -> &mut Self {
345-
self.stopper = stopper;
345+
self.stopper = Some(stopper);
346346
self
347347
}
348348
}

0 commit comments

Comments
 (0)