Skip to content

Commit 939914e

Browse files
Merge pull request #251 from rust-lang/mask-cast
Add Mask::cast
2 parents af53b5d + c9f4e0e commit 939914e

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

crates/core_simd/src/masks.rs

+8-1
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,13 @@ where
181181
self.0.to_int()
182182
}
183183

184+
/// Converts the mask to a mask of any other lane size.
185+
#[inline]
186+
#[must_use = "method returns a new mask and does not mutate the original value"]
187+
pub fn cast<U: MaskElement>(self) -> Mask<U, LANES> {
188+
Mask(self.0.convert())
189+
}
190+
184191
/// Tests the value of the specified lane.
185192
///
186193
/// # Safety
@@ -571,7 +578,7 @@ macro_rules! impl_from {
571578
LaneCount<LANES>: SupportedLaneCount,
572579
{
573580
fn from(value: Mask<$from, LANES>) -> Self {
574-
Self(value.0.convert())
581+
value.cast()
575582
}
576583
}
577584
)*

crates/core_simd/tests/masks.rs

+23
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,29 @@ macro_rules! test_mask_api {
9999
assert_eq!(bitmask, 0b01);
100100
assert_eq!(core_simd::Mask::<$type, 2>::from_bitmask(bitmask), mask);
101101
}
102+
103+
#[test]
104+
fn cast() {
105+
fn cast_impl<T: core_simd::MaskElement>()
106+
where
107+
core_simd::Mask<$type, 8>: Into<core_simd::Mask<T, 8>>,
108+
{
109+
let values = [true, false, false, true, false, false, true, false];
110+
let mask = core_simd::Mask::<$type, 8>::from_array(values);
111+
112+
let cast_mask = mask.cast::<T>();
113+
assert_eq!(values, cast_mask.to_array());
114+
115+
let into_mask: core_simd::Mask<T, 8> = mask.into();
116+
assert_eq!(values, into_mask.to_array());
117+
}
118+
119+
cast_impl::<i8>();
120+
cast_impl::<i16>();
121+
cast_impl::<i32>();
122+
cast_impl::<i64>();
123+
cast_impl::<isize>();
124+
}
102125
}
103126
}
104127
}

0 commit comments

Comments
 (0)