Runtime.try_run
Run one future to completion, or say why it cannot be run here.
Source and package
- Qualified name
-
libtmux::blocking:: Runtime:: try _run - Module
- blocking
- Declared in
Runtime- Package
- libtmux
- Source
- crates/libtmux/src/blocking.rs
-
Description#
Same as
run(), except that being inside an async context is returned rather than raised. This exists because the callers this module is for are the ones most likely to hit it: a script that grows a#[tokio::main], or a helper written for a script and later called from an async test.Errors
Returns
Error::RuntimeNestedwhen called from inside an async context. Await the future directly there.Examples#
Outside an async context it runs the future and gives back its value:
use libtmux::blocking::Runtime;let runtime = Runtime::new()?;let answer = runtime.try_run(async { 2 + 2 })?;assert_eq!(answer, 4);Inside one it says so, where
run()would panic:use libtmux::{Error, ErrorKind};use libtmux::blocking::Runtime;let runtime = Runtime::new()?;let nested = runtime.run(async { runtime.try_run(async { 2 + 2 }) });assert!(matches!(nested, Err(Error::RuntimeNested)));assert_eq!(nested.unwrap_err().kind(), ErrorKind::InvalidInput);