https://github.com/defelo/tokio-shield
Prevent futures in Rust from being aborted by wrapping them in tasks
https://github.com/defelo/tokio-shield
Last synced: 5 months ago
JSON representation
Prevent futures in Rust from being aborted by wrapping them in tasks
- Host: GitHub
- URL: https://github.com/defelo/tokio-shield
- Owner: Defelo
- Created: 2023-05-25T14:37:37.000Z (almost 2 years ago)
- Default Branch: develop
- Last Pushed: 2024-11-14T17:39:14.000Z (6 months ago)
- Last Synced: 2024-12-08T00:31:08.787Z (5 months ago)
- Language: Rust
- Homepage: https://docs.rs/tokio-shield
- Size: 30.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
[](https://github.com/Defelo/tokio-shield/actions/workflows/check.yml)
[](https://github.com/Defelo/tokio-shield/actions/workflows/test.yml)
[](https://codecov.io/gh/Defelo/tokio-shield)

[](https://deps.rs/repo/github/Defelo/tokio-shield)# tokio-shield
Prevent futures in Rust from being aborted by wrapping them in tasks.## Example
```rust
use std::time::Duration;
use tokio::{sync::oneshot, time::sleep};
use tokio_shield::Shield;#[tokio::main]
async fn main() {
let (tx, mut rx) = oneshot::channel();// Create and shield a future that waits for 10ms and then returns a value
// via a oneshot channel.
let future = async {
sleep(Duration::from_millis(10)).await;
tx.send(42).unwrap();
}.shield();// Spawn a task to run this future, but cancel it after 5ms.
let task = tokio::spawn(future);
sleep(Duration::from_millis(5)).await;
task.abort();
sleep(Duration::from_millis(5)).await;// After 10ms the value can successfully be read from the oneshot channel,
// because `shield` prevented our future from being canceled.
assert_eq!(rx.try_recv().unwrap(), 42);
}
```