Skip to content

Commit 7c2d295

Browse files
calebzulawskiworkingjubilee
authored andcommitted
Hide mask impl details in sealed trait.
1 parent ab8eec7 commit 7c2d295

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

crates/core_simd/src/masks.rs

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,36 @@ use crate::simd::{LaneCount, Simd, SimdElement, SupportedLaneCount};
1616
use core::cmp::Ordering;
1717
use core::fmt;
1818

19-
/// Marker trait for types that may be used as SIMD mask elements.
20-
pub unsafe trait MaskElement: SimdElement {
21-
#[doc(hidden)]
22-
fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
23-
where
24-
LaneCount<LANES>: SupportedLaneCount;
19+
mod sealed {
20+
use super::*;
21+
22+
/// Not only does this seal the `MaskElement` trait, but these functions prevent other traits
23+
/// from bleeding into the parent bounds.
24+
///
25+
/// For example, `eq` could be provided by requiring `MaskElement: PartialEq`, but that would
26+
/// prevent us from ever removing that bound, or from implementing `MaskElement` on
27+
/// non-`PartialEq` types in the future.
28+
pub trait Sealed {
29+
fn valid<const LANES: usize>(values: Simd<Self, LANES>) -> bool
30+
where
31+
LaneCount<LANES>: SupportedLaneCount,
32+
Self: SimdElement;
2533

26-
#[doc(hidden)]
27-
fn eq(self, other: Self) -> bool;
34+
fn eq(self, other: Self) -> bool;
2835

29-
#[doc(hidden)]
30-
const TRUE: Self;
36+
const TRUE: Self;
3137

32-
#[doc(hidden)]
33-
const FALSE: Self;
38+
const FALSE: Self;
39+
}
3440
}
41+
use sealed::Sealed;
42+
43+
/// Marker trait for types that may be used as SIMD mask elements.
44+
pub unsafe trait MaskElement: SimdElement + Sealed {}
3545

3646
macro_rules! impl_element {
3747
{ $ty:ty } => {
38-
unsafe impl MaskElement for $ty {
48+
impl Sealed for $ty {
3949
fn valid<const LANES: usize>(value: Simd<Self, LANES>) -> bool
4050
where
4151
LaneCount<LANES>: SupportedLaneCount,
@@ -48,6 +58,8 @@ macro_rules! impl_element {
4858
const TRUE: Self = -1;
4959
const FALSE: Self = 0;
5060
}
61+
62+
unsafe impl MaskElement for $ty {}
5163
}
5264
}
5365

0 commit comments

Comments
 (0)