-
Notifications
You must be signed in to change notification settings - Fork 8
/
natural.rs
145 lines (119 loc) · 3.96 KB
/
natural.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use std::num::NonZeroUsize;
use crate::token::variance::bound::{
Bounded, BoundedVariantRange, Boundedness, Unbounded, VariantRange,
};
use crate::token::variance::invariant::{GlobVariance, Identity, Invariant};
use crate::token::variance::ops::{self, Conjunction, Disjunction, Product};
use crate::token::variance::Variance;
macro_rules! impl_invariant_natural {
($name:ident $(,)?) => {
impl_invariant_natural!($name, once => 0);
};
($name:ident, once => $once:expr $(,)?) => {
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct $name(usize);
impl $name {
pub const ZERO: Self = $name(0);
pub const ONE: Self = $name(1);
pub const fn new(n: usize) -> Self {
$name(n)
}
pub fn map<F>(self, f: F) -> Self
where
F: FnOnce(usize) -> usize,
{
$name(f(self.0))
}
pub fn zip_map<F>(self, rhs: Self, f: F) -> Self
where
F: FnOnce(usize, usize) -> usize,
{
$name(f(self.0, rhs.0))
}
}
impl AsRef<usize> for $name {
fn as_ref(&self) -> &usize {
&self.0
}
}
impl Conjunction for $name {
type Output = Self;
fn conjunction(self, rhs: Self) -> Self::Output {
self.zip_map(rhs, ops::conjunction)
}
}
impl From<usize> for $name {
fn from(n: usize) -> $name {
$name(n)
}
}
impl From<$name> for usize {
fn from(size: $name) -> usize {
size.0
}
}
impl Identity for $name {
fn zero() -> Self {
$name(0)
}
}
impl Invariant for $name {
type Bound = BoundedVariantRange;
fn once() -> Self {
$name($once)
}
fn bound(lhs: Self, rhs: Self) -> Boundedness<Self::Bound> {
let [lower, upper] = crate::minmax(lhs, rhs);
BoundedVariantRange::try_from_lower_and_upper(lower.0, upper.0)
.map_or(Unbounded, Bounded)
}
fn into_lower_bound(self) -> Boundedness<Self::Bound> {
NonZeroUsize::new(self.0)
.map(BoundedVariantRange::Lower)
.map(From::from)
.unwrap_or(Unbounded)
}
}
impl PartialEq<usize> for $name {
fn eq(&self, rhs: &usize) -> bool {
self.0 == *rhs
}
}
impl Product<usize> for $name {
type Output = Self;
fn product(self, rhs: usize) -> Self::Output {
self.map(|lhs| ops::product(lhs, rhs))
}
}
impl Product<VariantRange> for $name {
type Output = GlobVariance<Self>;
fn product(self, rhs: VariantRange) -> Self::Output {
NonZeroUsize::new(self.0)
.map_or_else(
GlobVariance::<Self>::zero,
|lhs| Variance::Variant(rhs.map_bounded(|rhs| ops::product(rhs, lhs))),
)
}
}
impl Conjunction<$name> for BoundedVariantRange {
type Output = Self;
fn conjunction(self, rhs: $name) -> Self::Output {
self.translation(rhs.0)
}
}
impl Disjunction<$name> for BoundedVariantRange {
type Output = VariantRange;
fn disjunction(self, rhs: $name) -> Self::Output {
self.union(rhs.0)
}
}
};
}
impl_invariant_natural!(Depth, once => 1);
impl_invariant_natural!(Size);
impl GlobVariance<Depth> {
pub fn is_exhaustive(&self) -> bool {
!self.has_upper_bound()
}
}