toe_beans/v4/message/flags.rs
1/// Wraps a [bit field](https://en.wikipedia.org/wiki/Bit_field) used in [Message](crate::v4::Message).
2///
3/// The two bytes that this field occupies were unused in [RFC-951](https://datatracker.ietf.org/doc/html/rfc951#section-3), and later [defined](https://datatracker.ietf.org/doc/html/rfc1542#section-2.2) then [clarified](https://datatracker.ietf.org/doc/html/rfc1542#section-3.1) in RFC-1542.
4///
5/// The leftmost bit is defined as the Broadcast flag. The remaining bits are reserved for future use, and must be set to zero by clients and ignored by servers and relay agents.
6#[derive(Debug, PartialEq)]
7pub struct Flags {
8 /// Used to work around some clients that cannot accept IP unicast datagrams before the TCP/IP software is configured.
9 pub broadcast: bool,
10}
11
12impl Flags {
13 const EMPTY: [u8; 2] = [0, 0];
14 const BROADCAST: [u8; 2] = [128, 0];
15
16 /// As long as the flags field is only used for
17 /// broadcasts then this method can be used as a faster way
18 /// to serialize the broadcast value to bytes.
19 ///
20 /// Otherwise, you'll want to use the existing From impl.
21 ///
22 /// As of 2025 I do not see any RFC that updates RFC-2131 with
23 /// new flags.
24 #[inline]
25 pub const fn temporary_to_bytes(&self) -> [u8; 2] {
26 if self.broadcast {
27 Self::BROADCAST
28 } else {
29 Self::EMPTY
30 }
31 }
32}
33
34impl From<&Flags> for [u8; 2] {
35 fn from(flags: &Flags) -> Self {
36 let mut bytes: u16 = 0b00000000_00000000;
37
38 if flags.broadcast {
39 bytes |= 0b10000000_00000000
40 }
41
42 bytes.to_be_bytes()
43 }
44}
45
46#[cfg(test)]
47mod tests {
48 use super::*;
49
50 #[test]
51 fn broadcast_const_is_correct() {
52 let decimal: u16 = 0b10000000_00000000;
53 assert_eq!(Flags::BROADCAST, decimal.to_be_bytes());
54 }
55}