Skip to content

Commit f2378d7

Browse files
committed
Implement From trait for convenient convertion between VectorHashes
1 parent a0b63e3 commit f2378d7

File tree

2 files changed

+31
-30
lines changed

2 files changed

+31
-30
lines changed

src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,16 @@ impl<V: Vector> VectorHash<V> {
182182
}
183183
}
184184

185+
impl<T: Vector, V: Vector + From<T>> From<&VectorHash<T>> for VectorHash<V> {
186+
#[inline]
187+
fn from(hash: &VectorHash<T>) -> Self {
188+
Self {
189+
first: V::from(hash.first),
190+
last: V::from(hash.last),
191+
}
192+
}
193+
}
194+
185195
#[cfg(test)]
186196
mod tests {
187197
use super::{MemchrSearcher, Needle};

src/x86.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ impl Vector for __m16i {
4646
}
4747
}
4848

49+
impl From<__m128i> for __m16i {
50+
#[inline]
51+
fn from(vector: __m128i) -> Self {
52+
Self(vector)
53+
}
54+
}
55+
4956
#[derive(Clone, Copy)]
5057
#[repr(transparent)]
5158
#[allow(non_camel_case_types)]
@@ -85,6 +92,13 @@ impl Vector for __m32i {
8592
}
8693
}
8794

95+
impl From<__m128i> for __m32i {
96+
#[inline]
97+
fn from(vector: __m128i) -> Self {
98+
Self(vector)
99+
}
100+
}
101+
88102
#[derive(Clone, Copy)]
89103
#[repr(transparent)]
90104
#[allow(non_camel_case_types)]
@@ -124,6 +138,13 @@ impl Vector for __m64i {
124138
}
125139
}
126140

141+
impl From<__m128i> for __m64i {
142+
#[inline]
143+
fn from(vector: __m128i) -> Self {
144+
Self(vector)
145+
}
146+
}
147+
127148
impl Vector for __m128i {
128149
const LANES: usize = 16;
129150

@@ -192,36 +213,6 @@ impl Vector for __m256i {
192213
}
193214
}
194215

195-
impl From<&VectorHash<__m128i>> for VectorHash<__m64i> {
196-
#[inline]
197-
fn from(hash: &VectorHash<__m128i>) -> Self {
198-
Self {
199-
first: __m64i(hash.first),
200-
last: __m64i(hash.last),
201-
}
202-
}
203-
}
204-
205-
impl From<&VectorHash<__m128i>> for VectorHash<__m32i> {
206-
#[inline]
207-
fn from(hash: &VectorHash<__m128i>) -> Self {
208-
Self {
209-
first: __m32i(hash.first),
210-
last: __m32i(hash.last),
211-
}
212-
}
213-
}
214-
215-
impl From<&VectorHash<__m128i>> for VectorHash<__m16i> {
216-
#[inline]
217-
fn from(hash: &VectorHash<__m128i>) -> Self {
218-
Self {
219-
first: __m16i(hash.first),
220-
last: __m16i(hash.last),
221-
}
222-
}
223-
}
224-
225216
/// Single-substring searcher using an AVX2 algorithm based on the "Generic
226217
/// SIMD" algorithm [presented by Wojciech
227218
/// Muła](http://0x80.pl/articles/simd-strfind.html).

0 commit comments

Comments
 (0)