Skip to content

Commit

Permalink
Merge pull request #74 from hubertshelley/dependabot/cargo/tokio-tung…
Browse files Browse the repository at this point in the history
…stenite-0.25.0

chore(deps): update tokio-tungstenite requirement from 0.24.0 to 0.25.0
  • Loading branch information
hubertshelley authored Dec 17, 2024
2 parents 2988a4d + c793690 commit 8fd17dd
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
2 changes: 1 addition & 1 deletion examples/websocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"
async-trait = "0.1.83"
silent = { path = "../../silent", features = ["upgrade"] }
tokio = { version = "1.41.0", features = ["full"] }
tokio-tungstenite = "0.24.0"
tokio-tungstenite = "0.25.0"
futures-util = "0.3.31"
backtrace = "0.3.74"
headers = "0.4.0"
2 changes: 1 addition & 1 deletion silent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ serde_html_form = "0.2.6"
mime = "0.3.17"
futures-util = "0.3.31"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
tokio-tungstenite = { version = "0.24.0", optional = true }
tokio-tungstenite = { version = "0.25.0", optional = true }
headers = "0.4.0"
tokio-stream = { version = "0.1.16", features = ["net"], optional = true }
pin-project = { version = "1.1", optional = true }
Expand Down
1 change: 1 addition & 0 deletions silent/src/route/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct RootHandler {
inner: RouteMatched,
middlewares: Vec<Arc<dyn MiddleWareHandler>>,
}

#[async_trait]
impl Handler for RootHandler {
async fn call(&self, req: Request) -> Result<Response, SilentError> {
Expand Down
43 changes: 30 additions & 13 deletions silent/src/ws/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,56 @@ use crate::{Result, SilentError};
use std::borrow::Cow;
use std::fmt;
use std::fmt::Formatter;
use std::ops::Deref;
use tokio_tungstenite::tungstenite::protocol;
use tokio_tungstenite::tungstenite::protocol::frame::{Payload, Utf8Payload};

#[derive(Eq, PartialEq, Clone)]
pub struct Message {
pub(crate) inner: protocol::Message,
}

impl Deref for Message {
type Target = protocol::Message;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

impl Message {
/// Construct a new Text `Message`.
#[inline]
pub fn text<S: Into<String>>(s: S) -> Message {
pub fn text<S: Into<Utf8Payload>>(s: S) -> Message {
Message {
inner: protocol::Message::text(s),
}
}

/// Construct a new Binary `Message`.
#[inline]
pub fn binary<V: Into<Vec<u8>>>(v: V) -> Message {
pub fn binary<V: Into<Payload>>(v: V) -> Message {
Message {
inner: protocol::Message::binary(v),
inner: protocol::Message::binary(v.into()),
}
}

/// Construct a new Ping `Message`.
#[inline]
pub fn ping<V: Into<Vec<u8>>>(v: V) -> Message {
pub fn ping<V: Into<Payload>>(v: V) -> Message {
Message {
inner: protocol::Message::Ping(v.into()),
}
}

/// Construct a new pong `Message`.
#[inline]
pub fn pong<V: Into<Payload>>(v: V) -> Message {
Message {
inner: protocol::Message::Pong(v.into()),
}
}

/// Construct the default Close `Message`.
#[inline]
pub fn close() -> Message {
Expand Down Expand Up @@ -96,20 +114,19 @@ impl Message {
/// Try to get a reference to the string text, if this is a Text message.
#[inline]
pub fn to_str(&self) -> Result<&str> {
match self.inner {
protocol::Message::Text(ref s) => Ok(s),
_ => Err(SilentError::WsError("not a text message".into())),
}
self.inner
.to_text()
.map_err(|_| SilentError::WsError("not a text message".into()))
}

/// Returns the bytes of this message, if the message can contain data.
#[inline]
pub fn as_bytes(&self) -> &[u8] {
match self.inner {
protocol::Message::Text(ref s) => s.as_bytes(),
protocol::Message::Binary(ref v) => v,
protocol::Message::Ping(ref v) => v,
protocol::Message::Pong(ref v) => v,
protocol::Message::Text(ref s) => s.as_slice(),
protocol::Message::Binary(ref v) => v.as_slice(),
protocol::Message::Ping(ref v) => v.as_slice(),
protocol::Message::Pong(ref v) => v.as_slice(),
protocol::Message::Close(_) => &[],
protocol::Message::Frame(ref v) => v.payload(),
}
Expand All @@ -118,7 +135,7 @@ impl Message {
/// Destructure this message into binary data.
#[inline]
pub fn into_bytes(self) -> Vec<u8> {
self.inner.into_data()
self.inner.into_data().as_slice().to_vec()
}
}

Expand Down

0 comments on commit 8fd17dd

Please sign in to comment.