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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*!
# Dactyl: Saturated Unsigned Integer Conversion

The `SaturatingFrom` trait allows large primitives to be downcast into smaller
types with values capped at the smaller type's `::MAX` value, avoiding any
possible overflow or wrapping issues. It's a clamp, basically, except all uints
share the same bottom.

It is implemented for `u8`, `u16`, `u32`, and `u64` for all types larger than
said type, up to `u128`.

The `usize` type, being variable, works a little differently. It implements
`SaturatingFrom` on `u32`, `u64`, and `u128` regardless of the machine's bit
size, but its ceiling will vary based on the machine's bit size (it could be as
low as `u16::MAX` or as high as `u64::MAX`).

## Examples

```no_run
pub use dactyl::traits::SaturatingFrom;

assert_eq!(u8::saturating_from(1026_u16), 255_u8);
assert_eq!(u8::saturating_from(99_u16), 99_u8);
```
*/



/// # Helper: Generate Trait Implementations.
macro_rules! make_impls {
	($title:expr, $desc:expr, $from:ty, $to:ty, $MAX:literal) => {
		impl SaturatingFrom<$from> for $to {
			#[doc(inline)]
			#[doc = $title]
			///
			#[doc = $desc]
			fn saturating_from(src: $from) -> Self {
				if src >= $MAX { $MAX }
				else { src as Self }
			}
		}
	};

	($to:ty, $MAX:literal, $($from:ty),+) => {
		$(
			make_impls!(
				concat!("# Saturating From `", stringify!($from), "`"),
				concat!("This method will safely downcast any `", stringify!($from), "` into a `", stringify!($to), "`, capping the value to `", stringify!($to), "::MAX` to prevent overflow or wrapping."),
				$from,
				$to,
				$MAX
			);
		)+
	}
}



/// # Saturating From.
///
/// Convert an unsigned integer of a larger type into `Self`, capping the
/// maximum value to `Self::MAX` to prevent overflow or wrapping.
pub trait SaturatingFrom<T> {
	/// # Saturating From.
	fn saturating_from(src: T) -> Self;
}



// Most conversions work nice and neat.
make_impls!(u8, 255, u16, u32, u64, u128, usize);
make_impls!(u16, 65_535, u32, u64, u128, usize);
make_impls!(u32, 4_294_967_295, u64, u128); // from<usize> is manual, below.
make_impls!(u64, 18_446_744_073_709_551_615, u128); // from<usize> is manual, below.



impl SaturatingFrom<usize> for u32 {
	#[cfg(target_pointer_width = "64")]
	#[inline]
	/// # Saturating From `usize`
	fn saturating_from(src: usize) -> Self {
		// 64-bit pointers have to be saturated down.
		if src >= 4_294_967_295 { 4_294_967_295 }
		else { src as Self }
	}

	#[cfg(not(target_pointer_width = "64"))]
	#[inline]
	/// # Saturating From `usize`
	fn saturating_from(src: usize) -> Self { src as Self }
}

impl SaturatingFrom<usize> for u64 {
	#[inline]
	/// # Saturating From `usize`
	fn saturating_from(src: usize) -> Self { src as Self }
}



// The conversion traits for `u32`, `u64`, and `u128` have to be handled
// manually to account for `usize`'s variable width.

impl SaturatingFrom<u32> for usize {
	#[cfg(target_pointer_width = "16")]
	#[inline]
	/// # Saturating From `u32`
	fn saturating_from(src: u32) -> Self {
		// 16-bit pointers have to be saturated down.
		if src >= 65_535 { 65_535 }
		else { src as Self }
	}

	#[cfg(not(target_pointer_width = "16"))]
	#[inline]
	/// # Saturating From `u32`
	fn saturating_from(src: u32) -> Self { src as Self }
}

impl SaturatingFrom<u64> for usize {
	#[cfg(target_pointer_width = "16")]
	#[inline]
	/// # Saturating From `u64`
	fn saturating_from(src: u64) -> Self {
		// 16-bit pointers have to be saturated down.
		if src >= 65_535 { 65_535 }
		else { src as Self }
	}

	#[cfg(target_pointer_width = "32")]
	#[inline]
	/// # Saturating From `u64`
	fn saturating_from(src: u64) -> Self {
		// 32-bit pointers have to be saturated down.
		if src >= 4_294_967_295 { 4_294_967_295 }
		else { src as Self }
	}

	#[cfg(target_pointer_width = "64")]
	#[inline]
	/// # Saturating From `u64`
	fn saturating_from(src: u64) -> Self { src as Self }
}

impl SaturatingFrom<u128> for usize {
	#[cfg(target_pointer_width = "16")]
	#[inline]
	/// # Saturating From `u128`
	fn saturating_from(src: u128) -> Self {
		// 16-bit pointers have to be saturated down.
		if src >= 65_535 { 65_535 }
		else { src as Self }
	}

	#[cfg(target_pointer_width = "32")]
	#[inline]
	/// # Saturating From `u128`
	fn saturating_from(src: u128) -> Self {
		// 32-bit pointers have to be saturated down.
		if src >= 4_294_967_295 { 4_294_967_295 }
		else { src as Self }
	}

	#[cfg(target_pointer_width = "64")]
	#[inline]
	/// # Saturating From `u128`
	fn saturating_from(src: u128) -> Self {
		// 64-bit pointers have to be saturated down.
		if src >= 18_446_744_073_709_551_615 { 18_446_744_073_709_551_615 }
		else { src as Self }
	}
}



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

	#[test]
	fn t_u8_from() {
		for (i, t) in (0..=u16::from(u8::MAX)).zip(0..=u8::MAX) {
			assert_eq!(u8::saturating_from(i), t);
		}
		for (i, t) in (0..=u32::from(u8::MAX)).zip(0..=u8::MAX) {
			assert_eq!(u8::saturating_from(i), t);
		}
		for (i, t) in (0..=u64::from(u8::MAX)).zip(0..=u8::MAX) {
			assert_eq!(u8::saturating_from(i), t);
		}
		for (i, t) in (0..=u128::from(u8::MAX)).zip(0..=u8::MAX) {
			assert_eq!(u8::saturating_from(i), t);
		}
		for (i, t) in (0..=usize::from(u8::MAX)).zip(0..=u8::MAX) {
			assert_eq!(u8::saturating_from(i), t);
		}

		assert_eq!(u8::saturating_from(u16::MAX), u8::MAX);
		assert_eq!(u8::saturating_from(u32::MAX), u8::MAX);
		assert_eq!(u8::saturating_from(u64::MAX), u8::MAX);
		assert_eq!(u8::saturating_from(u128::MAX), u8::MAX);
		assert_eq!(u8::saturating_from(usize::MAX), u8::MAX);
	}

	#[test]
	fn t_u16_from() {
		for (i, t) in (0..=u32::from(u16::MAX)).zip(0..=u16::MAX) {
			assert_eq!(u16::saturating_from(i), t);
		}
		for (i, t) in (0..=u64::from(u16::MAX)).zip(0..=u16::MAX) {
			assert_eq!(u16::saturating_from(i), t);
		}
		for (i, t) in (0..=u128::from(u16::MAX)).zip(0..=u16::MAX) {
			assert_eq!(u16::saturating_from(i), t);
		}
		for (i, t) in (0..=usize::from(u16::MAX)).zip(0..=u16::MAX) {
			assert_eq!(u16::saturating_from(i), t);
		}

		assert_eq!(u16::saturating_from(u32::MAX), u16::MAX);
		assert_eq!(u16::saturating_from(u64::MAX), u16::MAX);
		assert_eq!(u16::saturating_from(u128::MAX), u16::MAX);
		assert_eq!(u16::saturating_from(usize::MAX), u16::MAX);
	}

	#[test]
	#[ignore] // This takes a very long time to run.
	fn t_u32_from() {
		for (i, t) in (0..=u64::from(u32::MAX)).zip(0..=u32::MAX) {
			assert_eq!(u32::saturating_from(i), t);
			assert_eq!(u32::saturating_from(u128::from(i)), t);
		}

		assert_eq!(u32::saturating_from(u64::MAX), u32::MAX);
		assert_eq!(u32::saturating_from(u128::MAX), u32::MAX);
		assert!(u32::saturating_from(usize::MAX) <= u32::MAX);
	}

	#[test]
	fn t_usize_from() {
		assert!(usize::saturating_from(u32::MAX) <= usize::MAX);
		assert!(usize::saturating_from(u64::MAX) <= usize::MAX);
		assert_eq!(usize::saturating_from(u128::MAX), usize::MAX);
	}
}