pub trait MapRange: Sized {
// Required method
fn map_range(self, from: Range<Self>, to: Range<Self>) -> Self;
}
Expand description
Mapping a value from range from
to another range to
.
§Panics
Panics if from.end == from.start
,
if either range is empty with unsigned integers,
if from.start > self
with unsigned integers,
or if any checked overflowing happens.
§Examples
// Mapping from trig to f32 texture range
let initial_value = random::<f32>().sin();
let mapped = initial_value.map_range(-1.0..1.0, 0.0..1.0);
let expected = initial_value / 2.0 + 0.5;
assert!((mapped - expected).abs() <= f32::EPSILON);
// Mapping can happen outside of the ranges.
// Might panic with unsigned integers if `self - from.start`
// overflows or if either range is empty.
let x = 10_i32;
let y = x.map_range(0..5, -5..0);
let z = x.map_range(0..5, 0..-5);
assert_eq!(y, 5);
assert_eq!(z, -10);
ⓘ
// panics
let _ = 10_u32.map_range(0..5, 5..2);
ⓘ
// panics
let _ = 10_u32.map_range(20..30, 20..40);
ⓘ
// panics
let _ = 200_u8.map_range(0..10, 0..20);
Required Methods§
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.