Skip to content

Commit 8cadfe6

Browse files
committed
feat: oneshot channel
Signed-off-by: tison <[email protected]>
1 parent a012ad9 commit 8cadfe6

File tree

4 files changed

+895
-0
lines changed

4 files changed

+895
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Mea (Make Easy Async) is a runtime-agnostic library providing essential synchron
3030
* [**Semaphore**](https://docs.rs/mea/*/mea/semaphore/struct.Semaphore.html): A synchronization primitive that controls access to a shared resource.
3131
* [**ShutdownSend & ShutdownRecv**](https://docs.rs/mea/*/mea/shutdown/): A composite synchronization primitive for managing shutdown signals.
3232
* [**WaitGroup**](https://docs.rs/mea/*/mea/waitgroup/struct.WaitGroup.html): A synchronization primitive that allows waiting for multiple tasks to complete.
33+
* [**oneshot::channel**](https://docs.rs/mea/*/mea/oneshot/index.html): A one-shot channel for sending a single value between tasks.
3334

3435
## Installation
3536

@@ -68,6 +69,7 @@ This crate collects runtime-agnostic synchronization primitives from spare parts
6869
* **RwLock** is derived from `tokio::sync::RwLock`, but the `max_readers` can be any `usize` instead of `[0, u32::MAX >> 3]`. No blocking method is provided, since it can be easily implemented with block_on of any runtime.
6970
* **Semaphore** is derived from `tokio::sync::Semaphore`, without `close` method since it is quite tricky to use. And thus, this semaphore doesn't have the limitation of max permits. Besides, new methods like `forget_exact` are added to fit the specific use case.
7071
* **WaitGroup** is inspired by [`waitgroup-rs`](https://github.com/laizy/waitgroup-rs), with a different implementation based on the internal `CountdownState` primitive. It fixes the unsound issue as described [here](https://github.com/rust-lang/futures-rs/issues/2880#issuecomment-2333842804).
72+
* **oneshot::channel** is derived from [`oneshot`](https://github.com/faern/oneshot), with significant simplifications since we need not support synchronized receiving functions.
7173

7274
Other parts are written from scratch.
7375

mea/src/lib.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
//! * [`ShutdownSend`] & [`ShutdownRecv`]: A composite synchronization primitive for managing
3434
//! shutdown signals
3535
//! * [`WaitGroup`]: A synchronization primitive that allows waiting for multiple tasks to complete
36+
//! * [`oneshot`]: A one-shot channel for sending a single value between tasks.
3637
//!
3738
//! ## Runtime Agnostic
3839
//!
@@ -62,6 +63,7 @@ pub mod barrier;
6263
pub mod condvar;
6364
pub mod latch;
6465
pub mod mutex;
66+
pub mod oneshot;
6567
pub mod rwlock;
6668
pub mod semaphore;
6769
pub mod shutdown;
@@ -83,12 +85,14 @@ mod tests {
8385
use crate::latch::Latch;
8486
use crate::mutex::Mutex;
8587
use crate::mutex::MutexGuard;
88+
use crate::oneshot;
8689
use crate::rwlock::RwLock;
8790
use crate::rwlock::RwLockReadGuard;
8891
use crate::rwlock::RwLockWriteGuard;
8992
use crate::semaphore::Semaphore;
9093
use crate::shutdown::ShutdownRecv;
9194
use crate::shutdown::ShutdownSend;
95+
use crate::waitgroup::Wait;
9296
use crate::waitgroup::WaitGroup;
9397

9498
#[test]
@@ -106,6 +110,15 @@ mod tests {
106110
do_assert_send_and_sync::<RwLock<i64>>();
107111
do_assert_send_and_sync::<RwLockReadGuard<'_, i64>>();
108112
do_assert_send_and_sync::<RwLockWriteGuard<'_, i64>>();
113+
do_assert_send_and_sync::<oneshot::Sender<i64>>();
114+
do_assert_send_and_sync::<oneshot::SendError<i64>>();
115+
}
116+
117+
#[test]
118+
fn assert_send() {
119+
fn do_assert_send<T: Send>() {}
120+
do_assert_send::<oneshot::Receiver<i64>>();
121+
do_assert_send::<oneshot::Recv<i64>>();
109122
}
110123

111124
#[test]
@@ -118,10 +131,14 @@ mod tests {
118131
do_assert_unpin::<ShutdownSend>();
119132
do_assert_unpin::<ShutdownRecv>();
120133
do_assert_unpin::<WaitGroup>();
134+
do_assert_unpin::<Wait>();
121135
do_assert_unpin::<Mutex<i64>>();
122136
do_assert_unpin::<MutexGuard<'_, i64>>();
123137
do_assert_unpin::<RwLock<i64>>();
124138
do_assert_unpin::<RwLockReadGuard<'_, i64>>();
125139
do_assert_unpin::<RwLockWriteGuard<'_, i64>>();
140+
do_assert_unpin::<oneshot::Sender<i64>>();
141+
do_assert_unpin::<oneshot::Receiver<i64>>();
142+
do_assert_unpin::<oneshot::Recv<i64>>();
126143
}
127144
}

0 commit comments

Comments
 (0)