Skip to content

Commit

Permalink
Allow using static with the output of the bytes/ascii_chars mac…
Browse files Browse the repository at this point in the history
…ros only
  • Loading branch information
Dr-Emann committed Oct 13, 2023
1 parent 816c72d commit 016e328
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
4 changes: 2 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ fn macros_bytes(f: &mut File, base: &Path) {
let closure = format!("|c| {}", closure_body.join(" || "));

format!(
"({}) => ($crate::Bytes::new([{}], {}, {}));\n",
"({}) => ($crate::Bytes::new_const([{}], {}, {}));\n",
args, array, max, closure
)
})
Expand Down Expand Up @@ -93,7 +93,7 @@ fn macros_ascii_chars(f: &mut File, base: &Path) {
let closure = format!("|c| {}", closure_body.join(" || "));

format!(
"({}) => ($crate::AsciiChars::new([{}], {}, {}));\n",
"({}) => ($crate::AsciiChars::new_const([{}], {}, {}));\n",
args, array, max, closure
)
})
Expand Down
48 changes: 45 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,8 @@ pub struct Bytes<F> {
_fallback: PhantomData<F>,
}

impl Bytes<fn(u8) -> bool> {}

impl<F> Bytes<F>
where
F: Fn(u8) -> bool,
Expand All @@ -203,7 +205,7 @@ where
/// intrinsics are not available. The closure **must** search for
/// the same bytes as in the array.
#[allow(unused_variables)]
pub const fn new(bytes: [u8; 16], len: i32, fallback: F) -> Self {
pub fn new(bytes: [u8; 16], len: i32, fallback: F) -> Self {
Bytes {
#[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))]
simd: simd::Bytes::new(bytes, len),
Expand All @@ -215,6 +217,28 @@ where
}
}

// This constructor is used by the `bytes!` macro, and shouldn't be relied on directly.
//
// To allow F to be dropped in a const context, we require F to be Copy
// (thus, have no destructor). If we _know_ we're using sse4.2, we don't use
// the fallback at all, so it will be dropped in this function.
#[doc(hidden)]
#[allow(unused_variables)]
pub const fn new_const(bytes: [u8; 16], len: i32, fallback: F) -> Self
where
F: Copy,
{
Self {
#[cfg(any(jetscii_sse4_2 = "yes", jetscii_sse4_2 = "maybe"))]
simd: simd::Bytes::new(bytes, len),

#[cfg(any(jetscii_sse4_2 = "maybe", jetscii_sse4_2 = "no"))]
fallback: fallback::Bytes::new(fallback),

_fallback: PhantomData,
}
}

/// Searches the slice for the first matching byte in the set.
#[inline]
pub fn find(&self, haystack: &[u8]) -> Option<usize> {
Expand All @@ -232,6 +256,8 @@ pub type BytesConst = Bytes<fn(u8) -> bool>;
/// characters may be used.
pub struct AsciiChars<F>(Bytes<F>);

impl AsciiChars<fn(u8) -> bool> {}

impl<F> AsciiChars<F>
where
F: Fn(u8) -> bool,
Expand All @@ -246,13 +272,29 @@ where
/// ### Panics
///
/// - If you provide a non-ASCII byte.
pub const fn new(chars: [u8; 16], len: i32, fallback: F) -> Self {
pub fn new(chars: [u8; 16], len: i32, fallback: F) -> Self {
for &b in &chars {
assert!(b < 128, "Cannot have non-ASCII bytes");
}
Self(Bytes::new(chars, len, fallback))
}

// This constructor is used by the `ascii_chars!` macro, and shouldn't be relied on directly.
//
// To allow F to be dropped in a const context, we require F to be Copy
// (thus, have no destructor). If we _know_ we're using sse4.2, we don't use
// the fallback at all, so it will be dropped in this function.
#[doc(hidden)]
pub const fn new_const(chars: [u8; 16], len: i32, fallback: F) -> Self
where
F: Copy,
{
let mut i = 0;
while i < chars.len() {
assert!(chars[i] < 128, "Cannot have non-ASCII bytes");
i += 1;
}
AsciiChars(Bytes::new(chars, len, fallback))
Self(Bytes::new_const(chars, len, fallback))
}

/// Searches the string for the first matching ASCII byte in the set.
Expand Down

0 comments on commit 016e328

Please sign in to comment.