Skip to content

Commit e48b248

Browse files
authored
refactor: use const generics from Rust 1.51 (#220)
1 parent ac44a38 commit e48b248

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

src/iter.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use core::convert::TryFrom;
21
use core::convert::TryInto;
32

43
#[allow(missing_docs)]
@@ -63,11 +62,8 @@ impl<'a> Bytes<'a> {
6362
}
6463

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

7369
/// Advance by 1, equivalent to calling `advance(1)`.

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -762,10 +762,10 @@ pub const EMPTY_HEADER: Header<'static> = Header { name: "", value: b"" };
762762
#[allow(missing_docs)]
763763
// WARNING: Exported for internal benchmarks, not fit for public consumption
764764
pub fn parse_version(bytes: &mut Bytes) -> Result<u8> {
765-
if let Some(eight) = bytes.peek_n::<[u8; 8]>(8) {
765+
if let Some(eight) = bytes.peek_n::<8>() {
766766
const H10: u64 = u64::from_ne_bytes(*b"HTTP/1.0");
767767
const H11: u64 = u64::from_ne_bytes(*b"HTTP/1.1");
768-
// SAFETY: peek_n(8) before ensure within bounds
768+
// SAFETY: peek_n before ensures within bounds
769769
unsafe {
770770
bytes.advance(8);
771771
}
@@ -797,7 +797,7 @@ pub fn parse_version(bytes: &mut Bytes) -> Result<u8> {
797797
pub fn parse_method<'a>(bytes: &mut Bytes<'a>) -> Result<&'a str> {
798798
const GET: [u8; 4] = *b"GET ";
799799
const POST: [u8; 4] = *b"POST";
800-
match bytes.peek_n::<[u8; 4]>(4) {
800+
match bytes.peek_n::<4>() {
801801
Some(GET) => {
802802
// SAFETY: we matched "GET " which has 4 bytes and is ASCII
803803
let method = unsafe {

src/simd/swar.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ type ByteBlock = [u8; BLOCK_SIZE];
99
#[inline]
1010
pub fn match_uri_vectored(bytes: &mut Bytes) {
1111
loop {
12-
if let Some(bytes8) = bytes.peek_n::<ByteBlock>(BLOCK_SIZE) {
12+
if let Some(bytes8) = bytes.peek_n::<BLOCK_SIZE>() {
1313
let n = match_uri_char_8_swar(bytes8);
1414
// SAFETY: using peek_n to retrieve the bytes ensures that there are at least n more bytes
1515
// in `bytes`, so calling `advance(n)` is safe.
@@ -37,7 +37,7 @@ pub fn match_uri_vectored(bytes: &mut Bytes) {
3737
#[inline]
3838
pub fn match_header_value_vectored(bytes: &mut Bytes) {
3939
loop {
40-
if let Some(bytes8) = bytes.peek_n::<ByteBlock>(BLOCK_SIZE) {
40+
if let Some(bytes8) = bytes.peek_n::<BLOCK_SIZE>() {
4141
let n = match_header_value_char_8_swar(bytes8);
4242
// SAFETY: using peek_n to retrieve the bytes ensures that there are at least n more bytes
4343
// in `bytes`, so calling `advance(n)` is safe.
@@ -64,7 +64,7 @@ pub fn match_header_value_vectored(bytes: &mut Bytes) {
6464

6565
#[inline]
6666
pub fn match_header_name_vectored(bytes: &mut Bytes) {
67-
while let Some(block) = bytes.peek_n::<ByteBlock>(BLOCK_SIZE) {
67+
while let Some(block) = bytes.peek_n::<BLOCK_SIZE>() {
6868
let n = match_block(is_header_name_token, block);
6969
// SAFETY: using peek_n to retrieve the bytes ensures that there are at least n more bytes
7070
// in `bytes`, so calling `advance(n)` is safe.

0 commit comments

Comments
 (0)