Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Parse traits #11

Merged
merged 6 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,46 +68,44 @@ mod simd;
mod test;

pub use crate::error::AtoiSimdError;
use crate::linker::{Parser, ParserNeg, ParserPos};
use crate::linker::{Parse, ParseNeg};

/// Parses slice of digits, and checks first '-' char for signed integers.
#[inline]
pub fn parse<T: Parser<T> + ParserPos<T>>(s: &[u8]) -> Result<T, AtoiSimdError> {
pub fn parse<T: Parse>(s: &[u8]) -> Result<T, AtoiSimdError> {
T::atoi_simd_parse(s)
}

/// Parses positive integer.
#[inline]
pub fn parse_pos<T: ParserPos<T>>(s: &[u8]) -> Result<T, AtoiSimdError> {
pub fn parse_pos<T: Parse>(s: &[u8]) -> Result<T, AtoiSimdError> {
T::atoi_simd_parse_pos(s)
}

/// Parses negative integer. Slice must not contain '-' sign.
#[inline]
pub fn parse_neg<T: ParserNeg<T>>(s: &[u8]) -> Result<T, AtoiSimdError> {
pub fn parse_neg<T: ParseNeg>(s: &[u8]) -> Result<T, AtoiSimdError> {
T::atoi_simd_parse_neg(s)
}

/// Parses slice of digits until it reaches invalid character, and checks first '-' char for signed integers.
/// Returns parsed value and parsed size of the slice.
#[inline]
pub fn parse_until_invalid<T: Parser<T> + ParserPos<T>>(
s: &[u8],
) -> Result<(T, usize), AtoiSimdError> {
pub fn parse_until_invalid<T: Parse>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> {
T::atoi_simd_parse_until_invalid(s)
}

/// Parses positive integer until it reaches invalid character.
/// Returns parsed value and parsed size of the slice.
#[inline]
pub fn parse_until_invalid_pos<T: ParserPos<T>>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> {
pub fn parse_until_invalid_pos<T: Parse>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> {
T::atoi_simd_parse_until_invalid_pos(s)
}

/// Parses negative integer until it reaches invalid character. Slice must not contain '-' sign.
/// Returns parsed value and parsed size of the slice.
#[inline]
pub fn parse_until_invalid_neg<T: ParserNeg<T>>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> {
pub fn parse_until_invalid_neg<T: ParseNeg>(s: &[u8]) -> Result<(T, usize), AtoiSimdError> {
T::atoi_simd_parse_until_invalid_neg(s)
}

Expand All @@ -116,6 +114,6 @@ pub fn parse_until_invalid_neg<T: ParserNeg<T>>(s: &[u8]) -> Result<(T, usize),
/// Skips '+' char and extra zeroes at the beginning.
/// It's slower than `parse()`.
#[inline]
pub fn parse_skipped<T: Parser<T> + ParserPos<T>>(s: &[u8]) -> Result<T, AtoiSimdError> {
pub fn parse_skipped<T: Parse>(s: &[u8]) -> Result<T, AtoiSimdError> {
T::atoi_simd_parse_skipped(s)
}
24 changes: 12 additions & 12 deletions src/linker/fb_32.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::fallback::*;

impl ParserPos<u8> for u8 {
impl ParsePos for u8 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<u8, AtoiSimdError> {
parse_fb_checked_pos::<{ u8::MAX as u64 }>(s).map(|v| v as u8)
Expand All @@ -13,7 +13,7 @@ impl ParserPos<u8> for u8 {
}
}

impl ParserPos<i8> for i8 {
impl ParsePos for i8 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<i8, AtoiSimdError> {
parse_fb_checked_pos::<{ i8::MAX as u64 }>(s).map(|v| v as i8)
Expand All @@ -25,7 +25,7 @@ impl ParserPos<i8> for i8 {
}
}

impl ParserNeg<i8> for i8 {
impl ParseNeg for i8 {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<i8, AtoiSimdError> {
parse_fb_checked_neg::<{ i8::MIN as i64 }>(s).map(|v| v as i8)
Expand All @@ -37,7 +37,7 @@ impl ParserNeg<i8> for i8 {
}
}

impl ParserPos<u16> for u16 {
impl ParsePos for u16 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<u16, AtoiSimdError> {
parse_fb_checked_pos::<{ u16::MAX as u64 }>(s).map(|v| v as u16)
Expand All @@ -49,7 +49,7 @@ impl ParserPos<u16> for u16 {
}
}

impl ParserPos<i16> for i16 {
impl ParsePos for i16 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<i16, AtoiSimdError> {
parse_fb_checked_pos::<{ i16::MAX as u64 }>(s).map(|v| v as i16)
Expand All @@ -61,7 +61,7 @@ impl ParserPos<i16> for i16 {
}
}

impl ParserNeg<i16> for i16 {
impl ParseNeg for i16 {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<i16, AtoiSimdError> {
parse_fb_checked_neg::<{ i16::MIN as i64 }>(s).map(|v| v as i16)
Expand All @@ -73,7 +73,7 @@ impl ParserNeg<i16> for i16 {
}
}

impl ParserPos<u32> for u32 {
impl ParsePos for u32 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<u32, AtoiSimdError> {
parse_fb_checked_pos::<{ u32::MAX as u64 }>(s).map(|v| v as u32)
Expand All @@ -85,7 +85,7 @@ impl ParserPos<u32> for u32 {
}
}

impl ParserPos<i32> for i32 {
impl ParsePos for i32 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<i32, AtoiSimdError> {
parse_fb_checked_pos::<{ i32::MAX as u64 }>(s).map(|v| v as i32)
Expand All @@ -97,7 +97,7 @@ impl ParserPos<i32> for i32 {
}
}

impl ParserNeg<i32> for i32 {
impl ParseNeg for i32 {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<i32, AtoiSimdError> {
parse_fb_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as i32)
Expand All @@ -110,7 +110,7 @@ impl ParserNeg<i32> for i32 {
}

#[cfg(target_pointer_width = "32")]
impl ParserPos<usize> for usize {
impl ParsePos for usize {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<usize, AtoiSimdError> {
parse_fb_checked_pos::<{ u32::MAX as u64 }>(s).map(|v| v as usize)
Expand All @@ -123,7 +123,7 @@ impl ParserPos<usize> for usize {
}

#[cfg(target_pointer_width = "32")]
impl ParserPos<isize> for isize {
impl ParsePos for isize {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<isize, AtoiSimdError> {
parse_fb_checked_pos::<{ i32::MAX as u64 }>(s).map(|v| v as isize)
Expand All @@ -136,7 +136,7 @@ impl ParserPos<isize> for isize {
}

#[cfg(target_pointer_width = "32")]
impl ParserNeg<isize> for isize {
impl ParseNeg for isize {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<isize, AtoiSimdError> {
parse_fb_checked_neg::<{ i32::MIN as i64 }>(s).map(|v| v as isize)
Expand Down
18 changes: 9 additions & 9 deletions src/linker/fb_64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;
use crate::fallback::*;

#[cfg(target_pointer_width = "64")]
impl ParserPos<usize> for usize {
impl ParsePos for usize {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<usize, AtoiSimdError> {
parse_fb_checked_64_pos::<{ u64::MAX }, 4>(s).map(|v| v as usize)
Expand All @@ -15,7 +15,7 @@ impl ParserPos<usize> for usize {
}

#[cfg(target_pointer_width = "64")]
impl ParserPos<isize> for isize {
impl ParsePos for isize {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<isize, AtoiSimdError> {
parse_fb_checked_64_pos::<{ i64::MAX as u64 }, 3>(s).map(|v| v as isize)
Expand All @@ -28,7 +28,7 @@ impl ParserPos<isize> for isize {
}

#[cfg(target_pointer_width = "64")]
impl ParserNeg<isize> for isize {
impl ParseNeg for isize {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<isize, AtoiSimdError> {
parse_fb_checked_64_neg(s).map(|v| v as isize)
Expand All @@ -40,7 +40,7 @@ impl ParserNeg<isize> for isize {
}
}

impl ParserPos<u64> for u64 {
impl ParsePos for u64 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<u64, AtoiSimdError> {
parse_fb_checked_64_pos::<{ u64::MAX }, 4>(s)
Expand All @@ -52,7 +52,7 @@ impl ParserPos<u64> for u64 {
}
}

impl ParserPos<i64> for i64 {
impl ParsePos for i64 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<i64, AtoiSimdError> {
parse_fb_checked_64_pos::<{ i64::MAX as u64 }, 3>(s).map(|v| v as i64)
Expand All @@ -64,7 +64,7 @@ impl ParserPos<i64> for i64 {
}
}

impl ParserNeg<i64> for i64 {
impl ParseNeg for i64 {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<i64, AtoiSimdError> {
parse_fb_checked_64_neg(s)
Expand All @@ -76,7 +76,7 @@ impl ParserNeg<i64> for i64 {
}
}

impl ParserPos<u128> for u128 {
impl ParsePos for u128 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<u128, AtoiSimdError> {
parse_fb_checked_128_pos::<{ u128::MAX }>(s)
Expand All @@ -88,7 +88,7 @@ impl ParserPos<u128> for u128 {
}
}

impl ParserPos<i128> for i128 {
impl ParsePos for i128 {
#[inline(always)]
fn atoi_simd_parse_pos(s: &[u8]) -> Result<i128, AtoiSimdError> {
parse_fb_checked_128_pos::<{ i128::MAX as u128 }>(s).map(|v| v as i128)
Expand All @@ -100,7 +100,7 @@ impl ParserPos<i128> for i128 {
}
}

impl ParserNeg<i128> for i128 {
impl ParseNeg for i128 {
#[inline(always)]
fn atoi_simd_parse_neg(s: &[u8]) -> Result<i128, AtoiSimdError> {
parse_fb_checked_128_neg(s)
Expand Down
Loading