use bytes::BufMut;
use ::bits::compose::Compose;
use ::bits::message_builder::OptBuilder;
use ::bits::parse::{ParseAll, Parser, ParseAllError, ShortBuf};
use ::iana::OptionCode;
use super::CodeOptData;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct TcpKeepalive(u16);
impl TcpKeepalive {
pub fn new(timeout: u16) -> Self {
TcpKeepalive(timeout)
}
pub fn push(builder: &mut OptBuilder, timeout: u16)
-> Result<(), ShortBuf> {
builder.push(&Self::new(timeout))
}
pub fn timeout(self) -> u16 {
self.0
}
}
impl ParseAll for TcpKeepalive {
type Err = ParseAllError;
fn parse_all(parser: &mut Parser, len: usize) -> Result<Self, Self::Err> {
u16::parse_all(parser, len).map(Self::new)
}
}
impl Compose for TcpKeepalive {
fn compose_len(&self) -> usize {
2
}
fn compose<B: BufMut>(&self, buf: &mut B) {
self.0.compose(buf)
}
}
impl CodeOptData for TcpKeepalive {
const CODE: OptionCode = OptionCode::TcpKeepalive;
}