@@ -28,6 +28,13 @@ pub trait Zero: Sized + Add<Self, Output = Self> {
28
28
fn is_zero ( & self ) -> bool ;
29
29
}
30
30
31
+ /// Defines an associated constant representing the additive identity element
32
+ /// for `Self`.
33
+ pub trait ConstZero : Zero {
34
+ /// The additive identity element of `Self`, `0`.
35
+ const ZERO : Self ;
36
+ }
37
+
31
38
macro_rules! zero_impl {
32
39
( $t: ty, $v: expr) => {
33
40
impl Zero for $t {
@@ -40,6 +47,10 @@ macro_rules! zero_impl {
40
47
* self == $v
41
48
}
42
49
}
50
+
51
+ impl ConstZero for $t {
52
+ const ZERO : Self = $v;
53
+ }
43
54
} ;
44
55
}
45
56
77
88
}
78
89
}
79
90
91
+ impl < T : ConstZero > ConstZero for Wrapping < T >
92
+ where
93
+ Wrapping < T > : Add < Output = Wrapping < T > > ,
94
+ {
95
+ const ZERO : Self = Wrapping ( T :: ZERO ) ;
96
+ }
97
+
80
98
/// Defines a multiplicative identity element for `Self`.
81
99
///
82
100
/// # Laws
@@ -115,6 +133,13 @@ pub trait One: Sized + Mul<Self, Output = Self> {
115
133
}
116
134
}
117
135
136
+ /// Defines an associated constant representing the multiplicative identity
137
+ /// element for `Self`.
138
+ pub trait ConstOne : One {
139
+ /// The multiplicative identity element of `Self`, `1`.
140
+ const ONE : Self ;
141
+ }
142
+
118
143
macro_rules! one_impl {
119
144
( $t: ty, $v: expr) => {
120
145
impl One for $t {
@@ -127,6 +152,10 @@ macro_rules! one_impl {
127
152
* self == $v
128
153
}
129
154
}
155
+
156
+ impl ConstOne for $t {
157
+ const ONE : Self = $v;
158
+ }
130
159
} ;
131
160
}
132
161
@@ -160,6 +189,13 @@ where
160
189
}
161
190
}
162
191
192
+ impl < T : ConstOne > ConstOne for Wrapping < T >
193
+ where
194
+ Wrapping < T > : Mul < Output = Wrapping < T > > ,
195
+ {
196
+ const ONE : Self = Wrapping ( T :: ONE ) ;
197
+ }
198
+
163
199
// Some helper functions provided for backwards compatibility.
164
200
165
201
/// Returns the additive identity, `0`.
0 commit comments