-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
93 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "flowync" | ||
version = "1.8.0" | ||
version = "1.8.1" | ||
authors = ["Ar37-rs <[email protected]>"] | ||
edition = "2018" | ||
description = "A simple utility for multithreading a/synchronization" | ||
|
@@ -10,3 +10,6 @@ keywords = ["async", "sync", "std", "multi-thread", "non-blocking"] | |
license = "MIT OR Apache-2.0" | ||
repository = "https://github.com/Ar37-rs/flowync" | ||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dev-dependencies] | ||
tokio = { version = "1", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// [dependencies] | ||
// # Make sure to enable tokio "full" features (multithreaded support) like so: | ||
// tokio = { version = "1", features = ["full"] } | ||
|
||
use flowync::Flower; | ||
use std::{io::Error, time::Instant}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let instant: Instant = Instant::now(); | ||
let flower: Flower<String, u32> = Flower::new(1); | ||
tokio::spawn({ | ||
let handle = flower.handle(); | ||
async move { | ||
let id = handle.id(); | ||
let result = | ||
Ok::<String, Error>(format!("the flower with id: {} is flowing", id).into()); | ||
|
||
match result { | ||
Ok(value) => { | ||
// Send current flower progress. | ||
handle.send_async(value).await; | ||
} | ||
Err(e) => { | ||
// Return error immediately if something not right, for example: | ||
return handle.err(e.to_string()); | ||
} | ||
} | ||
|
||
// Explicit Cancelation example: | ||
// Check if the current flower should be canceled | ||
if handle.should_cancel() { | ||
let value = format!("canceling the flower with id: {}", id); | ||
handle.send_async(value).await; | ||
return handle.err(format!("the flower with id: {} canceled", id)); | ||
} | ||
|
||
return handle.ok(instant.elapsed().subsec_micros()); | ||
} | ||
}); | ||
|
||
let mut done = false; | ||
|
||
loop { | ||
flower.try_recv( | ||
|value| { | ||
println!("{}\n", value); | ||
}, | ||
|result| { | ||
match result { | ||
Ok(elapsed) => println!( | ||
"the flower with id: {} finished in: {:?} microseconds \n", | ||
flower.id(), | ||
elapsed | ||
), | ||
Err(e) => println!("{}", e), | ||
} | ||
done = true; | ||
}, | ||
); | ||
|
||
// Cancel if need to | ||
// flower.cancel(); | ||
|
||
if done { | ||
break; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters