diff --git a/crates/core_simd/src/masks.rs b/crates/core_simd/src/masks.rs index e8962b86b1185..dcec336cfaf2c 100644 --- a/crates/core_simd/src/masks.rs +++ b/crates/core_simd/src/masks.rs @@ -181,6 +181,13 @@ where self.0.to_int() } + /// Converts the mask to a mask of any other lane size. + #[inline] + #[must_use = "method returns a new mask and does not mutate the original value"] + pub fn cast(self) -> Mask { + Mask(self.0.convert()) + } + /// Tests the value of the specified lane. /// /// # Safety @@ -571,7 +578,7 @@ macro_rules! impl_from { LaneCount: SupportedLaneCount, { fn from(value: Mask<$from, LANES>) -> Self { - Self(value.0.convert()) + value.cast() } } )* diff --git a/crates/core_simd/tests/masks.rs b/crates/core_simd/tests/masks.rs index d10c6610f5051..3a0493d4ee664 100644 --- a/crates/core_simd/tests/masks.rs +++ b/crates/core_simd/tests/masks.rs @@ -99,6 +99,29 @@ macro_rules! test_mask_api { assert_eq!(bitmask, 0b01); assert_eq!(core_simd::Mask::<$type, 2>::from_bitmask(bitmask), mask); } + + #[test] + fn cast() { + fn cast_impl() + where + core_simd::Mask<$type, 8>: Into>, + { + let values = [true, false, false, true, false, false, true, false]; + let mask = core_simd::Mask::<$type, 8>::from_array(values); + + let cast_mask = mask.cast::(); + assert_eq!(values, cast_mask.to_array()); + + let into_mask: core_simd::Mask = mask.into(); + assert_eq!(values, into_mask.to_array()); + } + + cast_impl::(); + cast_impl::(); + cast_impl::(); + cast_impl::(); + cast_impl::(); + } } } }