use core::time::Duration;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub struct Timeouts {
pub connect: Duration,
pub handshake: Duration,
pub read_idle: Duration,
pub write: Duration,
}
impl Timeouts {
#[must_use]
pub const fn new(
connect: Duration,
handshake: Duration,
read_idle: Duration,
write: Duration,
) -> Self {
Timeouts {
connect,
handshake,
read_idle,
write,
}
}
#[must_use]
pub const fn with_connect(mut self, connect: Duration) -> Self {
self.connect = connect;
self
}
#[must_use]
pub const fn with_handshake(mut self, handshake: Duration) -> Self {
self.handshake = handshake;
self
}
#[must_use]
pub const fn with_read_idle(mut self, read_idle: Duration) -> Self {
self.read_idle = read_idle;
self
}
#[must_use]
pub const fn with_write(mut self, write: Duration) -> Self {
self.write = write;
self
}
}
impl Default for Timeouts {
fn default() -> Self {
Timeouts::new(
Duration::from_secs(10),
Duration::from_secs(10),
Duration::from_secs(30),
Duration::from_secs(10),
)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn each_with_sets_only_its_own_field() {
let base = Timeouts::default();
let one = Duration::from_secs(1);
assert_eq!(
base.with_connect(one),
Timeouts {
connect: one,
..base
}
);
assert_eq!(
base.with_handshake(one),
Timeouts {
handshake: one,
..base
}
);
assert_eq!(
base.with_read_idle(one),
Timeouts {
read_idle: one,
..base
}
);
assert_eq!(base.with_write(one), Timeouts { write: one, ..base });
}
}