Skip to content

Commit

Permalink
Bump version.
Browse files Browse the repository at this point in the history
  • Loading branch information
ar37-rs committed Apr 6, 2022
1 parent 01369d7 commit a144f8b
Show file tree
Hide file tree
Showing 10 changed files with 258 additions and 144 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [3.0.0] - 2022-4-6
- Remove needless traits: Sync + 'static
- Breaking changes:
* remove fn try_recv and fn on_complete to avoid potential UB in a particular case.

## [2.0.x] - 2022-4-1
- Breaking change:
* remove Leaper
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "flowync"
version = "2.0.7"
version = "3.0.0"
authors = ["Ar37-rs <[email protected]>"]
edition = "2018"
description = "A simple utility for multithreading a/synchronization"
Expand Down
21 changes: 16 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
```rust
use flowync::Flower;

type TestFlower = Flower<u32, String>;

fn main() {
let flower = Flower::<i32, String>::new(1);
let flower: TestFlower = Flower::new(1);
std::thread::spawn({
let handle = flower.handle();
// Activate
Expand All @@ -20,8 +22,8 @@ fn main() {
// // Send current value through channel, will block the spawned thread
// until the option value successfully being polled in the main thread.
handle.send(i);
// or handle.send_async(i).await; can be used from any multithreaded async runtime,
// or handle.send_async(i).await; can be used from any multithreaded async runtime,

// // Return error if the job is failure, for example:
// if i >= 3 {
// return handle.err("Err");
Expand All @@ -39,15 +41,24 @@ fn main() {
// and will deactivate itself if the result value successfully received.
// Note: this fn is non-blocking (won't block the current thread).
if flower.is_active() {
flower.try_recv(|channel| {
// another logic goes here...
// e.g:
// notify_loading_fn();

flower.then(|channel| {
// poll channel
if let Some(value) = channel {
println!("{}", value);
}
}).on_complete(|result| {
},
|result| {
// match result
match result {
Ok(value) => println!("{}", value),
Err(err_msg) => println!("{}", err_msg),
}

// exit if completed
exit = true;
});
}
Expand Down
39 changes: 24 additions & 15 deletions examples/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

use flowync::Flower;

type TestFlower = Flower<u32, String>;

fn main() {
let flower = Flower::<i32, String>::new(1);
let flower: TestFlower = Flower::new(1);
std::thread::spawn({
let handle = flower.handle();
// Activate
Expand Down Expand Up @@ -32,23 +34,30 @@ fn main() {
// and will deactivate itself if the result value successfully received.
// Note: this fn is non-blocking (won't block the current thread).
if flower.is_active() {
flower
.try_recv(|channel| {
if let Some(value) = channel {
println!("{}", value);
}
})
.on_complete(|result| {
match result {
Ok(value) => println!("{}", value),
Err(err_msg) => println!("{}", err_msg),
}
exit = true;
});
// another logic goes here...
// e.g:
// notify_loading_fn();

flower.then(|channel| {
// poll channel
if let Some(value) = channel {
println!("{}", value);
}
},
|result| {
// match result
match result {
Ok(value) => println!("{}", value),
Err(err_msg) => println!("{}", err_msg),
}

// exit if completed
exit = true;
});
}

if exit {
break;
}
}
}
}
56 changes: 56 additions & 0 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#![allow(clippy::needless_return)]

use flowync::Flower;
use std::io::Error;

type TestSimpleFlower = Flower<(), String>;

fn main() {
let flower: TestSimpleFlower = Flower::new(1);
std::thread::spawn({
let handle = flower.handle();
// Activate
handle.activate();
move || {
let id = handle.id();
let result = Ok::<String, Error>(format!(
"thse flower with id: {} successfully completed",
id
));
match result {
Ok(value) => {
// And return ok if the job successfully completed.
return handle.ok(value);
}
Err(e) => {
// Return error immediately if something not right, for example:
return handle.err(e.to_string());
}
}
}
});

let mut exit = false;

loop {
// Instead of polling the mutex over and over, check if the flower is_active()
// and will deactivate itself if the result value successfully received.
// Note: this fn is non-blocking (won't block the current thread).
if flower.is_active() {
flower.then(
|_channel| (),
|result| {
match result {
Ok(value) => println!("{}", value),
Err(err_msg) => println!("{}", err_msg),
}
exit = true;
},
);
}

if exit {
break;
}
}
}
15 changes: 9 additions & 6 deletions examples/tokio_full.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
use flowync::Flower;
use std::{io::Error, time::Instant};

type TestTokioFlower = Flower<String, u32>;

#[tokio::main]
async fn main() {
let instant: Instant = Instant::now();
let flower: Flower<String, u32> = Flower::new(1);
let flower: TestTokioFlower = Flower::new(1);
tokio::spawn({
let this = flower.handle();
// Activate
Expand Down Expand Up @@ -50,13 +52,13 @@ async fn main() {

loop {
if flower.is_active() {
flower
.try_recv(|channel| {
flower.then(
|channel| {
if let Some(value) = channel {
println!("{}\n", value);
}
})
.on_complete(|result| {
},
|result| {
match result {
Ok(elapsed) => println!(
"the flower with id: {} finished in: {:?} microseconds \n",
Expand All @@ -66,7 +68,8 @@ async fn main() {
Err(err_msg) => println!("{}", err_msg),
}
done = true;
});
},
);
}

// // Cancel if need to
Expand Down
29 changes: 15 additions & 14 deletions examples/vectored_flowers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::{
time::{Duration, Instant},
};

type TestFlower = Flower<String, u32>;
type TestVectoredFlower = Flower<String, u32>;

fn main() {
let instant: Instant = Instant::now();
let mut vec_opt_flowers = Vec::new();
for i in 0..5 {
let flower: TestFlower = Flower::new(i);
let flower: TestVectoredFlower = Flower::new(i);
std::thread::spawn({
let this = flower.handle();
// Activate if need too, actually needless on this context because we use Option
Expand Down Expand Up @@ -66,19 +66,19 @@ fn main() {
if let Some(flower) = opt_flower {
let id = flower.id();

// // Cancel if need to.
// Cancel if need to.
// if (id % 2 != 0) || (id == 0) {
// flower.cancel();
// }

let mut done = false;
flower
.try_recv(|channel| {
flower.then(
|channel| {
if let Some(value) = channel {
println!("{}\n", value);
}
})
.on_complete(|result| {
},
|result| {
match result {
Ok(elapsed) => println!(
"the flower with id: {} finished in: {:?} milliseconds\n",
Expand All @@ -87,7 +87,8 @@ fn main() {
Err(err_msg) => println!("{}", err_msg),
}
done = true;
});
},
);

if done {
// Set to None to free later
Expand All @@ -97,13 +98,13 @@ fn main() {
}
});

// Free completed flower
vec_opt_flowers = vec_opt_flowers
.into_iter()
.filter(|opt_flower| opt_flower.is_some())
.collect::<Vec<_>>();

if count_down == 0 {
// Free completed flower
vec_opt_flowers = vec_opt_flowers
.into_iter()
.filter(|opt_flower| opt_flower.is_some())
.collect::<Vec<_>>();

println!(
"finished with vec_opt_flowers remains: {}",
vec_opt_flowers.len()
Expand Down
Loading

0 comments on commit a144f8b

Please sign in to comment.