Struct euclid::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) -> Selfwhere 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 + Copy, U> Add<&Length<T, U>> for Length<T, U>

§

type Output = Length<<T as Add<T>>::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: Add, U> Add<Length<T, U>> for Length<T, U>

§

type Output = Length<<T as Add<T>>::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<Length<T, U>> 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>

§

type Output = Scale<<T as Div<T>>::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>

§

type Output = Length<<T as Div<T>>::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>

§

type Output = Length<<T as Div<T>>::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>

§

type Output = Length<<T as Mul<T>>::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>

§

type Output = Length<<T as Mul<T>>::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>

§

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) -> Selfwhere Self: Sized,

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

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

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

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

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

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<T: PartialOrd, U> PartialOrd<Length<T, U>> 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

This method 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

This method 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

This method 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

This method 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<Length<T, U>> for Length<T, U>

§

type Output = Length<<T as Sub<T>>::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<Length<T, U>> for Length<T, U>

source§

fn sub_assign(&mut self, other: Self)

Performs the -= operation. Read more
source§

impl<'a, T: 'a + Add<Output = T> + Copy + Zero, U: 'a> Sum<&'a Length<T, U>> for Length<T, U>

source§

fn sum<I: Iterator<Item = &'a Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
source§

impl<T: Add<Output = T> + Zero, U> Sum<Length<T, U>> for Length<T, U>

source§

fn sum<I: Iterator<Item = Self>>(iter: I) -> Self

Method which takes an iterator and generates Self from the elements by “summing up” the items.
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> 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. 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 Twhere 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 Twhere T: Clone,

§

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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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 Twhere T: for<'de> Deserialize<'de>,