pub struct Size2D<T> {
pub width: T,
pub height: T,
}
Expand description
A vector describing a two-dimensional size.
Fields
width: T
height: T
Implementations
sourceimpl<T> Size2D<T> where
T: Copy,
impl<T> Size2D<T> where
T: Copy,
sourcepub fn square(size: T) -> Self
pub fn square(size: T) -> Self
Returns a new Size2D where width
and height
are equal.
Prefer using the splat syntax with the size()
macro instead
of calling this directly.
Examples
// This is acceptable, but...
let size = Size2D::square(200);
// ...this is the preferred way
let size = size!(200; 2);
assert_eq!(size, size!(200, 200));
sourcepub fn area(&self) -> T where
T: Mul<Output = T>,
pub fn area(&self) -> T where
T: Mul<Output = T>,
Gets the area of the size. This is a shorthand for size.width() * size.height()
Examples
let size = size!(100, 200);
assert_eq!(size.area(), 20000);
sourcepub fn cast<C>(&self) -> Size2D<C> where
C: Copy + 'static,
T: AsPrimitive<C>,
pub fn cast<C>(&self) -> Size2D<C> where
C: Copy + 'static,
T: AsPrimitive<C>,
sourceimpl<T> Size2D<T> where
T: Num + Copy + PartialOrd,
impl<T> Size2D<T> where
T: Num + Copy + PartialOrd,
sourcepub fn grow<S: ToSize2D<T>>(&self, size: S) -> Size2D<T>
pub fn grow<S: ToSize2D<T>>(&self, size: S) -> Size2D<T>
Compares the width and height components in size
and self
, creating a new size
where the components are the greater values. This is the opposite of shrink()
You can think of this as a one-way operation of expanding a rectangle.
Examples
let inner = size!(500, 700);
let outer = size!(400, 900);
assert_eq!(inner.grow(outer), size!(500, 900));
sourcepub fn shrink<S: ToSize2D<T>>(&self, size: S) -> Size2D<T>
pub fn shrink<S: ToSize2D<T>>(&self, size: S) -> Size2D<T>
Compares the width and height components in size
and self
, creating a new size
where the components are the lesser values. This is the opposite of grow()
You can think of this as a one-way operation of collapsing a rectangle.
Examples
let outer = size!(500, 200);
let inner = size!(100, 300);
assert_eq!(outer.shrink(inner), size!(100, 200));
sourcepub fn constrain<S: ToSize2D<T>>(&self, min: S, max: S) -> Size2D<T>
pub fn constrain<S: ToSize2D<T>>(&self, min: S, max: S) -> Size2D<T>
Returns a new size constrained within the min
and max
.
This is a shorthand for self.shrink(max).grow(min)
Examples
let min = size!(200, 300);
let max = size!(500, 500);
assert_eq!(size!(100, 400).constrain(min, max), size!(200, 400));
assert_eq!(size!(600, 300).constrain(min, max), size!(500, 300));
sourceimpl<T> Size2D<T> where
T: Copy,
Self: PartialOrd,
impl<T> Size2D<T> where
T: Copy,
Self: PartialOrd,
sourcepub fn clamp_area<S: ToSize2D<T>>(&self, min: S, max: S) -> Size2D<T>
pub fn clamp_area<S: ToSize2D<T>>(&self, min: S, max: S) -> Size2D<T>
Clamps the size area between min
and max
.
If you need to clamp individual components, use constrain()
Examples
let a = size!(200, 600);
let b = size!(100, 200);
assert_eq!(a.clamp_area(b, size!(300, 300)), size!(300, 300));
Trait Implementations
sourceimpl<T, R> Add<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
impl<T, R> Add<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
Implements adding two sizes together.
sourceimpl<T, R> AddAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
impl<T, R> AddAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
sourcefn add_assign(&mut self, rhs: R)
fn add_assign(&mut self, rhs: R)
Performs the +=
operation. Read more
sourceimpl<T, R> Div<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
impl<T, R> Div<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
Implements dividing two points
sourceimpl<T, R> DivAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
impl<T, R> DivAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
sourcefn div_assign(&mut self, rhs: R)
fn div_assign(&mut self, rhs: R)
Performs the /=
operation. Read more
sourceimpl<T, R> Mul<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
impl<T, R> Mul<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
Implements multiplying two points
sourceimpl<T, R> MulAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
impl<T, R> MulAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
sourcefn mul_assign(&mut self, rhs: R)
fn mul_assign(&mut self, rhs: R)
Performs the *=
operation. Read more
sourceimpl<T> PartialOrd<Size2D<T>> for Size2D<T> where
T: Num + Copy + PartialEq + PartialOrd,
impl<T> PartialOrd<Size2D<T>> for Size2D<T> where
T: Num + Copy + PartialEq + PartialOrd,
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl<T, R> Sub<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
impl<T, R> Sub<R> for Size2D<T> where
T: Num + Copy,
R: ToSize2D<T>,
Implements subtracting two sizes
sourceimpl<T, R> SubAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
impl<T, R> SubAssign<R> for Size2D<T> where
T: Num + NumAssign + Copy,
R: ToSize2D<T>,
sourcefn sub_assign(&mut self, rhs: R)
fn sub_assign(&mut self, rhs: R)
Performs the -=
operation. Read more
impl<T: Copy> Copy for Size2D<T>
impl<T> StructuralPartialEq for Size2D<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Size2D<T> where
T: RefUnwindSafe,
impl<T> Send for Size2D<T> where
T: Send,
impl<T> Sync for Size2D<T> where
T: Sync,
impl<T> Unpin for Size2D<T> where
T: Unpin,
impl<T> UnwindSafe for Size2D<T> where
T: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more