Skip to content

Commit 185a008

Browse files
author
Nicholas Cyprus
committed
First pass constification of the wire module
1 parent 5b3d6fc commit 185a008

22 files changed

+103
-103
lines changed

src/wire/arp.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,33 +39,33 @@ mod field {
3939
pub const OPER: Field = 6..8;
4040

4141
#[inline]
42-
pub fn SHA(hardware_len: u8, _protocol_len: u8) -> Field {
42+
pub const fn SHA(hardware_len: u8, _protocol_len: u8) -> Field {
4343
let start = OPER.end;
4444
start..(start + hardware_len as usize)
4545
}
4646

4747
#[inline]
48-
pub fn SPA(hardware_len: u8, protocol_len: u8) -> Field {
48+
pub const fn SPA(hardware_len: u8, protocol_len: u8) -> Field {
4949
let start = SHA(hardware_len, protocol_len).end;
5050
start..(start + protocol_len as usize)
5151
}
5252

5353
#[inline]
54-
pub fn THA(hardware_len: u8, protocol_len: u8) -> Field {
54+
pub const fn THA(hardware_len: u8, protocol_len: u8) -> Field {
5555
let start = SPA(hardware_len, protocol_len).end;
5656
start..(start + hardware_len as usize)
5757
}
5858

5959
#[inline]
60-
pub fn TPA(hardware_len: u8, protocol_len: u8) -> Field {
60+
pub const fn TPA(hardware_len: u8, protocol_len: u8) -> Field {
6161
let start = THA(hardware_len, protocol_len).end;
6262
start..(start + protocol_len as usize)
6363
}
6464
}
6565

6666
impl<T: AsRef<[u8]>> Packet<T> {
6767
/// Imbue a raw octet buffer with ARP packet structure.
68-
pub fn new_unchecked(buffer: T) -> Packet<T> {
68+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
6969
Packet { buffer }
7070
}
7171

@@ -289,7 +289,7 @@ impl Repr {
289289
}
290290

291291
/// Return the length of a packet that will be emitted from this high-level representation.
292-
pub fn buffer_len(&self) -> usize {
292+
pub const fn buffer_len(&self) -> usize {
293293
match *self {
294294
Repr::EthernetIpv4 { .. } => field::TPA(6, 4).end,
295295
}

src/wire/dhcpv4.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ bitflags! {
4444
}
4545

4646
impl MessageType {
47-
fn opcode(&self) -> OpCode {
47+
const fn opcode(&self) -> OpCode {
4848
match *self {
4949
MessageType::Discover
5050
| MessageType::Inform
@@ -233,7 +233,7 @@ pub(crate) mod field {
233233

234234
impl<T: AsRef<[u8]>> Packet<T> {
235235
/// Imbue a raw octet buffer with DHCP packet structure.
236-
pub fn new_unchecked(buffer: T) -> Packet<T> {
236+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
237237
Packet { buffer }
238238
}
239239

@@ -1143,7 +1143,7 @@ mod test {
11431143
assert_eq!(packet, DISCOVER_BYTES);
11441144
}
11451145

1146-
fn offer_repr() -> Repr<'static> {
1146+
const fn offer_repr() -> Repr<'static> {
11471147
Repr {
11481148
message_type: MessageType::Offer,
11491149
transaction_id: 0x3d1d,
@@ -1169,7 +1169,7 @@ mod test {
11691169
}
11701170
}
11711171

1172-
fn discover_repr() -> Repr<'static> {
1172+
const fn discover_repr() -> Repr<'static> {
11731173
Repr {
11741174
message_type: MessageType::Discover,
11751175
transaction_id: 0x3d1d,

src/wire/dns.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub struct Packet<T: AsRef<[u8]>> {
8383

8484
impl<T: AsRef<[u8]>> Packet<T> {
8585
/// Imbue a raw octet buffer with DNS packet structure.
86-
pub fn new_unchecked(buffer: T) -> Packet<T> {
86+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
8787
Packet { buffer }
8888
}
8989

@@ -311,7 +311,7 @@ impl<'a> Question<'a> {
311311
}
312312

313313
/// Return the length of a packet that will be emitted from this high-level representation.
314-
pub fn buffer_len(&self) -> usize {
314+
pub const fn buffer_len(&self) -> usize {
315315
self.name.len() + 4
316316
}
317317

@@ -412,7 +412,7 @@ pub struct Repr<'a> {
412412

413413
impl<'a> Repr<'a> {
414414
/// Return the length of a packet that will be emitted from this high-level representation.
415-
pub fn buffer_len(&self) -> usize {
415+
pub const fn buffer_len(&self) -> usize {
416416
field::HEADER_END + self.question.buffer_len()
417417
}
418418

src/wire/ethernet.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Address {
4343
}
4444

4545
/// Return an Ethernet address as a sequence of octets, in big-endian.
46-
pub fn as_bytes(&self) -> &[u8] {
46+
pub const fn as_bytes(&self) -> &[u8] {
4747
&self.0
4848
}
4949

@@ -58,12 +58,12 @@ impl Address {
5858
}
5959

6060
/// Query whether the "multicast" bit in the OUI is set.
61-
pub fn is_multicast(&self) -> bool {
61+
pub const fn is_multicast(&self) -> bool {
6262
self.0[0] & 0x01 != 0
6363
}
6464

6565
/// Query whether the "locally administered" bit in the OUI is set.
66-
pub fn is_local(&self) -> bool {
66+
pub const fn is_local(&self) -> bool {
6767
self.0[0] & 0x02 != 0
6868
}
6969
}
@@ -100,7 +100,7 @@ pub const HEADER_LEN: usize = field::PAYLOAD.start;
100100

101101
impl<T: AsRef<[u8]>> Frame<T> {
102102
/// Imbue a raw octet buffer with Ethernet frame structure.
103-
pub fn new_unchecked(buffer: T) -> Frame<T> {
103+
pub const fn new_unchecked(buffer: T) -> Frame<T> {
104104
Frame { buffer }
105105
}
106106

@@ -131,13 +131,13 @@ impl<T: AsRef<[u8]>> Frame<T> {
131131
}
132132

133133
/// Return the length of a frame header.
134-
pub fn header_len() -> usize {
134+
pub const fn header_len() -> usize {
135135
HEADER_LEN
136136
}
137137

138138
/// Return the length of a buffer required to hold a packet with the payload
139139
/// of a given length.
140-
pub fn buffer_len(payload_len: usize) -> usize {
140+
pub const fn buffer_len(payload_len: usize) -> usize {
141141
HEADER_LEN + payload_len
142142
}
143143

@@ -277,7 +277,7 @@ impl Repr {
277277
}
278278

279279
/// Return the length of a header that will be emitted from this high-level representation.
280-
pub fn buffer_len(&self) -> usize {
280+
pub const fn buffer_len(&self) -> usize {
281281
HEADER_LEN
282282
}
283283

src/wire/icmpv4.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ mod field {
184184

185185
impl<T: AsRef<[u8]>> Packet<T> {
186186
/// Imbue a raw octet buffer with ICMPv4 packet structure.
187-
pub fn new_unchecked(buffer: T) -> Packet<T> {
187+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
188188
Packet { buffer }
189189
}
190190

@@ -468,7 +468,7 @@ impl<'a> Repr<'a> {
468468
}
469469

470470
/// Return the length of a packet that will be emitted from this high-level representation.
471-
pub fn buffer_len(&self) -> usize {
471+
pub const fn buffer_len(&self) -> usize {
472472
match self {
473473
&Repr::EchoRequest { data, .. } | &Repr::EchoReply { data, .. } => {
474474
field::ECHO_SEQNO.end + data.len()

src/wire/icmpv6.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl Message {
5555
/// is an [NDISC] message type.
5656
///
5757
/// [NDISC]: https://tools.ietf.org/html/rfc4861
58-
pub fn is_ndisc(&self) -> bool {
58+
pub const fn is_ndisc(&self) -> bool {
5959
match *self {
6060
Message::RouterSolicit
6161
| Message::RouterAdvert
@@ -70,7 +70,7 @@ impl Message {
7070
/// is an [MLD] message type.
7171
///
7272
/// [MLD]: https://tools.ietf.org/html/rfc3810
73-
pub fn is_mld(&self) -> bool {
73+
pub const fn is_mld(&self) -> bool {
7474
match *self {
7575
Message::MldQuery | Message::MldReport => true,
7676
_ => false,
@@ -247,7 +247,7 @@ pub(super) mod field {
247247

248248
impl<T: AsRef<[u8]>> Packet<T> {
249249
/// Imbue a raw octet buffer with ICMPv6 packet structure.
250-
pub fn new_unchecked(buffer: T) -> Packet<T> {
250+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
251251
Packet { buffer }
252252
}
253253

@@ -625,7 +625,7 @@ impl<'a> Repr<'a> {
625625
}
626626

627627
/// Return the length of a packet that will be emitted from this high-level representation.
628-
pub fn buffer_len(&self) -> usize {
628+
pub const fn buffer_len(&self) -> usize {
629629
match self {
630630
&Repr::DstUnreachable { header, data, .. }
631631
| &Repr::PktTooBig { header, data, .. }

src/wire/ieee802154.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum_with_unknown! {
4343

4444
impl AddressingMode {
4545
/// Return the size in octets of the address.
46-
fn size(&self) -> usize {
46+
const fn size(&self) -> usize {
4747
match self {
4848
AddressingMode::Absent => 0,
4949
AddressingMode::Short => 2,
@@ -103,11 +103,11 @@ impl Address {
103103
*self == Self::BROADCAST
104104
}
105105

106-
fn short_from_bytes(a: [u8; 2]) -> Self {
106+
const fn short_from_bytes(a: [u8; 2]) -> Self {
107107
Self::Short(a)
108108
}
109109

110-
fn extended_from_bytes(a: [u8; 8]) -> Self {
110+
const fn extended_from_bytes(a: [u8; 8]) -> Self {
111111
Self::Extended(a)
112112
}
113113

@@ -125,7 +125,7 @@ impl Address {
125125
}
126126
}
127127

128-
pub fn as_bytes(&self) -> &[u8] {
128+
pub const fn as_bytes(&self) -> &[u8] {
129129
match self {
130130
Address::Absent => &[],
131131
Address::Short(value) => value,
@@ -224,7 +224,7 @@ macro_rules! set_fc_bit_field {
224224

225225
impl<T: AsRef<[u8]>> Frame<T> {
226226
/// Input a raw octet buffer with Ethernet frame structure.
227-
pub fn new_unchecked(buffer: T) -> Frame<T> {
227+
pub const fn new_unchecked(buffer: T) -> Frame<T> {
228228
Frame { buffer }
229229
}
230230

@@ -784,7 +784,7 @@ impl Repr {
784784

785785
/// Return the length of a buffer required to hold a packet with the payload of a given length.
786786
#[inline]
787-
pub fn buffer_len(&self) -> usize {
787+
pub const fn buffer_len(&self) -> usize {
788788
3 + 2
789789
+ match self.dst_addr {
790790
Some(Address::Absent) | None => 0,

src/wire/igmp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl fmt::Display for Message {
5454
/// [RFC 2236]: https://tools.ietf.org/html/rfc2236
5555
impl<T: AsRef<[u8]>> Packet<T> {
5656
/// Imbue a raw octet buffer with IGMPv2 packet structure.
57-
pub fn new_unchecked(buffer: T) -> Packet<T> {
57+
pub const fn new_unchecked(buffer: T) -> Packet<T> {
5858
Packet { buffer }
5959
}
6060

@@ -246,7 +246,7 @@ impl Repr {
246246
}
247247

248248
/// Return the length of a packet that will be emitted from this high-level representation.
249-
pub fn buffer_len(&self) -> usize {
249+
pub const fn buffer_len(&self) -> usize {
250250
// always 8 bytes
251251
field::GROUP_ADDRESS.end
252252
}
@@ -304,7 +304,7 @@ fn max_resp_code_to_duration(value: u8) -> Duration {
304304
Duration::from_millis(decisecs * 100)
305305
}
306306

307-
fn duration_to_max_resp_code(duration: Duration) -> u8 {
307+
const fn duration_to_max_resp_code(duration: Duration) -> u8 {
308308
let decisecs = duration.total_millis() / 100;
309309
if decisecs < 128 {
310310
decisecs as u8

0 commit comments

Comments
 (0)