Skip to main content

net_wire/tcp/
flags.rs

1//! TCP control flags.
2
3use core::ops::{BitOr, BitOrAssign};
4
5/// The eight TCP control flags.
6#[derive(Clone, Copy, Debug, Eq, PartialEq)]
7#[repr(transparent)]
8pub struct TcpFlags(u8);
9impl TcpFlags {
10    /// FIN flag.
11    pub const FIN: Self = Self(1);
12    /// SYN flag.
13    pub const SYN: Self = Self(2);
14    /// RST flag.
15    pub const RST: Self = Self(4);
16    /// PSH flag.
17    pub const PSH: Self = Self(8);
18    /// ACK flag.
19    pub const ACK: Self = Self(16);
20    /// URG flag.
21    pub const URG: Self = Self(32);
22    /// ECE flag.
23    pub const ECE: Self = Self(64);
24    /// CWR flag.
25    pub const CWR: Self = Self(128);
26    /// Constructs flags from their wire value.
27    pub const fn new(raw: u8) -> Self {
28        Self(raw)
29    }
30    /// Returns the wire value.
31    pub const fn raw(self) -> u8 {
32        self.0
33    }
34    /// Returns whether all flags in `other` are set.
35    pub const fn contains(self, other: Self) -> bool {
36        self.0 & other.0 == other.0
37    }
38    /// Returns the union of two flag sets.
39    pub const fn union(self, other: Self) -> Self {
40        Self(self.0 | other.0)
41    }
42}
43impl BitOr for TcpFlags {
44    type Output = Self;
45    fn bitor(self, rhs: Self) -> Self {
46        self.union(rhs)
47    }
48}
49impl BitOrAssign for TcpFlags {
50    fn bitor_assign(&mut self, rhs: Self) {
51        *self = self.union(rhs);
52    }
53}