#[repr(C)]
pub struct Scale<T, Src, Dst>(pub T, _);
Expand description

A scaling factor between two different units of measurement.

This is effectively a type-safe float, intended to be used in combination with other types like length::Length to enforce conversion between systems of measurement at compile time.

Src and Dst represent the units before and after multiplying a value by a Scale. They may be types without values, such as empty enums. For example:

use euclid::Scale;
use euclid::Length;
enum Mm {};
enum Inch {};

let mm_per_inch: Scale<f32, Inch, Mm> = Scale::new(25.4);

let one_foot: Length<f32, Inch> = Length::new(12.0);
let one_foot_in_mm: Length<f32, Mm> = one_foot * mm_per_inch;

Tuple Fields

0: T

Implementations

Creates an identity scale (1.0).

Returns the given point transformed by this scale.

Example
use euclid::{Scale, point2};
enum Mm {};
enum Cm {};

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);

assert_eq!(to_mm.transform_point(point2(42, -42)), point2(420, -420));

Returns the given point transformed by this scale.

Returns the given vector transformed by this scale.

Example
use euclid::{Scale, vec2};
enum Mm {};
enum Cm {};

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);

assert_eq!(to_mm.transform_vector(vec2(42, -42)), vec2(420, -420));

Returns the given vector transformed by this scale.

Example
use euclid::{Scale, size2};
enum Mm {};
enum Cm {};

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);

assert_eq!(to_mm.transform_size(size2(42, -42)), size2(420, -420));

Returns the given rect transformed by this scale.

Example
use euclid::{Scale, rect};
enum Mm {};
enum Cm {};

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);

assert_eq!(to_mm.transform_rect(&rect(1, 2, 42, -42)), rect(10, 20, 420, -420));

Returns the given box transformed by this scale.

Returns the given box transformed by this scale.

Returns true if this scale has no effect.

Example
use euclid::Scale;
use euclid::num::One;
enum Mm {};
enum Cm {};

let cm_per_mm: Scale<f32, Mm, Cm> = Scale::new(0.1);
let mm_per_mm: Scale<f32, Mm, Mm> = Scale::new(1.0);

assert_eq!(cm_per_mm.is_identity(), false);
assert_eq!(mm_per_mm.is_identity(), true);
assert_eq!(mm_per_mm, Scale::one());

Returns the underlying scalar scale factor.

The inverse Scale (1.0 / self).

Example
use euclid::Scale;
enum Mm {};
enum Cm {};

let cm_per_mm: Scale<f32, Cm, Mm> = Scale::new(0.1);

assert_eq!(cm_per_mm.inverse(), Scale::new(10.0));

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

Panics

If the source value cannot be represented by the target type NewT, then method panics. Use try_cast if that must be case.

Example
use euclid::Scale;
enum Mm {};
enum Cm {};

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);

assert_eq!(to_mm.cast::<f32>(), Scale::new(10.0));

That conversion will panic, because i32 not enough to store such big numbers:

use euclid::Scale;
enum Mm {};// millimeter = 10^-2 meters
enum Em {};// exameter   = 10^18 meters

// Panics
let to_em: Scale<i32, Mm, Em> = Scale::new(10e20).cast();

Fallible cast from one numeric representation to another, preserving the units. If the source value cannot be represented by the target type NewT, then None is returned.

Example
use euclid::Scale;
enum Mm {};
enum Cm {};
enum Em {};// Exameter = 10^18 meters

let to_mm: Scale<i32, Cm, Mm> = Scale::new(10);
let to_em: Scale<f32, Mm, Em> = Scale::new(10e20);

assert_eq!(to_mm.try_cast::<f32>(), Some(Scale::new(10.0)));
// Integer to small to store that number
assert_eq!(to_em.try_cast::<i32>(), None);

Trait Implementations

The resulting type after applying the + operator.

Performs the + operation. Read more

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

The resulting type after applying the / operator.

Performs the / operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Performs the /= operation. Read more

Feeds this value into the given Hasher. Read more

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

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

The resulting type after applying the * operator.

Performs the * operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

Performs the *= operation. Read more

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

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

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

The resulting type after applying the - operator.

Performs the - operation. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.