Skip to content

Commit 8fa617d

Browse files
committed
Make functions const where possible
1 parent bbf5794 commit 8fa617d

File tree

7 files changed

+43
-38
lines changed

7 files changed

+43
-38
lines changed

src/bytes.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -76,20 +76,20 @@ pub trait FromBytes: Sized {
7676

7777
impl<'a> Bytes<'a> {
7878
/// Returns a new cursor over a slice of bytes.
79-
pub(crate) fn new(bytes: &'a [u8]) -> Self {
79+
pub(crate) const fn new(bytes: &'a [u8]) -> Self {
8080
Self {
8181
inner: bytes,
8282
pos: 0,
8383
}
8484
}
8585

8686
/// Returns the current position of the cursor.
87-
pub(crate) fn pos(&self) -> usize {
87+
pub(crate) const fn pos(&self) -> usize {
8888
self.pos
8989
}
9090

9191
/// Returns the total length of the original underlying buffer.
92-
pub(crate) fn len(&self) -> usize {
92+
pub(crate) const fn len(&self) -> usize {
9393
self.inner.len()
9494
}
9595

src/field/ampdu_status.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -51,16 +51,6 @@ impl FromBytes for AmpduStatus {
5151
}
5252

5353
impl AmpduStatus {
54-
/// Returns the raw A-MPDU flags.
55-
pub fn flags(&self) -> Flags {
56-
self.flags
57-
}
58-
59-
/// Returns the A-MPDU reference number.
60-
pub fn reference(&self) -> u32 {
61-
self.reference
62-
}
63-
6454
/// Whether the frame is 0-length subframe of this A-MPDU.
6555
pub fn is_zero_len(&self) -> Option<bool> {
6656
self.flags
@@ -88,6 +78,16 @@ impl AmpduStatus {
8878
.contains(Flags::EOF_KNOWN)
8979
.some(|| self.flags.contains(Flags::EOF))
9080
}
81+
82+
/// Returns the raw A-MPDU flags.
83+
pub const fn flags(&self) -> Flags {
84+
self.flags
85+
}
86+
87+
/// Returns the A-MPDU reference number.
88+
pub const fn reference(&self) -> u32 {
89+
self.reference
90+
}
9191
}
9292

9393
#[cfg(test)]

src/field/channel.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ impl FromBytes for Channel {
5151

5252
impl Channel {
5353
/// Returns the channel frequency in MHz.
54-
pub fn freq(&self) -> u16 {
54+
pub const fn freq(&self) -> u16 {
5555
self.freq
5656
}
5757

5858
/// Returns flags describing the channel.
59-
pub fn flags(&self) -> Flags {
59+
pub const fn flags(&self) -> Flags {
6060
self.flags
6161
}
6262
}

src/field/mcs.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,12 @@ impl Mcs {
163163
}
164164

165165
/// Returns the raw known information.
166-
pub fn known(&self) -> Known {
166+
pub const fn known(&self) -> Known {
167167
self.known
168168
}
169169

170170
/// Returns the raw flags.
171-
pub fn flags(&self) -> Flags {
171+
pub const fn flags(&self) -> Flags {
172172
self.flags
173173
}
174174
}

src/field/timestamp.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,9 @@ impl Unit {
9494
}
9595

9696
impl Timestamp {
97-
/// Returns the flags describing the timestamp.
98-
pub fn flags(&self) -> Flags {
99-
self.flags
100-
}
101-
10297
/// Returns the raw timestamp value, in
10398
/// [`.unit()`](struct.Timestamp.html#method.unit) units.
104-
pub fn ts(&self) -> u64 {
99+
pub const fn ts(&self) -> u64 {
105100
self.ts
106101
}
107102

@@ -135,4 +130,9 @@ impl Timestamp {
135130
let bits = self.unit_position >> 4;
136131
SamplingPosition::from_bits(bits).ok_or_else(|| ParseSamplingPositionError(bits))
137132
}
133+
134+
/// Returns the flags describing the timestamp.
135+
pub const fn flags(&self) -> Flags {
136+
self.flags
137+
}
138138
}

src/field/vht.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -115,22 +115,22 @@ pub struct Vht {
115115

116116
impl User {
117117
/// Returns the VHT index (1 - 9).
118-
pub fn index(&self) -> u8 {
118+
pub const fn index(&self) -> u8 {
119119
self.index
120120
}
121121

122122
/// Returns the number of spatial streams (1 - 8).
123-
pub fn nss(&self) -> u8 {
123+
pub const fn nss(&self) -> u8 {
124124
self.nss
125125
}
126126

127127
/// Returns the number of space-time streams.
128-
pub fn nsts(&self) -> u8 {
128+
pub const fn nsts(&self) -> u8 {
129129
self.nsts
130130
}
131131

132132
/// Returns the FEC type.
133-
pub fn fec(&self) -> Fec {
133+
pub const fn fec(&self) -> Fec {
134134
self.fec
135135
}
136136
}
@@ -230,12 +230,12 @@ impl Vht {
230230
}
231231

232232
/// Returns the raw known information.
233-
pub fn known(&self) -> Known {
233+
pub const fn known(&self) -> Known {
234234
self.known
235235
}
236236

237237
/// Returns the raw flags.
238-
pub fn flags(&self) -> Flags {
238+
pub const fn flags(&self) -> Flags {
239239
self.flags
240240
}
241241
}

src/lib.rs

+14-9
Original file line numberDiff line numberDiff line change
@@ -213,12 +213,12 @@ pub struct Header {
213213

214214
impl Field {
215215
/// Returns this field's namespace.
216-
pub fn namespace(&self) -> &Namespace {
216+
pub const fn namespace(&self) -> &Namespace {
217217
&self.namespace
218218
}
219219

220220
/// Returns this field's presence bit number.
221-
pub fn bit(&self) -> u32 {
221+
pub const fn bit(&self) -> u32 {
222222
self.bit
223223
}
224224
}
@@ -266,12 +266,12 @@ impl<'a> Iter<'a> {
266266
}
267267

268268
/// Returns the version of the radiotap header.
269-
pub fn version(&self) -> u8 {
269+
pub const fn version(&self) -> u8 {
270270
VERSION
271271
}
272272

273273
/// Returns the entire length of the radiotap header.
274-
pub fn length(&self) -> usize {
274+
pub const fn length(&self) -> usize {
275275
self.length
276276
}
277277

@@ -287,7 +287,7 @@ impl<'a> Iter<'a> {
287287
///
288288
/// let iter = radiotap::Iter::new(capture).unwrap().into_default();
289289
/// ```
290-
pub fn into_default(self) -> IterDefault<'a> {
290+
pub const fn into_default(self) -> IterDefault<'a> {
291291
IterDefault { inner: self }
292292
}
293293

@@ -381,12 +381,14 @@ impl<'a> Iter<'a> {
381381

382382
impl IterDefault<'_> {
383383
/// Returns the version of the radiotap header.
384-
pub fn version(&self) -> u8 {
384+
#[inline]
385+
pub const fn version(&self) -> u8 {
385386
self.inner.version()
386387
}
387388

388389
/// Returns the entire length of the radiotap header.
389-
pub fn length(&self) -> usize {
390+
#[inline]
391+
pub const fn length(&self) -> usize {
390392
self.inner.length()
391393
}
392394

@@ -412,11 +414,13 @@ impl IterDefault<'_> {
412414
}
413415

414416
/// Skip the given kind of field.
417+
#[inline]
415418
pub fn skip(&mut self, kind: Type) -> Result<()> {
416419
self.inner.skip(kind)
417420
}
418421

419422
/// Reads the given kind of field.
423+
#[inline]
420424
pub fn read<U, E>(&mut self, kind: Type) -> Result<U>
421425
where
422426
U: FromBytes<Error = E>,
@@ -425,6 +429,7 @@ impl IterDefault<'_> {
425429
self.inner.read(kind)
426430
}
427431

432+
#[inline]
428433
fn read_some<U, E>(&mut self, kind: Type) -> Result<Option<U>>
429434
where
430435
U: FromBytes<Error = E>,
@@ -493,13 +498,13 @@ pub fn parse(capture: &[u8]) -> Result<Header> {
493498
impl Header {
494499
/// Returns the version of the radiotap header.
495500
#[inline]
496-
pub fn version(&self) -> u8 {
501+
pub const fn version(&self) -> u8 {
497502
VERSION
498503
}
499504

500505
/// Returns the length of the entire radiotap header.
501506
#[inline]
502-
pub fn length(&self) -> usize {
507+
pub const fn length(&self) -> usize {
503508
self.length
504509
}
505510
}

0 commit comments

Comments
 (0)