Skip to main content

monocoque_core/
timeout.rs

1//! Timeout utilities for I/O operations
2//!
3//! Provides timeout wrappers for async read/write operations using compio's timeout support.
4
5use compio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
6use compio::time::timeout;
7use std::io;
8use std::time::Duration;
9
10/// Execute an async `read_exact` operation with a timeout.
11///
12/// Reads exactly the full buffer or returns an error.
13pub async fn read_exact_with_timeout<S, B>(
14    stream: &mut S,
15    buf: B,
16    duration: Option<Duration>,
17) -> io::Result<compio::buf::BufResult<(), B>>
18where
19    S: AsyncRead + Unpin,
20    B: compio::buf::IoBufMut,
21{
22    match duration {
23        None => {
24            // No timeout, block indefinitely
25            Ok(stream.read_exact(buf).await)
26        }
27        Some(d) if d.is_zero() => {
28            // Non-blocking mode
29            Err(io::Error::new(
30                io::ErrorKind::WouldBlock,
31                "Non-blocking mode not yet implemented",
32            ))
33        }
34        Some(d) => {
35            // Timeout mode
36            match timeout(d, stream.read_exact(buf)).await {
37                Ok(result) => Ok(result),
38                Err(_elapsed) => Err(io::Error::new(
39                    io::ErrorKind::TimedOut,
40                    "Read operation timed out",
41                )),
42            }
43        }
44    }
45}
46
47/// Execute an async `write_all` operation with a timeout.
48///
49/// Writes the entire buffer or returns an error.
50pub async fn write_all_with_timeout<S, B>(
51    stream: &mut S,
52    buf: B,
53    duration: Option<Duration>,
54) -> io::Result<compio::buf::BufResult<(), B>>
55where
56    S: AsyncWrite + Unpin,
57    B: compio::buf::IoBuf,
58{
59    match duration {
60        None => {
61            // No timeout, block indefinitely
62            Ok(stream.write_all(buf).await)
63        }
64        Some(d) if d.is_zero() => {
65            // Non-blocking mode
66            Err(io::Error::new(
67                io::ErrorKind::WouldBlock,
68                "Non-blocking mode not yet implemented",
69            ))
70        }
71        Some(d) => {
72            // Timeout mode
73            match timeout(d, stream.write_all(buf)).await {
74                Ok(result) => Ok(result),
75                Err(_elapsed) => Err(io::Error::new(
76                    io::ErrorKind::TimedOut,
77                    "Write operation timed out",
78                )),
79            }
80        }
81    }
82}
83
84#[cfg(test)]
85mod tests {
86    use super::*;
87
88    // Note: These are compile-time tests to ensure the API is sound
89    // Full integration tests would require actual I/O operations
90
91    #[test]
92    fn test_timeout_types() {
93        // Verify Duration handling
94        let _infinite: Option<Duration> = None;
95        let _nonblocking = Some(Duration::ZERO);
96        let _timed = Some(Duration::from_secs(5));
97    }
98}