Skip to content

Commit 3614726

Browse files
authored
refactor: update to use APIs up to MSRV (#206)
- {integer}::from_ne_bytes const since 1.44 - {integer}::trailing_ones stable since 1.46 - str::strip_prefix stable since 1.45
1 parent 9f29e79 commit 3614726

File tree

6 files changed

+19
-31
lines changed

6 files changed

+19
-31
lines changed

build.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -99,11 +99,11 @@ struct Version (u32, u32, u32);
9999

100100
impl Version {
101101
fn parse(s: &str) -> Result<Version, String> {
102-
if !s.starts_with("rustc ") {
103-
return Err(format!("unrecognized version string: {}", s));
104-
}
105-
let s = s.trim_start_matches("rustc ");
106-
102+
let s = match s.strip_prefix("rustc ") {
103+
Some(s) => s,
104+
None => return Err(format!("unrecognized version string: {}", s)),
105+
};
106+
107107
let mut iter = s
108108
.split('.')
109109
.take(3)

src/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a> Bytes<'a> {
6464

6565
#[inline]
6666
pub fn peek_n<'b: 'a, U: TryFrom<&'a [u8]>>(&'b self, n: usize) -> Option<U> {
67-
// TODO: once we bump MSRC, use const generics to allow only [u8; N] reads
67+
// TODO: once we bump MSRV, use const generics to allow only [u8; N] reads
6868
// TODO: drop `n` arg in favour of const
6969
// let n = core::mem::size_of::<U>();
7070
self.as_ref().get(..n)?.try_into().ok()

src/lib.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -757,21 +757,16 @@ pub const EMPTY_HEADER: Header<'static> = Header { name: "", value: b"" };
757757
// WARNING: Exported for internal benchmarks, not fit for public consumption
758758
pub fn parse_version(bytes: &mut Bytes) -> Result<u8> {
759759
if let Some(eight) = bytes.peek_n::<[u8; 8]>(8) {
760-
// NOTE: should be const once MSRV >= 1.44
761-
let h10: u64 = u64::from_ne_bytes(*b"HTTP/1.0");
762-
let h11: u64 = u64::from_ne_bytes(*b"HTTP/1.1");
760+
const H10: u64 = u64::from_ne_bytes(*b"HTTP/1.0");
761+
const H11: u64 = u64::from_ne_bytes(*b"HTTP/1.1");
763762
// SAFETY: peek_n(8) before ensure within bounds
764763
unsafe {
765764
bytes.advance(8);
766765
}
767-
let block = u64::from_ne_bytes(eight);
768-
// NOTE: should be match once h10 & h11 are consts
769-
return if block == h10 {
770-
Ok(Status::Complete(0))
771-
} else if block == h11 {
772-
Ok(Status::Complete(1))
773-
} else {
774-
Err(Error::Version)
766+
return match u64::from_ne_bytes(eight) {
767+
H10 => Ok(Status::Complete(0)),
768+
H11 => Ok(Status::Complete(1)),
769+
_ => Err(Error::Version),
775770
};
776771
}
777772

src/simd/avx2.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,8 @@ unsafe fn match_url_char_32_avx(buf: &[u8]) -> usize {
7474
// Simply, we're converting a vector value to scalar value here.
7575
let res = _mm256_movemask_epi8(bit) as u32;
7676

77-
// Count trailing zeros to find the first encountered invalid character.
78-
// Bitwise NOT is required once again to flip truthiness.
79-
// TODO: use .trailing_ones() once MSRV >= 1.46
80-
(!res).trailing_zeros() as usize
77+
// Count trailing ones to find the first encountered invalid character.
78+
res.trailing_ones() as usize
8179
}
8280

8381
#[target_feature(enable = "avx2")]
@@ -138,10 +136,8 @@ unsafe fn match_header_value_char_32_avx(buf: &[u8]) -> usize {
138136
// Creates a scalar value from vector value.
139137
let res = _mm256_movemask_epi8(bit) as u32;
140138

141-
// Count trailing zeros to find the first encountered invalid character.
142-
// Bitwise NOT is required once again to flip truthiness.
143-
// TODO: use .trailing_ones() once MSRV >= 1.46
144-
(!res).trailing_zeros() as usize
139+
// Count trailing ones to find the first encountered invalid character.
140+
res.trailing_ones() as usize
145141
}
146142

147143
#[test]

src/simd/sse42.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ unsafe fn match_url_char_16_sse(buf: &[u8]) -> usize {
3737
let bit = _mm_andnot_si128(del, low);
3838
let res = _mm_movemask_epi8(bit) as u16;
3939

40-
// TODO: use .trailing_ones() once MSRV >= 1.46
41-
(!res).trailing_zeros() as usize
40+
res.trailing_ones() as usize
4241
}
4342

4443
#[target_feature(enable = "sse4.2")]
@@ -79,8 +78,7 @@ unsafe fn match_header_value_char_16_sse(buf: &[u8]) -> usize {
7978
let bit = _mm_andnot_si128(del, _mm_or_si128(low, tab));
8079
let res = _mm_movemask_epi8(bit) as u16;
8180

82-
// TODO: use .trailing_ones() once MSRV >= 1.46
83-
(!res).trailing_zeros() as usize
81+
res.trailing_ones() as usize
8482
}
8583

8684
#[test]

src/simd/swar.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@ fn match_block(f: impl Fn(u8) -> bool, block: ByteBlock) -> usize {
103103
BLOCK_SIZE
104104
}
105105

106-
// A const alternative to u64::from_ne_bytes to avoid bumping MSRV (1.36 => 1.44)
107106
// creates a u64 whose bytes are each equal to b
108107
const fn uniform_block(b: u8) -> usize {
109-
(b as u64 * 0x01_01_01_01_01_01_01_01 /* [1_u8; 8] */) as usize
108+
usize::from_ne_bytes([b; BLOCK_SIZE])
110109
}
111110

112111
// A byte-wise range-check on an entire word/block,

0 commit comments

Comments
 (0)