Skip to content

Commit

Permalink
Make Service::call() take &mut self
Browse files Browse the repository at this point in the history
  • Loading branch information
petoknm authored and reu committed Jan 28, 2025
1 parent edd8e2d commit 71f7d5e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 16 deletions.
2 changes: 1 addition & 1 deletion examples/expect-continue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl Service for UploadService {
type Body = &'static str;
type Error = Infallible;

fn call(&self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
fn call(&mut self, _req: Request<Body>) -> Result<http::Response<Self::Body>, Self::Error> {
Ok(Response::builder()
.status(StatusCode::OK)
.body("Thanks for the info!")
Expand Down
27 changes: 12 additions & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub trait Service {
type Body: HttpBody;
type Error: Into<Box<dyn Error + Send + Sync>>;

fn call(&self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error>;
fn call(&mut self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error>;

fn should_continue(&self, _: &IncomingRequest) -> StatusCode {
StatusCode::CONTINUE
Expand All @@ -104,14 +104,14 @@ pub trait Service {

impl<F, Body, Err> Service for F
where
F: Fn(IncomingRequest) -> Result<Response<Body>, Err>,
F: FnMut(IncomingRequest) -> Result<Response<Body>, Err>,
Body: HttpBody,
Err: Into<Box<dyn Error + Send + Sync>>,
{
type Body = Body;
type Error = Err;

fn call(&self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error> {
fn call(&mut self, request: IncomingRequest) -> Result<Response<Self::Body>, Self::Error> {
self(request)
}
}
Expand Down Expand Up @@ -163,9 +163,9 @@ impl Server<'_> {
S: Send + Clone + 'static,
{
for conn in self.incoming {
let app = service.clone();
let mut app = service.clone();
self.thread_pool.execute(move || {
serve(conn, app).ok();
serve(conn, &mut app).ok();
});
}

Expand All @@ -187,13 +187,12 @@ impl Server<'_> {
/// })
/// # }
/// ```
pub fn serve_single_thread<S>(self, service: S) -> io::Result<()>
pub fn serve_single_thread<S>(self, mut service: S) -> io::Result<()>
where
S: Service + Clone,
S: Service,
{
for conn in self.incoming {
let app = service.clone();
serve(conn, app).ok();
serve(conn, &mut service).ok();
}
Ok(())
}
Expand Down Expand Up @@ -221,15 +220,13 @@ impl Server<'_> {
/// ```
pub fn make_service<M>(self, make_service: M) -> io::Result<()>
where
M: MakeService,
M: Clone + 'static,
M: MakeService + 'static,
<M as MakeService>::Service: Send,
{
for conn in self.incoming {
let app = make_service.clone();
if let Ok(handler) = app.call(&conn) {
if let Ok(mut handler) = make_service.call(&conn) {
self.thread_pool.execute(move || {
serve(conn, handler).ok();
serve(conn, &mut handler).ok();
});
}
}
Expand Down Expand Up @@ -404,7 +401,7 @@ where
}
}

fn serve<C: Into<Connection>, A: Service>(stream: C, app: A) -> io::Result<()> {
fn serve<C: Into<Connection>, A: Service>(stream: C, app: &mut A) -> io::Result<()> {
let conn = stream.into();
let mut read_queue = ReadQueue::new(BufReader::new(conn.clone()));

Expand Down

0 comments on commit 71f7d5e

Please sign in to comment.