Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
XdoctorwhoZ committed Aug 2, 2024
1 parent ab95b61 commit 83d5701
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/examples/test_cb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ use tokio::time::sleep;
use tokio::time::Duration;

use futures::future::BoxFuture;
use futures::Future;

type POK = fn(u8) -> BoxFuture<'static, Result<bool, ()>>;

// fn wrap<F>(function: F) -> POK
// where
// F: Future<Output = Result<bool, ()>> + Send + 'static,
// {
// return |val: u8| -> BoxFuture<'static, Result<bool, ()>> { Box::pin(function) };
// }

fn test(val: u8) -> BoxFuture<'static, Result<bool, ()>> {
Box::pin(async move {
Expand All @@ -10,14 +20,39 @@ fn test(val: u8) -> BoxFuture<'static, Result<bool, ()>> {
})
}

struct Test {
b: Option<BoxFuture<'static, Result<bool, ()>>>,
}

impl Test {
fn set_b<F>(&mut self, function: F)
where
F: Future<Output = Result<bool, ()>> + Send + 'static,
{
self.b = Some(Box::pin(function));
}
}

#[tokio::main]
async fn main() {
println!("test cb");

let mut cbb: Option<fn(u8) -> BoxFuture<'static, Result<bool, ()>>> = None;
let mut cbb: Option<POK> = None;

cbb = Some(test);

// let a = wrap(async move {
// println!("neww!! !!!!");
// Ok(true)
// });
// cbb = Some(a);

let mut abc = Test { b: None };
abc.set_b(async move {
println!("neww!! !!!!");
Ok(true)
});

tokio::spawn(cbb.unwrap()(1));

tokio::spawn(test(1));
Expand Down

0 comments on commit 83d5701

Please sign in to comment.