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

Fix docs test

add use futures_util::FutureExt;
  • Loading branch information
shenjackyuanjie committed Mar 30, 2024
1 parent d9ef52c commit 7e95f2e
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions socketio/src/asynchronous/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,34 @@ 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::{Error, 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) => {
use futures_util::FutureExt;
|payload: Payload, client: Client| $f(payload, client).boxed()
};
}
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::{Error, 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) => {
use futures_util::FutureExt;
|event: Event, payload: Payload, client: Client| $f(event, payload, client).boxed()
};
}

0 comments on commit 7e95f2e

Please sign in to comment.