pub struct Transform { /* private fields */ }Expand description
A small helper struct which represents a 1 dimensional affine transformation from a signed 64 bit space A, to a signed 64 bit space B. Conceptually, this is the function…
f(a) = b = (a * scale) + offset
Internally, however, the exact function used is
f(a) = b = (((a - A_offset) * B_scale) / A_scale) + B_offset
Where the offsets involved are 64 bit signed integers, and the scale factors are 32 bit unsigned integers.
Overflow/Underflow saturation behavior is as follows. The transformation operation is divided into three stages.
- Offset by A_offset
- Scale by (B_scale / A_scale)
- Offset by B_offset
Each stage is saturated independently. That is to say, if the result of stage #1 is clamped at int64::min, this is the input value which will be fed into stage #2. The calculations are not done with infinite precision and then clamped at the end.
TODO(johngro): Reconsider this. Clamping at intermediate stages can make it more difficult to understand that saturation happened at all, and might be important to a client. It may be better to either signal explicitly that this happened, or to extend the precision of the operation in the rare slow path so that saturation behavior occurs only at the end of the op, and produces a correct result if the transform would have saturated at an intermediate step, but got brought back into range by a subsequent operation.
Saturation is enabled by default, but may be disabled by choosing the Saturate::No form of Apply/ApplyInverse. When saturation behavior is disabled, the results of a transformation where over/underflow occurs at any stage is undefined.
Implementations§
Source§impl Transform
impl Transform
Sourcepub const fn const_default() -> Self
pub const fn const_default() -> Self
Returns the identity Transform (f(a) = a).
Sourcepub const fn new(a_offset: i64, b_offset: i64, ratio: Ratio) -> Self
pub const fn new(a_offset: i64, b_offset: i64, ratio: Ratio) -> Self
Constructs a new Transform.
pub const fn new_linear(ratio: Ratio) -> Self
pub const fn invertible(&self) -> bool
pub const fn a_offset(&self) -> i64
pub const fn b_offset(&self) -> i64
pub const fn ratio(&self) -> Ratio
pub const fn numerator(&self) -> u32
pub const fn denominator(&self) -> u32
pub const fn inverse(&self) -> Self
pub fn apply_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64
pub fn apply_inverse_static<const SATURATE: bool>( a_offset: i64, b_offset: i64, ratio: Ratio, val: i64, ) -> i64
pub fn apply<const SATURATE: bool>(&self, val: i64) -> i64
pub fn apply_inverse<const SATURATE: bool>(&self, val: i64) -> i64
pub fn compose(bc: &Transform, ab: &Transform, exact: Exact) -> Transform
Trait Implementations§
impl Copy for Transform
impl Eq for Transform
Source§impl FromBytes for Transform
impl FromBytes for Transform
Source§fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>where
Self: Sized,
fn read_from_bytes(source: &[u8]) -> Result<Self, SizeError<&[u8], Self>>where
Self: Sized,
Source§fn read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>where
Self: Sized,
fn read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), SizeError<&[u8], Self>>where
Self: Sized,
Source§fn read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), SizeError<&[u8], Self>>where
Self: Sized,
fn read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), SizeError<&[u8], Self>>where
Self: Sized,
impl Immutable for Transform
Source§impl IntoBytes for Transform
impl IntoBytes for Transform
Source§fn as_mut_bytes(&mut self) -> &mut [u8] ⓘwhere
Self: FromBytes,
fn as_mut_bytes(&mut self) -> &mut [u8] ⓘwhere
Self: FromBytes,
Source§fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>where
Self: Immutable,
fn write_to(&self, dst: &mut [u8]) -> Result<(), SizeError<&Self, &mut [u8]>>where
Self: Immutable,
Source§fn write_to_prefix(
&self,
dst: &mut [u8],
) -> Result<(), SizeError<&Self, &mut [u8]>>where
Self: Immutable,
fn write_to_prefix(
&self,
dst: &mut [u8],
) -> Result<(), SizeError<&Self, &mut [u8]>>where
Self: Immutable,
Source§impl Mul for Transform
Composes two timeline functions B->C and A->B producing A->C.
impl Mul for Transform
Composes two timeline functions B->C and A->B producing A->C.
Panics on loss of precision.
impl StructuralPartialEq for Transform
Source§impl TryFromBytes for Transform
impl TryFromBytes for Transform
Source§fn try_read_from_bytes(
source: &[u8],
) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_bytes(
source: &[u8],
) -> Result<Self, ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Source§fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_prefix(
source: &[u8],
) -> Result<(Self, &[u8]), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Source§fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
fn try_read_from_suffix(
source: &[u8],
) -> Result<(&[u8], Self), ConvertError<!, SizeError<&[u8], Self>, ValidityError<&[u8], Self>>>where
Self: Sized,
Auto Trait Implementations§
impl Freeze for Transform
impl RefUnwindSafe for Transform
impl Send for Transform
impl Sync for Transform
impl Unpin for Transform
impl UnsafeUnpin for Transform
impl UnwindSafe for Transform
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere
T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere
T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DTwhere
ST: ?Sized,
DT: ?Sized,
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DTwhere
ST: ?Sized,
DT: ?Sized,
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)