Skip to content

Commit 93b4c82

Browse files
bors[bot]Nicholas Cyprus
andauthored
Merge #693
693: Constification of the wire module r=Dirbaio a=ngc0202 A huge portion of the `wire` module is constifyable, as shown in this commit. Making these `const fn`s, especially the functions set aside in the `field` modules of wire, would be greatly beneficial to me as a consumer of this crate, and I imagine many others. Many of the structs in my own crate's "wire" module for a custom protocol are more constrained versions of the packets and reprs provided by smoltcp. Due to being more constrained, many fields which are variable in the generic reprs are now constant in mine, which then lend themselves to being constants in my crate, however these functions and methods not being marked `const` prevents me from using smoltcp's tools to calculate these values. **Most** of the functions which I marked `const` in this commit are by their very nature/semantics guaranteed to be constifiable anyway, however a handful aren't necessarily, therefore: **Important note:** While the majority of the functions are marked `const` are very safe to do so, not all of them are guaranteed to be so, and the reviewer of this PR should take caution and feel free to unmark something as const if they're not comfortable making that guarantee in the public API. Another note: There are various other functions which can't be made `const fn` as written, but could be with minor tweaks. I did not include these tweaks but I think it should be considered as further work for this PR or a potential follow-up PR. Furthermore, plenty outside of `wire` is constifyable as well but I did not include those in the scope of this PR. Co-authored-by: Nicholas Cyprus <nicholas.cyprus@caci.com>
2 parents ef59b94 + a99c059 commit 93b4c82

26 files changed

+108
-108
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
# Failure is permitted on nightly.
2222
rust:
2323
- stable
24-
- 1.60.0
24+
- 1.61.0
2525
- nightly
2626

2727
features:
@@ -65,7 +65,7 @@ jobs:
6565
# Failure is permitted on nightly.
6666
rust:
6767
- stable
68-
- 1.60.0
68+
- 1.61.0
6969
- nightly
7070

7171
features:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "smoltcp"
33
version = "0.8.1"
44
edition = "2018"
5-
rust-version = "1.60"
5+
rust-version = "1.61"
66
authors = ["whitequark <whitequark@whitequark.org>"]
77
description = "A TCP/IP stack designed for bare-metal, real-time systems without a heap."
88
documentation = "https://docs.rs/smoltcp/"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ include complicated compile-time computations, such as macro or type tricks, eve
1111
at cost of performance degradation.
1212

1313
_smoltcp_ does not need heap allocation *at all*, is [extensively documented][docs],
14-
and compiles on stable Rust 1.60 and later.
14+
and compiles on stable Rust 1.61 and later.
1515

1616
_smoltcp_ achieves [~Gbps of throughput](#examplesbenchmarkrs) when tested against
1717
the Linux TCP stack in loopback mode.

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@
6565
//!
6666
//! # Minimum Supported Rust Version (MSRV)
6767
//!
68-
//! This crate is guaranteed to compile on stable Rust 1.60 and up with any valid set of features.
68+
//! This crate is guaranteed to compile on stable Rust 1.61 and up with any valid set of features.
6969
//! It *might* compile on older versions but that may change in any new patch release.
7070
//!
7171
//! The exception is when using the `defmt` feature, in which case `defmt`'s MSRV applies, which

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, .. }

0 commit comments

Comments
 (0)