From 343badfcaf7f08689f7d1f98af45505508e9c964 Mon Sep 17 00:00:00 2001 From: Jon Doron Date: Wed, 15 Jan 2025 10:07:32 +0200 Subject: [PATCH] Add no std support Signed-off-by: Jon Doron --- Cargo.toml | 9 ++++++--- src/aarch64.rs | 2 +- src/lib.rs | 30 +++++++++++++++++++++--------- src/stdsimd.rs | 2 +- src/wasm32.rs | 10 +++++----- src/x86.rs | 33 ++++++++++++++++++--------------- tests/i386.rs | 2 +- 7 files changed, 53 insertions(+), 35 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 504aa74..9ee0b2f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,14 +12,17 @@ keywords = ["search", "text", "string", "single", "simd"] [dependencies] cfg-if = "1" paste = "1" -memchr = "2.3" +memchr = { version = "2.7.4", default-features = false } seq-macro = "0.3" [dev-dependencies] -memmap2 = "0.5" +memmap2 = "0.9" [profile.release] debug = true [features] -stdsimd = [] +default = ["std"] +alloc = [] +std = ["alloc"] +stdsimd = ["std"] diff --git a/src/aarch64.rs b/src/aarch64.rs index 22526ad..d4a1519 100644 --- a/src/aarch64.rs +++ b/src/aarch64.rs @@ -3,7 +3,7 @@ use crate::{Needle, NeedleWithSize, Searcher, Vector, VectorHash}; #[cfg(target_arch = "aarch64")] -use std::arch::aarch64::*; +use core::arch::aarch64::*; static MD: [u8; 16] = [ 1 << 0, diff --git a/src/lib.rs b/src/lib.rs index 29019da..0f8f37e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ #![warn(missing_docs)] #![cfg_attr(feature = "stdsimd", feature(portable_simd))] +#![cfg_attr(not(feature = "std"), no_std)] /// Substring search implementations using aarch64 architecture features. #[cfg(target_arch = "aarch64")] @@ -23,9 +24,12 @@ pub mod x86; #[cfg(target_arch = "wasm32")] pub mod wasm32; +#[cfg(feature = "alloc")] +extern crate alloc; + +#[cfg(feature = "alloc")] +use alloc::{boxed::Box, rc::Rc, sync::Arc, vec::Vec}; use memchr::memchr; -use std::rc::Rc; -use std::sync::Arc; #[macro_use] mod multiversion; @@ -58,6 +62,7 @@ impl Needle for [u8] { } } +#[cfg(feature = "alloc")] impl Needle for Box { const SIZE: Option = N::SIZE; @@ -67,6 +72,7 @@ impl Needle for Box { } } +#[cfg(feature = "alloc")] impl Needle for Rc { const SIZE: Option = N::SIZE; @@ -76,6 +82,7 @@ impl Needle for Rc { } } +#[cfg(feature = "alloc")] impl Needle for Arc { const SIZE: Option = N::SIZE; @@ -94,6 +101,7 @@ impl Needle for &N { } } +#[cfg(feature = "alloc")] impl Needle for Vec { const SIZE: Option = None; @@ -189,10 +197,10 @@ impl> From<&VectorHash> for VectorHash { macro_rules! memcmp { ($chunk:ident, $needle:ident, $len:literal) => { - std::slice::from_raw_parts($chunk, $len) == std::slice::from_raw_parts($needle, $len) + core::slice::from_raw_parts($chunk, $len) == core::slice::from_raw_parts($needle, $len) }; ($chunk:ident, $needle:ident, $len:ident) => { - std::slice::from_raw_parts($chunk, $len) == std::slice::from_raw_parts($needle, $len) + core::slice::from_raw_parts($chunk, $len) == core::slice::from_raw_parts($needle, $len) }; } @@ -294,7 +302,9 @@ trait Searcher { #[cfg(test)] mod tests { - use super::{MemchrSearcher, Needle}; + use super::MemchrSearcher; + #[cfg(feature = "alloc")] + use super::Needle; fn memchr_search(haystack: &[u8], needle: &[u8]) -> bool { MemchrSearcher::new(needle[0]).search_in(haystack) @@ -331,9 +341,10 @@ mod tests { } #[test] + #[cfg(feature = "alloc")] fn needle_array_size() { - use std::rc::Rc; - use std::sync::Arc; + use alloc::rc::Rc; + use alloc::sync::Arc; assert_eq!(<[u8; 0] as Needle>::SIZE, Some(0)); @@ -347,9 +358,10 @@ mod tests { } #[test] + #[cfg(feature = "alloc")] fn needle_slice_size() { - use std::rc::Rc; - use std::sync::Arc; + use alloc::rc::Rc; + use alloc::sync::Arc; assert_eq!(Box::<[u8]>::SIZE, None); diff --git a/src/stdsimd.rs b/src/stdsimd.rs index ff10a56..d537b8b 100644 --- a/src/stdsimd.rs +++ b/src/stdsimd.rs @@ -27,7 +27,7 @@ where #[inline] unsafe fn load(a: *const u8) -> Self { - std::ptr::read_unaligned(a as *const Self) + core::ptr::read_unaligned(a as *const Self) } #[inline] diff --git a/src/wasm32.rs b/src/wasm32.rs index 231aa00..8d74808 100644 --- a/src/wasm32.rs +++ b/src/wasm32.rs @@ -2,7 +2,7 @@ use crate::{Needle, NeedleWithSize, Searcher, Vector, VectorHash}; #[cfg(target_arch = "wasm32")] -use std::arch::wasm32::*; +use core::arch::wasm32::*; impl Vector for v128 { const LANES: usize = 16; @@ -17,7 +17,7 @@ impl Vector for v128 { #[inline] #[target_feature(enable = "simd128")] unsafe fn load(a: *const u8) -> Self { - std::ptr::read_unaligned(a as *const v128) + core::ptr::read_unaligned(a as *const v128) } #[inline] @@ -57,7 +57,7 @@ impl Vector for v64 { #[inline] #[target_feature(enable = "simd128")] unsafe fn load(a: *const u8) -> Self { - Self(u64x2_splat(std::ptr::read_unaligned(a as *const u64))) + Self(u64x2_splat(core::ptr::read_unaligned(a as *const u64))) } #[inline] @@ -103,7 +103,7 @@ impl Vector for v32 { #[inline] #[target_feature(enable = "simd128")] unsafe fn load(a: *const u8) -> Self { - Self(u32x4_splat(std::ptr::read_unaligned(a as *const u32))) + Self(u32x4_splat(core::ptr::read_unaligned(a as *const u32))) } #[inline] @@ -149,7 +149,7 @@ impl Vector for v16 { #[inline] #[target_feature(enable = "simd128")] unsafe fn load(a: *const u8) -> Self { - Self(u16x8_splat(std::ptr::read_unaligned(a as *const u16))) + Self(u16x8_splat(core::ptr::read_unaligned(a as *const u16))) } #[inline] diff --git a/src/x86.rs b/src/x86.rs index 6639977..8a019e8 100644 --- a/src/x86.rs +++ b/src/x86.rs @@ -17,11 +17,11 @@ #![allow(clippy::missing_safety_doc)] use crate::{MemchrSearcher, Needle, NeedleWithSize, Searcher, Vector, VectorHash}; -use seq_macro::seq; #[cfg(target_arch = "x86")] -use std::arch::x86::*; +use core::arch::x86::*; #[cfg(target_arch = "x86_64")] -use std::arch::x86_64::*; +use core::arch::x86_64::*; +use seq_macro::seq; #[derive(Clone, Copy)] #[repr(transparent)] @@ -41,7 +41,7 @@ impl Vector for __m16i { #[inline] #[target_feature(enable = "avx2")] unsafe fn load(a: *const u8) -> Self { - __m16i(_mm_set1_epi16(std::ptr::read_unaligned(a as *const i16))) + __m16i(_mm_set1_epi16(core::ptr::read_unaligned(a as *const i16))) } #[inline] @@ -59,7 +59,7 @@ impl Vector for __m16i { #[inline] #[target_feature(enable = "avx2")] unsafe fn to_bitmask(a: Self) -> u32 { - std::mem::transmute(_mm_movemask_epi8(a.0) & 0x3) + core::mem::transmute(_mm_movemask_epi8(a.0) & 0x3) } } @@ -88,7 +88,7 @@ impl Vector for __m32i { #[inline] #[target_feature(enable = "avx2")] unsafe fn load(a: *const u8) -> Self { - __m32i(_mm_set1_epi32(std::ptr::read_unaligned(a as *const i32))) + __m32i(_mm_set1_epi32(core::ptr::read_unaligned(a as *const i32))) } #[inline] @@ -106,7 +106,7 @@ impl Vector for __m32i { #[inline] #[target_feature(enable = "avx2")] unsafe fn to_bitmask(a: Self) -> u32 { - std::mem::transmute(_mm_movemask_epi8(a.0) & 0xF) + core::mem::transmute(_mm_movemask_epi8(a.0) & 0xF) } } @@ -135,7 +135,7 @@ impl Vector for __m64i { #[inline] #[target_feature(enable = "avx2")] unsafe fn load(a: *const u8) -> Self { - __m64i(_mm_set1_epi64x(std::ptr::read_unaligned(a as *const i64))) + __m64i(_mm_set1_epi64x(core::ptr::read_unaligned(a as *const i64))) } #[inline] @@ -153,7 +153,7 @@ impl Vector for __m64i { #[inline] #[target_feature(enable = "avx2")] unsafe fn to_bitmask(a: Self) -> u32 { - std::mem::transmute(_mm_movemask_epi8(a.0) & 0xFF) + core::mem::transmute(_mm_movemask_epi8(a.0) & 0xFF) } } @@ -195,7 +195,7 @@ impl Vector for __m128i { #[inline] #[target_feature(enable = "avx2")] unsafe fn to_bitmask(a: Self) -> u32 { - std::mem::transmute(_mm_movemask_epi8(a)) + core::mem::transmute(_mm_movemask_epi8(a)) } } @@ -230,7 +230,7 @@ impl Vector for __m256i { #[inline] #[target_feature(enable = "avx2")] unsafe fn to_bitmask(a: Self) -> u32 { - std::mem::transmute(_mm256_movemask_epi8(a)) + core::mem::transmute(_mm256_movemask_epi8(a)) } } @@ -533,16 +533,17 @@ mod tests { #[test] #[should_panic] fn avx2_invalid_position() { - unsafe { Avx2Searcher::with_position(b"foo".to_vec().into_boxed_slice(), 3) }; + unsafe { Avx2Searcher::with_position(b"foo", 3) }; } #[test] #[should_panic] fn dynamic_avx2_invalid_position() { - unsafe { DynamicAvx2Searcher::with_position(b"foo".to_vec().into_boxed_slice(), 3) }; + unsafe { DynamicAvx2Searcher::with_position(b"foo", 3) }; } #[test] + #[cfg(feature = "alloc")] #[should_panic] fn avx2_empty_needle() { unsafe { Avx2Searcher::new(Box::new([])) }; @@ -567,22 +568,24 @@ mod tests { #[test] #[cfg(target_pointer_width = "64")] fn size_of_avx2_searcher() { - use std::mem::size_of; + use core::mem::size_of; assert_eq!(size_of::>(), 128); assert_eq!(size_of::>(), 128); assert_eq!(size_of::>(), 128); + #[cfg(feature = "alloc")] assert_eq!(size_of::>>(), 128); } #[test] #[cfg(target_pointer_width = "64")] fn size_of_dynamic_avx2_searcher() { - use std::mem::size_of; + use core::mem::size_of; assert_eq!(size_of::>(), 160); assert_eq!(size_of::>(), 160); assert_eq!(size_of::>(), 160); + #[cfg(feature = "alloc")] assert_eq!(size_of::>>(), 160); } diff --git a/tests/i386.rs b/tests/i386.rs index 9cb9380..393a649 100644 --- a/tests/i386.rs +++ b/tests/i386.rs @@ -18,7 +18,7 @@ fn search(haystack: &str, needle: &str) { cfg_if::cfg_if! { if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] { use sliceslice::x86::DynamicAvx2Searcher; - let searcher = unsafe { DynamicAvx2Searcher::new(needle.to_owned().into_boxed_slice()) }; + let searcher = unsafe { DynamicAvx2Searcher::new(needle) }; assert_eq!(unsafe { searcher.search_in(haystack) }, result); } else if #[cfg(target_arch = "wasm32")] { use sliceslice::wasm32::Wasm32Searcher;