sp_runtime/
type_with_default.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Provides a type that wraps another type and provides a default value.
19
20use crate::traits::{Bounded, One, Zero};
21use codec::{Compact, CompactAs, Decode, Encode, HasCompact, MaxEncodedLen};
22use core::{
23	fmt::Display,
24	marker::PhantomData,
25	ops::{
26		Add, AddAssign, BitAnd, BitOr, BitXor, Deref, Div, DivAssign, Mul, MulAssign, Not, Rem,
27		RemAssign, Shl, Shr, Sub, SubAssign,
28	},
29};
30use num_traits::{
31	CheckedAdd, CheckedDiv, CheckedMul, CheckedNeg, CheckedRem, CheckedShl, CheckedShr, CheckedSub,
32	Num, NumCast, PrimInt, Saturating, ToPrimitive,
33};
34use scale_info::TypeInfo;
35use sp_core::Get;
36
37#[cfg(feature = "serde")]
38use serde::{Deserialize, Serialize};
39
40/// A type that wraps another type and provides a default value.
41///
42/// Passes through arithmetical and many other operations to the inner value.
43#[derive(Encode, Decode, TypeInfo, Debug, MaxEncodedLen)]
44#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
45pub struct TypeWithDefault<T, D: Get<T>>(T, PhantomData<D>);
46
47impl<T, D: Get<T>> TypeWithDefault<T, D> {
48	fn new(value: T) -> Self {
49		Self(value, PhantomData)
50	}
51}
52
53impl<T: Clone, D: Get<T>> Clone for TypeWithDefault<T, D> {
54	fn clone(&self) -> Self {
55		Self(self.0.clone(), PhantomData)
56	}
57}
58
59impl<T: Copy, D: Get<T>> Copy for TypeWithDefault<T, D> {}
60
61impl<T: PartialEq, D: Get<T>> PartialEq for TypeWithDefault<T, D> {
62	fn eq(&self, other: &Self) -> bool {
63		self.0 == other.0
64	}
65}
66
67impl<T: Eq, D: Get<T>> Eq for TypeWithDefault<T, D> {}
68
69impl<T: PartialOrd, D: Get<T>> PartialOrd for TypeWithDefault<T, D> {
70	fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
71		self.0.partial_cmp(&other.0)
72	}
73}
74
75impl<T: Ord, D: Get<T>> Ord for TypeWithDefault<T, D> {
76	fn cmp(&self, other: &Self) -> core::cmp::Ordering {
77		self.0.cmp(&other.0)
78	}
79}
80
81impl<T, D: Get<T>> Deref for TypeWithDefault<T, D> {
82	type Target = T;
83	fn deref(&self) -> &Self::Target {
84		&self.0
85	}
86}
87
88impl<T, D: Get<T>> Default for TypeWithDefault<T, D> {
89	fn default() -> Self {
90		Self::new(D::get())
91	}
92}
93
94impl<T: From<u16>, D: Get<T>> From<u16> for TypeWithDefault<T, D> {
95	fn from(value: u16) -> Self {
96		Self::new(value.into())
97	}
98}
99
100impl<T: From<u32>, D: Get<T>> From<u32> for TypeWithDefault<T, D> {
101	fn from(value: u32) -> Self {
102		Self::new(value.into())
103	}
104}
105
106impl<T: From<u64>, D: Get<T>> From<u64> for TypeWithDefault<T, D> {
107	fn from(value: u64) -> Self {
108		Self::new(value.into())
109	}
110}
111
112impl<T: CheckedNeg, D: Get<T>> CheckedNeg for TypeWithDefault<T, D> {
113	fn checked_neg(&self) -> Option<Self> {
114		self.0.checked_neg().map(Self::new)
115	}
116}
117
118impl<T: CheckedRem, D: Get<T>> CheckedRem for TypeWithDefault<T, D> {
119	fn checked_rem(&self, rhs: &Self) -> Option<Self> {
120		self.0.checked_rem(&rhs.0).map(Self::new)
121	}
122}
123
124impl<T: CheckedShr, D: Get<T>> CheckedShr for TypeWithDefault<T, D> {
125	fn checked_shr(&self, n: u32) -> Option<Self> {
126		self.0.checked_shr(n).map(Self::new)
127	}
128}
129
130impl<T: CheckedShl, D: Get<T>> CheckedShl for TypeWithDefault<T, D> {
131	fn checked_shl(&self, n: u32) -> Option<Self> {
132		self.0.checked_shl(n).map(Self::new)
133	}
134}
135
136impl<T: Rem<Output = T>, D: Get<T>> Rem for TypeWithDefault<T, D> {
137	type Output = Self;
138	fn rem(self, rhs: Self) -> Self {
139		Self::new(self.0 % rhs.0)
140	}
141}
142
143impl<T: Rem<u32, Output = T>, D: Get<T>> Rem<u32> for TypeWithDefault<T, D> {
144	type Output = Self;
145	fn rem(self, rhs: u32) -> Self {
146		Self::new(self.0 % (rhs.into()))
147	}
148}
149
150impl<T: Shr<u32, Output = T>, D: Get<T>> Shr<u32> for TypeWithDefault<T, D> {
151	type Output = Self;
152	fn shr(self, rhs: u32) -> Self {
153		Self::new(self.0 >> rhs)
154	}
155}
156
157impl<T: Shr<usize, Output = T>, D: Get<T>> Shr<usize> for TypeWithDefault<T, D> {
158	type Output = Self;
159	fn shr(self, rhs: usize) -> Self {
160		Self::new(self.0 >> rhs)
161	}
162}
163
164impl<T: Shl<u32, Output = T>, D: Get<T>> Shl<u32> for TypeWithDefault<T, D> {
165	type Output = Self;
166	fn shl(self, rhs: u32) -> Self {
167		Self::new(self.0 << rhs)
168	}
169}
170
171impl<T: Shl<usize, Output = T>, D: Get<T>> Shl<usize> for TypeWithDefault<T, D> {
172	type Output = Self;
173	fn shl(self, rhs: usize) -> Self {
174		Self::new(self.0 << rhs)
175	}
176}
177
178impl<T: RemAssign, D: Get<T>> RemAssign for TypeWithDefault<T, D> {
179	fn rem_assign(&mut self, rhs: Self) {
180		self.0 %= rhs.0
181	}
182}
183
184impl<T: DivAssign, D: Get<T>> DivAssign for TypeWithDefault<T, D> {
185	fn div_assign(&mut self, rhs: Self) {
186		self.0 /= rhs.0
187	}
188}
189
190impl<T: MulAssign, D: Get<T>> MulAssign for TypeWithDefault<T, D> {
191	fn mul_assign(&mut self, rhs: Self) {
192		self.0 *= rhs.0
193	}
194}
195
196impl<T: SubAssign, D: Get<T>> SubAssign for TypeWithDefault<T, D> {
197	fn sub_assign(&mut self, rhs: Self) {
198		self.0 -= rhs.0
199	}
200}
201
202impl<T: AddAssign, D: Get<T>> AddAssign for TypeWithDefault<T, D> {
203	fn add_assign(&mut self, rhs: Self) {
204		self.0 += rhs.0
205	}
206}
207
208impl<T: From<u8>, D: Get<T>> From<u8> for TypeWithDefault<T, D> {
209	fn from(value: u8) -> Self {
210		Self::new(value.into())
211	}
212}
213
214impl<T: Display, D: Get<T>> Display for TypeWithDefault<T, D> {
215	fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
216		write!(f, "{}", self.0)
217	}
218}
219
220impl<T: TryFrom<u128>, D: Get<T>> TryFrom<u128> for TypeWithDefault<T, D> {
221	type Error = <T as TryFrom<u128>>::Error;
222	fn try_from(n: u128) -> Result<TypeWithDefault<T, D>, Self::Error> {
223		T::try_from(n).map(Self::new)
224	}
225}
226
227impl<T: TryFrom<usize>, D: Get<T>> TryFrom<usize> for TypeWithDefault<T, D> {
228	type Error = <T as TryFrom<usize>>::Error;
229	fn try_from(n: usize) -> Result<TypeWithDefault<T, D>, Self::Error> {
230		T::try_from(n).map(Self::new)
231	}
232}
233
234impl<T: TryInto<u8>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for u8 {
235	type Error = <T as TryInto<u8>>::Error;
236	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
237		value.0.try_into()
238	}
239}
240
241impl<T: TryInto<u16>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for u16 {
242	type Error = <T as TryInto<u16>>::Error;
243	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
244		value.0.try_into()
245	}
246}
247
248impl<T: TryInto<u32>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for u32 {
249	type Error = <T as TryInto<u32>>::Error;
250	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
251		value.0.try_into()
252	}
253}
254
255impl<T: TryInto<u64>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for u64 {
256	type Error = <T as TryInto<u64>>::Error;
257	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
258		value.0.try_into()
259	}
260}
261
262impl<T: TryInto<u128>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for u128 {
263	type Error = <T as TryInto<u128>>::Error;
264	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
265		value.0.try_into()
266	}
267}
268
269impl<T: TryInto<usize>, D: Get<T>> TryFrom<TypeWithDefault<T, D>> for usize {
270	type Error = <T as TryInto<usize>>::Error;
271	fn try_from(value: TypeWithDefault<T, D>) -> Result<Self, Self::Error> {
272		value.0.try_into()
273	}
274}
275
276impl<T: Zero + PartialEq, D: Get<T>> Zero for TypeWithDefault<T, D> {
277	fn zero() -> Self {
278		Self::new(T::zero())
279	}
280
281	fn is_zero(&self) -> bool {
282		self.0 == T::zero()
283	}
284}
285
286impl<T: Bounded, D: Get<T>> Bounded for TypeWithDefault<T, D> {
287	fn min_value() -> Self {
288		Self::new(T::min_value())
289	}
290
291	fn max_value() -> Self {
292		Self::new(T::max_value())
293	}
294}
295
296impl<T: PrimInt, D: Get<T>> PrimInt for TypeWithDefault<T, D> {
297	fn count_ones(self) -> u32 {
298		self.0.count_ones()
299	}
300
301	fn leading_zeros(self) -> u32 {
302		self.0.leading_zeros()
303	}
304
305	fn trailing_zeros(self) -> u32 {
306		self.0.trailing_zeros()
307	}
308
309	fn rotate_left(self, n: u32) -> Self {
310		Self::new(self.0.rotate_left(n))
311	}
312
313	fn rotate_right(self, n: u32) -> Self {
314		Self::new(self.0.rotate_right(n))
315	}
316
317	fn swap_bytes(self) -> Self {
318		Self::new(self.0.swap_bytes())
319	}
320
321	fn from_be(x: Self) -> Self {
322		Self::new(T::from_be(x.0))
323	}
324
325	fn from_le(x: Self) -> Self {
326		Self::new(T::from_le(x.0))
327	}
328
329	fn to_be(self) -> Self {
330		Self::new(self.0.to_be())
331	}
332
333	fn to_le(self) -> Self {
334		Self::new(self.0.to_le())
335	}
336
337	fn count_zeros(self) -> u32 {
338		self.0.count_zeros()
339	}
340
341	fn signed_shl(self, n: u32) -> Self {
342		Self::new(self.0.signed_shl(n))
343	}
344
345	fn signed_shr(self, n: u32) -> Self {
346		Self::new(self.0.signed_shr(n))
347	}
348
349	fn unsigned_shl(self, n: u32) -> Self {
350		Self::new(self.0.unsigned_shl(n))
351	}
352
353	fn unsigned_shr(self, n: u32) -> Self {
354		Self::new(self.0.unsigned_shr(n))
355	}
356
357	fn pow(self, exp: u32) -> Self {
358		Self::new(self.0.pow(exp))
359	}
360}
361
362impl<T: Saturating, D: Get<T>> Saturating for TypeWithDefault<T, D> {
363	fn saturating_add(self, rhs: Self) -> Self {
364		Self::new(self.0.saturating_add(rhs.0))
365	}
366
367	fn saturating_sub(self, rhs: Self) -> Self {
368		Self::new(self.0.saturating_sub(rhs.0))
369	}
370}
371
372impl<T: Div<Output = T>, D: Get<T>> Div for TypeWithDefault<T, D> {
373	type Output = Self;
374	fn div(self, rhs: Self) -> Self {
375		Self::new(self.0 / rhs.0)
376	}
377}
378
379impl<T: Mul<Output = T>, D: Get<T>> Mul for TypeWithDefault<T, D> {
380	type Output = Self;
381	fn mul(self, rhs: Self) -> Self {
382		Self::new(self.0 * rhs.0)
383	}
384}
385
386impl<T: CheckedDiv, D: Get<T>> CheckedDiv for TypeWithDefault<T, D> {
387	fn checked_div(&self, rhs: &Self) -> Option<Self> {
388		self.0.checked_div(&rhs.0).map(Self::new)
389	}
390}
391
392impl<T: CheckedMul, D: Get<T>> CheckedMul for TypeWithDefault<T, D> {
393	fn checked_mul(&self, rhs: &Self) -> Option<Self> {
394		self.0.checked_mul(&rhs.0).map(Self::new)
395	}
396}
397
398impl<T: Sub<Output = T>, D: Get<T>> Sub for TypeWithDefault<T, D> {
399	type Output = Self;
400	fn sub(self, rhs: Self) -> Self {
401		Self::new(self.0 - rhs.0)
402	}
403}
404
405impl<T: CheckedSub, D: Get<T>> CheckedSub for TypeWithDefault<T, D> {
406	fn checked_sub(&self, rhs: &Self) -> Option<Self> {
407		self.0.checked_sub(&rhs.0).map(Self::new)
408	}
409}
410
411impl<T: Add<Output = T>, D: Get<T>> Add for TypeWithDefault<T, D> {
412	type Output = Self;
413	fn add(self, rhs: Self) -> Self {
414		Self::new(self.0 + rhs.0)
415	}
416}
417
418impl<T: CheckedAdd, D: Get<T>> CheckedAdd for TypeWithDefault<T, D> {
419	fn checked_add(&self, rhs: &Self) -> Option<Self> {
420		self.0.checked_add(&rhs.0).map(Self::new)
421	}
422}
423
424impl<T: BitAnd<Output = T>, D: Get<T>> BitAnd for TypeWithDefault<T, D> {
425	type Output = Self;
426	fn bitand(self, rhs: Self) -> Self {
427		Self::new(self.0 & rhs.0)
428	}
429}
430
431impl<T: BitOr<Output = T>, D: Get<T>> BitOr for TypeWithDefault<T, D> {
432	type Output = Self;
433	fn bitor(self, rhs: Self) -> Self {
434		Self::new(self.0 | rhs.0)
435	}
436}
437
438impl<T: BitXor<Output = T>, D: Get<T>> BitXor for TypeWithDefault<T, D> {
439	type Output = Self;
440	fn bitxor(self, rhs: Self) -> Self {
441		Self::new(self.0 ^ rhs.0)
442	}
443}
444
445impl<T: One, D: Get<T>> One for TypeWithDefault<T, D> {
446	fn one() -> Self {
447		Self::new(T::one())
448	}
449}
450
451impl<T: Not<Output = T>, D: Get<T>> Not for TypeWithDefault<T, D> {
452	type Output = Self;
453	fn not(self) -> Self {
454		Self::new(self.0.not())
455	}
456}
457
458impl<T: NumCast, D: Get<T>> NumCast for TypeWithDefault<T, D> {
459	fn from<P: ToPrimitive>(n: P) -> Option<Self> {
460		<T as NumCast>::from(n).map_or(None, |n| Some(Self::new(n)))
461	}
462}
463
464impl<T: Num, D: Get<T>> Num for TypeWithDefault<T, D> {
465	type FromStrRadixErr = <T as Num>::FromStrRadixErr;
466
467	fn from_str_radix(s: &str, radix: u32) -> Result<Self, Self::FromStrRadixErr> {
468		T::from_str_radix(s, radix).map(Self::new)
469	}
470}
471
472impl<T: ToPrimitive, D: Get<T>> ToPrimitive for TypeWithDefault<T, D> {
473	fn to_i64(&self) -> Option<i64> {
474		self.0.to_i64()
475	}
476
477	fn to_u64(&self) -> Option<u64> {
478		self.0.to_u64()
479	}
480
481	fn to_i128(&self) -> Option<i128> {
482		self.0.to_i128()
483	}
484
485	fn to_u128(&self) -> Option<u128> {
486		self.0.to_u128()
487	}
488}
489
490impl<T, D: Get<T>> From<Compact<TypeWithDefault<T, D>>> for TypeWithDefault<T, D> {
491	fn from(c: Compact<TypeWithDefault<T, D>>) -> Self {
492		c.0
493	}
494}
495
496impl<T: HasCompact, D: Get<T>> CompactAs for TypeWithDefault<T, D> {
497	type As = T;
498
499	fn encode_as(&self) -> &Self::As {
500		&self.0
501	}
502
503	fn decode_from(val: Self::As) -> Result<Self, codec::Error> {
504		Ok(Self::new(val))
505	}
506}