From fef95bf37a138cdf94985e17f27fd36481525171 Mon Sep 17 00:00:00 2001 From: Danny <1695103+Rapptz@users.noreply.github.com> Date: Mon, 26 Sep 2022 15:07:12 -0400 Subject: [PATCH] Add From impls for extract::ws::Message (#1421) * Add From impls for extract::ws::Message These come from tungstenite but were not exposed by axum * Add changelog entry --- axum/CHANGELOG.md | 3 +++ axum/src/extract/ws.rs | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index 7483c6c3..2589459b 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -40,6 +40,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 you likely need to re-enable the `tokio` feature ([#1382]) - **breaking:** `handler::{WithState, IntoService}` are merged into one type, named `HandlerService` ([#1418]) +- **added:** String and binary `From` impls have been added to `extract::ws::Message` + to be more inline with `tungstenite` ([#1421]) [#1368]: https://github.com/tokio-rs/axum/pull/1368 [#1371]: https://github.com/tokio-rs/axum/pull/1371 @@ -52,6 +54,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 [#1408]: https://github.com/tokio-rs/axum/pull/1408 [#1414]: https://github.com/tokio-rs/axum/pull/1414 [#1418]: https://github.com/tokio-rs/axum/pull/1418 +[#1421]: https://github.com/tokio-rs/axum/pull/1421 # 0.6.0-rc.2 (10. September, 2022) diff --git a/axum/src/extract/ws.rs b/axum/src/extract/ws.rs index 4156a16c..a48f9a73 100644 --- a/axum/src/extract/ws.rs +++ b/axum/src/extract/ws.rs @@ -545,6 +545,30 @@ impl Message { } } +impl From for Message { + fn from(string: String) -> Self { + Message::Text(string) + } +} + +impl<'s> From<&'s str> for Message { + fn from(string: &'s str) -> Self { + Message::Text(string.into()) + } +} + +impl<'b> From<&'b [u8]> for Message { + fn from(data: &'b [u8]) -> Self { + Message::Binary(data.into()) + } +} + +impl From> for Message { + fn from(data: Vec) -> Self { + Message::Binary(data) + } +} + impl From for Vec { fn from(msg: Message) -> Self { msg.into_data()