euclid

Struct Length

Source
#[repr(C)]
pub struct Length<T, Unit>(pub T, _);
Expand description

A one-dimensional distance, with value represented by T and unit of measurement Unit.

T can be any numeric type, for example a primitive type like u64 or f32.

Unit is not used in the representation of a Length value. It is used only at compile time to ensure that a Length stored with one unit is converted explicitly before being used in an expression that requires a different unit. It may be a type without values, such as an empty enum.

You can multiply a Length by a scale::Scale to convert it from one unit to another. See the Scale docs for an example.

Tuple Fields§

§0: T

Implementations§

Source§

impl<T, U> Length<T, U>

Source

pub const fn new(x: T) -> Self

Associate a value with a unit of measure.

Source§

impl<T: Clone, U> Length<T, U>

Source

pub fn get(self) -> T

Unpack the underlying value from the wrapper.

Source

pub fn cast_unit<V>(self) -> Length<T, V>

Cast the unit

Source

pub fn lerp(self, other: Self, t: T) -> Self
where T: One + Sub<Output = T> + Mul<Output = T> + Add<Output = T>,

Linearly interpolate between this length and another length.

§Example
use euclid::default::Length;

let from = Length::new(0.0);
let to = Length::new(8.0);

assert_eq!(from.lerp(to, -1.0), Length::new(-8.0));
assert_eq!(from.lerp(to,  0.0), Length::new( 0.0));
assert_eq!(from.lerp(to,  0.5), Length::new( 4.0));
assert_eq!(from.lerp(to,  1.0), Length::new( 8.0));
assert_eq!(from.lerp(to,  2.0), Length::new(16.0));
Source§

impl<T: PartialOrd, U> Length<T, U>

Source

pub fn min(self, other: Self) -> Self

Returns minimum between this length and another length.

Source

pub fn max(self, other: Self) -> Self

Returns maximum between this length and another length.

Source§

impl<T: NumCast + Clone, U> Length<T, U>

Source

pub fn cast<NewT: NumCast>(self) -> Length<NewT, U>

Cast from one numeric representation to another, preserving the units.

Source

pub fn try_cast<NewT: NumCast>(self) -> Option<Length<NewT, U>>

Fallible cast from one numeric representation to another, preserving the units.

Trait Implementations§

Source§

impl<T: Add, U> Add for Length<T, U>

Source§

type Output = Length<<T as Add>::Output, U>

The resulting type after applying the + operator.
Source§

fn add(self, other: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl<T: AddAssign, U> AddAssign for Length<T, U>

Source§

fn add_assign(&mut self, other: Self)

Performs the += operation. Read more
Source§

impl<U, T: ApproxEq<T>> ApproxEq<T> for Length<T, U>

Source§

fn approx_epsilon() -> T

Default epsilon value
Source§

fn approx_eq_eps(&self, other: &Length<T, U>, approx_epsilon: &T) -> bool

Returns true is this object is approximately equal to the other one, using a provided epsilon value.
Source§

fn approx_eq(&self, other: &Self) -> bool

Returns true is this object is approximately equal to the other one, using the approx_epsilon() epsilon value.
Source§

impl<T: Clone, U> Clone for Length<T, U>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug, U> Debug for Length<T, U>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<T: Default, U> Default for Length<T, U>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de, T, U> Deserialize<'de> for Length<T, U>
where T: Deserialize<'de>,

Source§

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl<Src, Dst, T: Div> Div<Length<T, Src>> for Length<T, Dst>

Source§

type Output = Scale<<T as Div>::Output, Src, Dst>

The resulting type after applying the / operator.
Source§

fn div(self, other: Length<T, Src>) -> Self::Output

Performs the / operation. Read more
Source§

impl<Src, Dst, T: Div> Div<Scale<T, Src, Dst>> for Length<T, Dst>

Source§

type Output = Length<<T as Div>::Output, Src>

The resulting type after applying the / operator.
Source§

fn div(self, scale: Scale<T, Src, Dst>) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Div, U> Div<T> for Length<T, U>

Source§

type Output = Length<<T as Div>::Output, U>

The resulting type after applying the / operator.
Source§

fn div(self, scale: T) -> Self::Output

Performs the / operation. Read more
Source§

impl<T: Copy + Div<T, Output = T>, U> DivAssign<T> for Length<T, U>

Source§

fn div_assign(&mut self, scale: T)

Performs the /= operation. Read more
Source§

impl<T: Hash, U> Hash for Length<T, U>

Source§

fn hash<H: Hasher>(&self, h: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<Src, Dst, T: Mul> Mul<Scale<T, Src, Dst>> for Length<T, Src>

Source§

type Output = Length<<T as Mul>::Output, Dst>

The resulting type after applying the * operator.
Source§

fn mul(self, scale: Scale<T, Src, Dst>) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Mul, U> Mul<T> for Length<T, U>

Source§

type Output = Length<<T as Mul>::Output, U>

The resulting type after applying the * operator.
Source§

fn mul(self, scale: T) -> Self::Output

Performs the * operation. Read more
Source§

impl<T: Copy + Mul<T, Output = T>, U> MulAssign<T> for Length<T, U>

Source§

fn mul_assign(&mut self, scale: T)

Performs the *= operation. Read more
Source§

impl<U, T: Neg> Neg for Length<T, U>

Source§

type Output = Length<<T as Neg>::Output, U>

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl<T: Ord, U> Ord for Length<T, U>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq, U> PartialEq for Length<T, U>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: PartialOrd, U> PartialOrd for Length<T, U>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T: Saturating, U> Saturating for Length<T, U>

Source§

fn saturating_add(self, other: Self) -> Self

Saturating addition operator. Returns a+b, saturating at the numeric bounds instead of overflowing.
Source§

fn saturating_sub(self, other: Self) -> Self

Saturating subtraction operator. Returns a-b, saturating at the numeric bounds instead of overflowing.
Source§

impl<T, U> Serialize for Length<T, U>
where T: Serialize,

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl<T: Sub, U> Sub for Length<T, U>

Source§

type Output = Length<<T as Sub>::Output, U>

The resulting type after applying the - operator.
Source§

fn sub(self, other: Length<T, U>) -> Self::Output

Performs the - operation. Read more
Source§

impl<T: SubAssign, U> SubAssign for Length<T, U>

Source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
Source§

impl<T: Zero, U> Zero for Length<T, U>

Source§

fn zero() -> Self

Source§

impl<T: Copy, U> Copy for Length<T, U>

Source§

impl<T: Eq, U> Eq for Length<T, U>

Auto Trait Implementations§

§

impl<T, Unit> Freeze for Length<T, Unit>
where T: Freeze,

§

impl<T, Unit> RefUnwindSafe for Length<T, Unit>
where T: RefUnwindSafe, Unit: RefUnwindSafe,

§

impl<T, Unit> Send for Length<T, Unit>
where T: Send, Unit: Send,

§

impl<T, Unit> Sync for Length<T, Unit>
where T: Sync, Unit: Sync,

§

impl<T, Unit> Unpin for Length<T, Unit>
where T: Unpin, Unit: Unpin,

§

impl<T, Unit> UnwindSafe for Length<T, Unit>
where T: UnwindSafe, Unit: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,