You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: crates/capsec-tokio/README.md
+39-5Lines changed: 39 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,19 +10,52 @@ You probably want to depend on the `capsec` facade crate with the `tokio` featur
10
10
capsec = { version = "0.1", features = ["tokio"] }
11
11
```
12
12
13
-
## Example
13
+
## Capsec + Tokio Guide
14
+
15
+
There are three common patterns for using capabilities in async code.
16
+
17
+
### Pattern 1: Scoped proof (no spawn)
18
+
19
+
When your async code runs on the current task (no `tokio::spawn`), pass capabilities by reference. The scope-before-await pattern inside each wrapper keeps futures `Send` automatically.
20
+
21
+
```rust,ignore
22
+
async fn handle_request(cap: &impl Has<FsRead>) {
23
+
let data = capsec::tokio::fs::read("/tmp/config.toml", cap).await?;
24
+
// use data...
25
+
}
26
+
```
27
+
28
+
### Pattern 2: `spawn_with` (single capability)
29
+
30
+
When spawning a task that needs one capability, use `capsec_tokio::task::spawn_with`. It converts `Cap<P>` to `SendCap<P>` for you — no need to call `.make_send()` manually.
14
31
15
32
```rust,ignore
16
-
use capsec::prelude::*;
33
+
let cap = root.grant::<FsRead>();
17
34
35
+
let handle = capsec::tokio::task::spawn_with(cap, |cap| async move {
When a spawned task needs multiple capabilities, use `#[capsec::context(send)]` to create a `Send`-safe context struct, wrap it in `Arc`, and clone for each task.
0 commit comments