Skip to main content

AspectRatio

Struct AspectRatio 

Source
pub struct AspectRatio { /* private fields */ }
Expand description

A width-to-height ratio, exact.

Stored as an integer fraction rather than an f64 because 16:9 is not representable in binary floating point, and a window that must stay aspect-locked across thousands of resize events accumulates that error into a visible drift. Comparisons and the fit math below stay in integers where they can.

Implementations§

Source§

impl AspectRatio

Source

pub const WIDESCREEN: Self

16:9.

Source

pub const CLASSIC: Self

4:3.

Source

pub const SQUARE: Self

1:1.

Source

pub const fn new(width: u32, height: u32) -> Self

A ratio of width : height.

§Panics

If either term is zero. A degenerate aspect ratio has no correct behaviour and silently substituting one hides the caller’s bug.

Source

pub const fn numerator(self) -> u32

The width term.

Source

pub const fn denominator(self) -> u32

The height term.

Source

pub fn as_f64(self) -> f64

The ratio as a float, for shader constants and letterbox math.

Source

pub fn fit(self, bounds: PhysicalSize) -> PhysicalSize

The largest size with this aspect ratio that fits inside bounds.

This is the letterbox computation, and it lives here because the windowing rules in docs/notes/backends.md make in-renderer letterboxing the universal fallback: Wayland has no aspect hint at all, and a tiling window manager can force any size on any backend, so every consumer that aspect-locks needs this math and none of them should write it twice.

Returns a size of at most bounds in both axes, and at least 1×1.

Trait Implementations§

Source§

impl Clone for AspectRatio

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for AspectRatio

Source§

impl Debug for AspectRatio

Source§

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

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

impl Display for AspectRatio

Source§

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

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

impl Eq for AspectRatio

Source§

impl Hash for AspectRatio

Source§

fn hash<__H: Hasher>(&self, state: &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 PartialEq for AspectRatio

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for AspectRatio

Auto Trait Implementations§

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, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.