Skip to content

Commit

Permalink
add re-export for the macro, and let the test pass
Browse files Browse the repository at this point in the history
just use it from the root
add use futures_util::FutureExt;
  • Loading branch information
shenjackyuanjie authored and 1c3t3a committed Mar 31, 2024
1 parent 384fac9 commit d291abd
Showing 1 changed file with 31 additions and 14 deletions.
45 changes: 31 additions & 14 deletions socketio/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
Expand All @@ -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()
};
}};
}

0 comments on commit d291abd

Please sign in to comment.