From 8657eef547ae82569c65b3921d8804d85c19d0bf Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Fri, 15 Mar 2024 23:56:37 +0800 Subject: [PATCH 1/5] Add async_any_callback and async_callback macro with it's docs --- CHANGELOG.md | 9 +++++- socketio/src/asynchronous/mod.rs | 48 ++++++++++++++++++++++++++++++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index acbb3aff..b16229cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,10 +34,17 @@ The format is based on [Keep a Changelog], and this project adheres to ## _[Unreleased]_ -_nothing new to show for… yet!_ +_nothing new to show for… yet!_> + +_async callback macro_ + +_2024.03.15_ + +- Add macro `async_callback` and `async_any_callback` for async callbacks [#395](https://github.com/1c3t3a/rust-socketio/issue/395) [#399](https://github.com/1c3t3a/rust-socketio/pull/399) ## [0.4.4] - _Bump dependencies_ + _2023.11.18_ - Bump tungstenite version to v0.20.1 (avoiding security vulnerability) [#368](https://github.com/1c3t3a/rust-socketio/pull/368) diff --git a/socketio/src/asynchronous/mod.rs b/socketio/src/asynchronous/mod.rs index 2976dd9a..2a6cbcb7 100644 --- a/socketio/src/asynchronous/mod.rs +++ b/socketio/src/asynchronous/mod.rs @@ -4,5 +4,49 @@ mod socket; #[cfg(feature = "async")] pub use client::builder::ClientBuilder; -pub use client::client::Client; -pub use client::client::ReconnectSettings; +pub use client::client::{Client, ReconnectSettings}; + + +#[doc = r#" +A macro to wrap a async callback function to be used in the client. + +This macro is used to wrap a callback function that can handle any event. + +```rust +pub async fn callback(payload: Payload, client: Client) {} + +let socket = ClientBuilder::new(host) + .on("message", async_callback!(callback)) + .connect() + .await + .expect("Connection failed"); +``` +"#] +#[macro_export] +macro_rules! async_callback { + ($f:expr) => { + |payload: Payload, client: Client| $f(payload, client).boxed() + }; +} + +#[doc = r#" +A macro to wrap a async callback function to be used in the client. + +This macro is used to wrap a callback function that can handle any event. + +```rust +pub async fn callback_any(event: Event, payload: Payload, client: Client) {} + +let socket = ClientBuilder::new(host) + .on_any(async_any_callback!(callback_any)) + .connect() + .await + .expect("Connection failed"); +``` +"#] +#[macro_export] +macro_rules! async_any_callback { + ($f:expr) => { + |event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed() + }; +} From 923a74f6157d402a066ceef133884a89384d2a32 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 16 Mar 2024 00:42:06 +0800 Subject: [PATCH 2/5] Fix typing, and remove new line from changelog --- CHANGELOG.md | 1 - socketio/src/asynchronous/mod.rs | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b16229cc..20029e75 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,6 @@ _2024.03.15_ ## [0.4.4] - _Bump dependencies_ - _2023.11.18_ - Bump tungstenite version to v0.20.1 (avoiding security vulnerability) [#368](https://github.com/1c3t3a/rust-socketio/pull/368) diff --git a/socketio/src/asynchronous/mod.rs b/socketio/src/asynchronous/mod.rs index 2a6cbcb7..fc7ea1a8 100644 --- a/socketio/src/asynchronous/mod.rs +++ b/socketio/src/asynchronous/mod.rs @@ -8,9 +8,9 @@ pub use client::client::{Client, ReconnectSettings}; #[doc = r#" -A macro to wrap a async callback function to be used in the client. +A macro to wrap an async callback function to be used in the client. -This macro is used to wrap a callback function that can handle any event. +This macro is used to wrap a callback function that can handle a specific event. ```rust pub async fn callback(payload: Payload, client: Client) {} @@ -30,7 +30,7 @@ macro_rules! async_callback { } #[doc = r#" -A macro to wrap a async callback function to be used in the client. +A macro to wrap an async callback function to be used in the client. This macro is used to wrap a callback function that can handle any event. From d9ef52c7acae59520282cfa00026c0fad76b5ac5 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sat, 16 Mar 2024 20:33:22 +0800 Subject: [PATCH 3/5] Fix with cargo fmt --all, bump base64 and tokio version, and add multi thread feature to fix test error --- Cargo.lock | 12 +++++++++--- socketio/Cargo.toml | 2 +- socketio/src/asynchronous/mod.rs | 1 - 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 0fd7636f..a08f6390 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,6 +144,12 @@ version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" +[[package]] +name = "base64" +version = "0.22.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" + [[package]] name = "bitflags" version = "1.3.2" @@ -1658,7 +1664,7 @@ version = "0.11.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "046cd98826c46c2ac8ddecae268eb5c2e58628688a5fc7a2643704a73faba95b" dependencies = [ - "base64", + "base64 0.21.5", "bytes", "encoding_rs", "futures-core", @@ -1699,7 +1705,7 @@ dependencies = [ "adler32", "async-stream", "async-trait", - "base64", + "base64 0.21.5", "bytes", "criterion", "futures-util", @@ -1723,7 +1729,7 @@ dependencies = [ "adler32", "async-stream", "backoff", - "base64", + "base64 0.22.0", "bytes", "cargo-tarpaulin", "futures-util", diff --git a/socketio/Cargo.toml b/socketio/Cargo.toml index 3620faf1..a7e5064c 100644 --- a/socketio/Cargo.toml +++ b/socketio/Cargo.toml @@ -12,7 +12,7 @@ license = "MIT" [dependencies] rust_engineio = { version = "0.4.3", path = "../engineio" } -base64 = "0.21.5" +base64 = "0.22.0" bytes = "1" backoff = "0.4" rand = "0.8.5" diff --git a/socketio/src/asynchronous/mod.rs b/socketio/src/asynchronous/mod.rs index fc7ea1a8..18310bb0 100644 --- a/socketio/src/asynchronous/mod.rs +++ b/socketio/src/asynchronous/mod.rs @@ -6,7 +6,6 @@ mod socket; pub use client::builder::ClientBuilder; pub use client::client::{Client, ReconnectSettings}; - #[doc = r#" A macro to wrap an async callback function to be used in the client. From 9f07ae738a2705194a0a3af3c114c99bdf086a07 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 31 Mar 2024 01:22:40 +0800 Subject: [PATCH 4/5] add re-export for the macro, and let the test pass just use it from the root add use futures_util::FutureExt; --- socketio/src/asynchronous/mod.rs | 45 ++++++++++++++++++++++---------- 1 file changed, 31 insertions(+), 14 deletions(-) diff --git a/socketio/src/asynchronous/mod.rs b/socketio/src/asynchronous/mod.rs index 18310bb0..e57cdf6a 100644 --- a/socketio/src/asynchronous/mod.rs +++ b/socketio/src/asynchronous/mod.rs @@ -6,26 +6,36 @@ mod socket; pub use client::builder::ClientBuilder; pub use client::client::{Client, ReconnectSettings}; +// re-export the macro +pub use crate::{async_any_callback, async_callback}; + #[doc = r#" A macro to wrap an async callback function to be used in the client. This macro is used to wrap a callback function that can handle a specific event. ```rust +use rust_socketio::async_callback; +use rust_socketio::asynchronous::{Client, ClientBuilder}; +use rust_socketio::{Event, Payload}; + pub async fn callback(payload: Payload, client: Client) {} -let socket = ClientBuilder::new(host) - .on("message", async_callback!(callback)) - .connect() - .await - .expect("Connection failed"); +#[tokio::main] +async fn main() { + let socket = ClientBuilder::new("http://example.com") + .on("message", async_callback!(callback)) + .connect() + .await; +} ``` "#] #[macro_export] macro_rules! async_callback { - ($f:expr) => { + ($f:expr) => {{ + use futures_util::FutureExt; |payload: Payload, client: Client| $f(payload, client).boxed() - }; + }}; } #[doc = r#" @@ -34,18 +44,25 @@ A macro to wrap an async callback function to be used in the client. This macro is used to wrap a callback function that can handle any event. ```rust +use rust_socketio::async_any_callback; +use rust_socketio::asynchronous::{Client, ClientBuilder}; +use rust_socketio::{Event, Payload}; + pub async fn callback_any(event: Event, payload: Payload, client: Client) {} -let socket = ClientBuilder::new(host) - .on_any(async_any_callback!(callback_any)) - .connect() - .await - .expect("Connection failed"); +#[tokio::main] +async fn main() { + let socket = ClientBuilder::new("http://example.com") + .on_any(async_any_callback!(callback_any)) + .connect() + .await; +} ``` "#] #[macro_export] macro_rules! async_any_callback { - ($f:expr) => { + ($f:expr) => {{ + use futures_util::FutureExt; |event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed() - }; + }}; } From 5a08f982d3fed8ba94072f8ce159906870091229 Mon Sep 17 00:00:00 2001 From: shenjack <3695888@qq.com> Date: Sun, 31 Mar 2024 20:06:47 +0800 Subject: [PATCH 5/5] revert base64 requirements --- Cargo.lock | 12 +++--------- socketio/Cargo.toml | 2 +- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a08f6390..0fd7636f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -144,12 +144,6 @@ version = "0.21.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "35636a1494ede3b646cc98f74f8e62c773a38a659ebc777a2cf26b9b74171df9" -[[package]] -name = "base64" -version = "0.22.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9475866fec1451be56a3c2400fd081ff546538961565ccb5b7142cbd22bc7a51" - [[package]] name = "bitflags" version = "1.3.2" @@ -1664,7 +1658,7 @@ version = "0.11.22" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "046cd98826c46c2ac8ddecae268eb5c2e58628688a5fc7a2643704a73faba95b" dependencies = [ - "base64 0.21.5", + "base64", "bytes", "encoding_rs", "futures-core", @@ -1705,7 +1699,7 @@ dependencies = [ "adler32", "async-stream", "async-trait", - "base64 0.21.5", + "base64", "bytes", "criterion", "futures-util", @@ -1729,7 +1723,7 @@ dependencies = [ "adler32", "async-stream", "backoff", - "base64 0.22.0", + "base64", "bytes", "cargo-tarpaulin", "futures-util", diff --git a/socketio/Cargo.toml b/socketio/Cargo.toml index a7e5064c..3620faf1 100644 --- a/socketio/Cargo.toml +++ b/socketio/Cargo.toml @@ -12,7 +12,7 @@ license = "MIT" [dependencies] rust_engineio = { version = "0.4.3", path = "../engineio" } -base64 = "0.22.0" +base64 = "0.21.5" bytes = "1" backoff = "0.4" rand = "0.8.5"