5 个版本
| 0.2.2 | 2024 年 1 月 14 日 |
|---|---|
| 0.2.1 | 2023 年 8 月 14 日 |
| 0.2.0 | 2023 年 8 月 10 日 |
| 0.1.1 | 2023 年 8 月 8 日 |
| 0.1.0 | 2023 年 7 月 30 日 |
#633 in 并发
每月 34 次下载
用于 6 个 Crates (4 直接)
13KB
222 行
swctx
Set/Wait Context (swctx) 是一个具有一些特殊语义的一次性通道-like 构造。
lib.rs:
swctx 非常类似于一次性通道,但添加了一些语义。
use std::thread;
use swctx::mkpair;
let (sctx, wctx) = mkpair::<&str, &str, &str>();
let jh = thread::spawn(move || {
sctx.set_state("in thread");
sctx.set("hello");
});
jh.join().unwrap();
assert_eq!(wctx.wait().unwrap(), "hello");
在典型用例中,应用程序或库调用 [mkpair()] 来创建一对链接的 SetCtx 和 WaitCtx 对象。将 SetCtx 对象传递给远程线程/任务,并使用 WaitCtx 等待对象到达 [从发送 SetCtx 的线程/任务]。
一旦线程/任务有数据要发送回 WaitCtx,它就调用 [SetCtx::set()] 来发送数据。
SetCtx 有一个内部状态,可以使用 [SetCtx::set_state()] 来设置,该状态将被报告回 WaitCtx,如果 SetCtx 提前被丢弃,则 WaitCtx 将返回 Error::Aborted。
SetCtx 还可以通过调用 [SetCtx::fail()] 来指示失败,并传递一个应用程序特定的错误代码。这将导致 WaitCtx 解锁并返回 Error::App。
依赖项
~0.4–5.5MB
~11K SLoC