Skip to content

Commit

Permalink
add ResponseSender::close shortcut.
Browse files Browse the repository at this point in the history
  • Loading branch information
fakeshadow committed Feb 27, 2024
1 parent 2265363 commit 2092992
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 2 deletions.
1 change: 1 addition & 0 deletions http-ws/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
## Add
- add `RequestStream::inner_mut` method for accessing inner stream type.
- add `RequestStream::codec_mut` method for accessing `Codec` type.
- add `ResponseSender::close` method for sending close message.

## Change
- reduce `stream::RequestStream`'s generic type params.
Expand Down
10 changes: 10 additions & 0 deletions http-ws/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use tokio::sync::mpsc::{channel, Receiver, Sender};
use super::{
codec::{Codec, Message},
error::ProtocolError,
proto::CloseReason,
};

pin_project! {
Expand Down Expand Up @@ -239,6 +240,15 @@ impl ResponseSender {
pub fn binary(&self, bin: impl Into<Bytes>) -> impl Future<Output = Result<(), ProtocolError>> + '_ {
self.send(Message::Binary(bin.into()))
}

/// encode [Message::Close] variant and add to [ResponseStream].
/// take ownership of Self as after close message no more message can be sent to client.
pub fn close(
self,
reason: Option<impl Into<CloseReason> + 'static>,
) -> impl Future<Output = Result<(), ProtocolError>> + 'static {

Check warning on line 249 in http-ws/src/stream.rs

View workflow job for this annotation

GitHub Actions / clippy

this function can be simplified using the `async fn` syntax

warning: this function can be simplified using the `async fn` syntax --> http-ws/src/stream.rs:246:5 | 246 | / pub fn close( 247 | | self, 248 | | reason: Option<impl Into<CloseReason> + 'static>, 249 | | ) -> impl Future<Output = Result<(), ProtocolError>> + 'static { | |__________________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn = note: `#[warn(clippy::manual_async_fn)]` on by default help: make the function `async` and return the output of the future directly | 246 ~ pub async fn close( 247 + self, 248 + reason: Option<impl Into<CloseReason> + 'static>, 249 ~ ) -> Result<(), ProtocolError> { | help: move the body of the async block to the enclosing function | 249 | ) -> impl Future<Output = Result<(), ProtocolError>> + 'static { self.send(Message::Close(reason.map(Into::into))).await } | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Check warning on line 249 in http-ws/src/stream.rs

View workflow job for this annotation

GitHub Actions / clippy

this function can be simplified using the `async fn` syntax

warning: this function can be simplified using the `async fn` syntax --> http-ws/src/stream.rs:246:5 | 246 | / pub fn close( 247 | | self, 248 | | reason: Option<impl Into<CloseReason> + 'static>, 249 | | ) -> impl Future<Output = Result<(), ProtocolError>> + 'static { | |__________________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_async_fn = note: `#[warn(clippy::manual_async_fn)]` on by default help: make the function `async` and return the output of the future directly | 246 ~ pub async fn close( 247 + self, 248 + reason: Option<impl Into<CloseReason> + 'static>, 249 ~ ) -> Result<(), ProtocolError> { | help: move the body of the async block to the enclosing function | 249 | ) -> impl Future<Output = Result<(), ProtocolError>> + 'static { self.send(Message::Close(reason.map(Into::into))).await } | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
async move { self.send(Message::Close(reason.map(Into::into))).await }
}
}

/// [Weak] version of [ResponseSender].
Expand Down
4 changes: 2 additions & 2 deletions http/src/util/service/route.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ impl<R, N, const M: usize> ReadyService for RouteService<R, N, M> {
}

/// Error type of Method not allow for route.
pub struct MethodNotAllowed(pub Vec<Method>);
pub struct MethodNotAllowed(pub Box<Vec<Method>>);

impl MethodNotAllowed {
/// slice of allowed methods of current route.
Expand Down Expand Up @@ -229,7 +229,7 @@ where
type Error = RouterError<R::Error>;

async fn call(&self, _: Req) -> Result<Self::Response, Self::Error> {
Err(RouterError::NotAllowed(MethodNotAllowed(Vec::new())))
Err(RouterError::NotAllowed(MethodNotAllowed(Box::new(Vec::new()))))

Check warning on line 232 in http/src/util/service/route.rs

View workflow job for this annotation

GitHub Actions / clippy

`Box::new(_)` of default value

warning: `Box::new(_)` of default value --> http/src/util/service/route.rs:232:54 | 232 | Err(RouterError::NotAllowed(MethodNotAllowed(Box::new(Vec::new())))) | ^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#box_default = note: `#[warn(clippy::box_default)]` on by default

Check warning on line 232 in http/src/util/service/route.rs

View workflow job for this annotation

GitHub Actions / clippy

`Box::new(_)` of default value

warning: `Box::new(_)` of default value --> http/src/util/service/route.rs:232:54 | 232 | Err(RouterError::NotAllowed(MethodNotAllowed(Box::new(Vec::new())))) | ^^^^^^^^^^^^^^^^^^^^ help: try: `Box::default()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#box_default = note: `#[warn(clippy::box_default)]` on by default
}
}

Expand Down

0 comments on commit 2092992

Please sign in to comment.