1use crate::marker::{Freeze, PointeeSized, Unsize};
4use crate::ops::{CoerceUnsized, DispatchFromDyn};
5
6#[allow_internal_unstable(internal_syntax)]
11#[unstable(feature = "pattern_types", issue = "123646")]
12#[diagnostic::opaque]
13pub macro pattern_type($( $arg:tt )*) {
14 k#pattern_type($( $arg )*)
15}
16
17#[unstable(feature = "pattern_type_range_trait", issue = "123646")]
21#[rustc_const_unstable(feature = "pattern_type_range_trait", issue = "123646")]
22#[diagnostic::on_unimplemented(
23 message = "`{Self}` is not a valid base type for range patterns",
24 label = "only integer types and `char` are supported"
25)]
26pub const trait RangePattern {
27 #[lang = "RangeMin"]
29 const MIN: Self;
30
31 #[lang = "RangeMax"]
33 const MAX: Self;
34
35 #[lang = "RangeSub"]
37 #[track_caller]
38 fn sub_one(self) -> Self;
39}
40
41macro_rules! impl_range_pat {
42 ($($ty:ty,)*) => {
43 $(
44 #[rustc_const_unstable(feature = "pattern_type_range_trait", issue = "123646")]
45 const impl RangePattern for $ty {
46 const MIN: $ty = <$ty>::MIN;
47 const MAX: $ty = <$ty>::MAX;
48 fn sub_one(self) -> Self {
49 match self.checked_sub(1) {
50 Some(val) => val,
51 None => panic!("exclusive range end at minimum value of type")
52 }
53 }
54 }
55 )*
56 }
57}
58
59#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for i8 {
const MIN: i8 = <i8>::MIN;
const MAX: i8 = <i8>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for i16 {
const MIN: i16 = <i16>::MIN;
const MAX: i16 = <i16>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for i32 {
const MIN: i32 = <i32>::MIN;
const MAX: i32 = <i32>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for i64 {
const MIN: i64 = <i64>::MIN;
const MAX: i64 = <i64>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for i128 {
const MIN: i128 = <i128>::MIN;
const MAX: i128 = <i128>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for isize {
const MIN: isize = <isize>::MIN;
const MAX: isize = <isize>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for u8 {
const MIN: u8 = <u8>::MIN;
const MAX: u8 = <u8>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for u16 {
const MIN: u16 = <u16>::MIN;
const MAX: u16 = <u16>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for u32 {
const MIN: u32 = <u32>::MIN;
const MAX: u32 = <u32>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for u64 {
const MIN: u64 = <u64>::MIN;
const MAX: u64 = <u64>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for u128 {
const MIN: u128 = <u128>::MIN;
const MAX: u128 = <u128>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}
#[rustc_const_unstable(feature = "pattern_type_range_trait", issue =
"123646")]
const impl RangePattern for usize {
const MIN: usize = <usize>::MIN;
const MAX: usize = <usize>::MAX;
fn sub_one(self) -> Self {
match self.checked_sub(1) {
Some(val) => val,
None => {
crate::panicking::panic_fmt(format_args!("exclusive range end at minimum value of type"));
}
}
}
}impl_range_pat! {
60 i8, i16, i32, i64, i128, isize,
61 u8, u16, u32, u64, u128, usize,
62}
63
64#[rustc_const_unstable(feature = "pattern_type_range_trait", issue = "123646")]
65const impl RangePattern for char {
66 const MIN: Self = char::MIN;
67
68 const MAX: Self = char::MAX;
69
70 fn sub_one(self) -> Self {
71 match char::from_u32(self as u32 - 1) {
72 None => {
crate::panicking::panic_fmt(format_args!("exclusive range to start of valid chars"));
}panic!("exclusive range to start of valid chars"),
73 Some(val) => val,
74 }
75 }
76}
77
78impl<T: PointeeSized, U: PointeeSized> CoerceUnsized<k#pattern_type(*const U is !null)pattern_type!(*const U is !null)> for k#pattern_type(*const T is !null)pattern_type!(*const T is !null) where
79 T: Unsize<U>
80{
81}
82
83impl<T: DispatchFromDyn<U>, U> DispatchFromDyn<k#pattern_type(U is !null)pattern_type!(U is !null)> for k#pattern_type(T is !null)pattern_type!(T is !null) {}
84
85impl<T: PointeeSized> Unpin for k#pattern_type(*const T is !null)pattern_type!(*const T is !null) {}
86
87unsafe impl<T: PointeeSized> Freeze for k#pattern_type(*const T is !null)pattern_type!(*const T is !null) {}
88
89unsafe impl<T: PointeeSized> Freeze for k#pattern_type(*mut T is !null)pattern_type!(*mut T is !null) {}