Skip to main content

run_with_timeout

Function run_with_timeout 

Source
pub fn run_with_timeout<F, T>(
    dur: Duration,
    f: F,
) -> Result<Option<T>, CoreError>
where F: Send + 'static + FnOnce() -> Result<T, CoreError>, T: Send + 'static,
Expand description

Executes a function with a timeout constraint

Spawns the function in a separate thread and waits for completion up to the specified duration. This prevents slow operations from blocking the main thread indefinitely.

§Arguments

  • dur - Maximum duration to wait for the function to complete
  • f - The function to execute with timeout protection

§Returns

  • Ok(Some(T)) - Function completed successfully within timeout
  • Ok(None) - Function timed out
  • Err - Function panicked or returned an error

§Examples

use claude_code_statusline_core::timeout::run_with_timeout;
use std::time::Duration;

let result = run_with_timeout(Duration::from_millis(100), || {
    Ok("Success".to_string())
});
assert!(result.unwrap().is_some());

let timeout = run_with_timeout(Duration::from_millis(10), || {
    std::thread::sleep(Duration::from_millis(100));
    Ok("Too slow")
});
assert!(timeout.unwrap().is_none());

§Implementation Notes

  • Uses channels for thread communication
  • Catches panics and converts them to errors
  • Thread is detached after timeout (may continue running)