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