Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions frame/support/src/traits/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,36 @@ impl<T: Ord> Contains<T> for All<T> {
fn contains(_: &T) -> bool { true }
}

/// Create a type which implements the `Contains` trait for a particular type with syntax similar
/// to `matches!`.
#[macro_export]
macro_rules! match_type {
( pub type $n:ident: impl Contains<$t:ty> = { $phead:pat $( | $ptail:pat )* } ; ) => {
pub struct $n;
impl $crate::traits::Contains<$t> for $n {
fn contains(l: &$t) -> bool {
matches!(l, $phead $( | $ptail )* )
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;

match_type! {
pub type OneOrTenToTwenty: impl Contains<u8> = { 1 | 10..=20 };
}

#[test]
fn match_type_works() {
for i in 0..=255 {
assert_eq!(OneOrTenToTwenty::contains(&i), i == 1 || i >= 10 && i <= 20);
}
}
}

/// A trait for a set which can enumerate its members in order.
pub trait SortedMembers<T: Ord> {
/// Get a vector of all members in the set, ordered.
Expand Down