Skip to content

Commit

Permalink
Provide synchronous and asynchronous side by side
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanUkhov committed Dec 27, 2024
1 parent c51b107 commit 2e2b176
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "loop"
version = "0.3.2"
version = "0.4.0"
edition = "2021"
license = "Apache-2.0/MIT"
authors = ["Ivan Ukhov <ivan.ukhov@gmail.com>"]
Expand Down
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,20 @@ The package allows for processing iterators in parallel.
Synchronously:

```rust
use r#loop::parallelize;

let double = |value| 2 * value;
let _ = r#loop::parallelize(0..10, double, None).collect::<Vec<_>>();
let _ = parallelize(0..10, double, None).collect::<Vec<_>>();
```

Asynchronously:

```rust
use futures::stream::StreamExt;
use r#loop::asynchronous::parallelize;

let double = |value| async move { 2 * value };
let _ = r#loop::parallelize(0..10, double).collect::<Vec<_>>().await;
let _ = parallelize(0..10, double).collect::<Vec<_>>().await;
```

## Contribution
Expand Down
3 changes: 3 additions & 0 deletions src/asynchronous.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! Asynchronous implementation.
use std::sync::Arc;

use tokio::sync::{mpsc, Mutex};
use tokio_stream::wrappers::ReceiverStream;

/// Process an iterator in parallel.
pub fn parallelize<Items, Item, Map, Future, Output>(
items: Items,
mut map: Map,
Expand Down
21 changes: 8 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@
//! Synchronously:
//!
//! ```
//! # #[cfg(not(feature = "asynchronous"))]
//! fn main() {
//! use r#loop::parallelize;
//!
//! let double = |value| 2 * value;
//! let _ = r#loop::parallelize(0..10, double).collect::<Vec<_>>();
//! let _ = parallelize(0..10, double).collect::<Vec<_>>();
//! }
//! # #[cfg(feature = "asynchronous")]
//! # fn main() {}
//!```
//!
//! Asynchronously:
Expand All @@ -21,23 +20,19 @@
//! #[tokio::main]
//! async fn main() {
//! use futures::stream::StreamExt;
//! use r#loop::asynchronous::parallelize;
//!
//! let double = |value| async move { 2 * value };
//! let _ = r#loop::parallelize(0..10, double).collect::<Vec<_>>().await;
//! let _ = parallelize(0..10, double).collect::<Vec<_>>().await;
//! }
//! # #[cfg(not(feature = "asynchronous"))]
//! # fn main() {}
//! ```
#[cfg(feature = "asynchronous")]
#[path = "asynchronous.rs"]
mod implementation;

#[cfg(not(feature = "asynchronous"))]
#[path = "synchronous.rs"]
mod implementation;
pub mod asynchronous;
pub mod synchronous;

mod support;

/// Process an iterator in parallel.
pub use implementation::parallelize;
pub use synchronous::parallelize;
3 changes: 3 additions & 0 deletions src/synchronous.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
//! Synchronous implementation.
use std::sync::{mpsc, Arc, Mutex};

/// Process an iterator in parallel.
pub fn parallelize<Items, Item, Map, Output>(
items: Items,
mut map: Map,
Expand Down

0 comments on commit 2e2b176

Please sign in to comment.