Skip to main content

Corners

Struct Corners 

Source
#[repr(C)]
pub struct Corners<T: Clone + Debug + Default + PartialEq> { pub top_left: T, pub top_right: T, pub bottom_right: T, pub bottom_left: T, }
Expand description

Represents the corners of a box in a 2D space, such as border radius.

Each field represents the size of the corner on one side of the box: top_left, top_right, bottom_right, and bottom_left.

Fields§

§top_left: T

The value associated with the top left corner.

§top_right: T

The value associated with the top right corner.

§bottom_right: T

The value associated with the bottom right corner.

§bottom_left: T

The value associated with the bottom left corner.

Implementations§

Source§

impl<T> Corners<T>
where T: Add<T, Output = T> + Half + Clone + Debug + Default + PartialEq,

Source

pub fn all(value: T) -> Self

Constructs Corners where all sides are set to the same specified value.

This function creates a Corners instance with the top_left, top_right, bottom_right, and bottom_left fields all initialized to the same value provided as an argument. This is useful when you want to have uniform corners around a box, such as a uniform border radius on a rectangle.

§Arguments
  • value - The value to set for all four corners.
§Returns

An Corners instance with all corners set to the given value.

§Examples
let uniform_corners = Corners::all(5.0);
assert_eq!(uniform_corners.top_left, 5.0);
assert_eq!(uniform_corners.top_right, 5.0);
assert_eq!(uniform_corners.bottom_right, 5.0);
assert_eq!(uniform_corners.bottom_left, 5.0);
Source

pub fn corner(&self, corner: Anchor) -> T

Returns the requested corner value, supporting all eight corner positions.

For the four basic corners (TopLeft, TopRight, BottomLeft, BottomRight), this returns the corresponding field value directly.

For the center positions (TopCenter, BottomCenter, LeftCenter, RightCenter), this calculates the average of the two adjacent corners.

§Returns

A value of type T representing the corner requested by the parameter.

§Examples

Basic corner positions:

let corners = Corners {
    top_left: 10,
    top_right: 20,
    bottom_left: 30,
    bottom_right: 40
};
assert_eq!(corners.corner(Anchor::TopLeft), 10);
assert_eq!(corners.corner(Anchor::BottomRight), 40);

Center positions (calculated as average of adjacent corners):

let corners = Corners {
    top_left: 10,
    top_right: 20,
    bottom_left: 30,
    bottom_right: 40
};
assert_eq!(corners.corner(Anchor::TopCenter), 15);
assert_eq!(corners.corner(Anchor::BottomCenter), 35);
assert_eq!(corners.corner(Anchor::LeftCenter), 20);
assert_eq!(corners.corner(Anchor::RightCenter), 30);
Source§

impl Corners<AbsoluteLength>

Source

pub fn to_pixels(self, rem_size: Pixels) -> Corners<Pixels>

Converts the AbsoluteLength to Pixels based on the provided rem size.

§Arguments
  • rem_size - The size of one REM unit in pixels, used for conversion if the AbsoluteLength is in REMs.
§Returns

Returns a Corners<Pixels> instance with each corner’s length converted to pixels.

§Examples
let corners = Corners {
    top_left: AbsoluteLength::Pixels(Pixels::from(15.0)),
    top_right: AbsoluteLength::Rems(Rems(1.0)),
    bottom_right: AbsoluteLength::Pixels(Pixels::from(30.0)),
    bottom_left: AbsoluteLength::Rems(Rems(2.0)),
};
let rem_size = Pixels::from(16.0);
let corners_in_pixels = corners.to_pixels(rem_size);

assert_eq!(corners_in_pixels.top_left, Pixels::from(15.0));
assert_eq!(corners_in_pixels.top_right, Pixels::from(16.0)); // 1 rem converted to pixels
assert_eq!(corners_in_pixels.bottom_right, Pixels::from(30.0));
assert_eq!(corners_in_pixels.bottom_left, Pixels::from(32.0)); // 2 rems converted to pixels
Source§

impl Corners<Pixels>

Source

pub fn scale(&self, factor: f32) -> Corners<ScaledPixels>

Scales the Corners<Pixels> by a given factor, returning Corners<ScaledPixels>.

This method is typically used for adjusting the corner sizes for different display densities or scaling factors.

§Arguments
  • factor - The scaling factor to apply to each corner.
§Returns

Returns a new Corners<ScaledPixels> where each corner is the result of scaling the original corner by the given factor.

§Examples
let corners = Corners {
    top_left: Pixels::from(10.0),
    top_right: Pixels::from(20.0),
    bottom_right: Pixels::from(30.0),
    bottom_left: Pixels::from(40.0),
};
let scaled_corners = corners.scale(2.0);
assert_eq!(scaled_corners.top_left, ScaledPixels::from(20.0));
assert_eq!(scaled_corners.top_right, ScaledPixels::from(40.0));
assert_eq!(scaled_corners.bottom_right, ScaledPixels::from(60.0));
assert_eq!(scaled_corners.bottom_left, ScaledPixels::from(80.0));
Source

pub fn max(&self) -> Pixels

Returns the maximum value of any corner.

§Returns

The maximum Pixels value among all four corners.

Source§

impl<T: Div<f32, Output = T> + Ord + Clone + Debug + Default + PartialEq> Corners<T>

Source

pub fn clamp_radii_for_quad_size(self, size: Size<T>) -> Corners<T>

Clamps corner radii to be less than or equal to half the shortest side of a quad.

§Arguments
  • size - The size of the quad which limits the size of the corner radii.
§Returns

Anchor radii values clamped to fit.

Source§

impl<T: Clone + Debug + Default + PartialEq> Corners<T>

Source

pub fn map<U>(&self, f: impl Fn(&T) -> U) -> Corners<U>
where U: Clone + Debug + Default + PartialEq,

Applies a function to each field of the Corners, producing a new Corners<U>.

This method allows for converting a Corners<T> to a Corners<U> by specifying a closure that defines how to convert between the two types. The closure is applied to each field (top_left, top_right, bottom_right, bottom_left), resulting in new corners of the desired type.

§Arguments
  • f - A closure that takes a reference to a value of type T and returns a value of type U.
§Returns

Returns a new Corners<U> with each field mapped by the provided function.

§Examples
let corners = Corners {
    top_left: Pixels::from(10.0),
    top_right: Pixels::from(20.0),
    bottom_right: Pixels::from(30.0),
    bottom_left: Pixels::from(40.0),
};
let corners_in_rems = corners.map(|&px| Rems(f32::from(px) / 16.0));
assert_eq!(corners_in_rems, Corners {
    top_left: Rems(0.625),
    top_right: Rems(1.25),
    bottom_right: Rems(1.875),
    bottom_left: Rems(2.5),
});

Trait Implementations§

Source§

impl<T: Clone + Clone + Debug + Default + PartialEq> Clone for Corners<T>

Source§

fn clone(&self) -> Corners<T>

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<T: Debug + Clone + Debug + Default + PartialEq> Debug for Corners<T>

Source§

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

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

impl<T: Default + Clone + Debug + Default + PartialEq> Default for Corners<T>

Source§

fn default() -> Corners<T>

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

impl<T: Clone + Debug + Default + PartialEq> From<CornersRefinement<T>> for Corners<T>
where Option<T>: Clone,

Source§

fn from(value: CornersRefinement<T>) -> Self

Converts to this type from the input type.
Source§

impl From<Pixels> for Corners<Pixels>

Source§

fn from(val: Pixels) -> Self

Converts to this type from the input type.
Source§

impl From<f32> for Corners<Pixels>

Source§

fn from(val: f32) -> Self

Converts to this type from the input type.
Source§

impl<T> IsZero for Corners<T>
where T: IsZero + Clone + Debug + Default + PartialEq,

Source§

fn is_zero(&self) -> bool

Determines if the value is zero. Read more
Source§

impl<T> Mul for Corners<T>
where T: Mul<Output = T> + Clone + Debug + Default + PartialEq,

Source§

type Output = Corners<T>

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Self) -> Self::Output

Performs the * operation. Read more
Source§

impl<T, S> MulAssign<S> for Corners<T>
where T: Mul<S, Output = T> + Clone + Debug + Default + PartialEq, S: Clone,

Source§

fn mul_assign(&mut self, rhs: S)

Performs the *= operation. Read more
Source§

impl<T: PartialEq + Clone + Debug + Default + PartialEq> PartialEq for Corners<T>

Source§

fn eq(&self, other: &Corners<T>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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: Clone + Debug + Default + PartialEq> Refineable for Corners<T>
where Option<T>: Clone,

Source§

type Refinement = CornersRefinement<T>

Source§

fn refine(&mut self, refinement: &Self::Refinement)

Applies the given refinement to this instance, modifying it in place. Read more
Source§

fn refined(self, refinement: Self::Refinement) -> Self

Returns a new instance with the refinement applied, equivalent to cloning self and calling refine on it.
Source§

fn is_superset_of(&self, refinement: &Self::Refinement) -> bool

Returns true if this instance would contain all values from the refinement. Read more
Source§

fn subtract(&self, refinement: &Self::Refinement) -> Self::Refinement

Returns a refinement that represents the difference between this instance and the given refinement. Read more
Source§

fn from_cascade(cascade: &Cascade<Self>) -> Self
where Self: Sized + Default,

Creates an instance from a cascade by merging all refinements atop the default value.
Source§

impl<T> Copy for Corners<T>
where T: Copy + Clone + Debug + Default + PartialEq,

Source§

impl<T: Eq + Clone + Debug + Default + PartialEq> Eq for Corners<T>

Source§

impl<T: Clone + Debug + Default + PartialEq> StructuralPartialEq for Corners<T>

Auto Trait Implementations§

§

impl<T> Freeze for Corners<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Corners<T>
where T: RefUnwindSafe,

§

impl<T> Send for Corners<T>
where T: Send,

§

impl<T> Sync for Corners<T>
where T: Sync,

§

impl<T> Unpin for Corners<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for Corners<T>
where T: UnsafeUnpin,

§

impl<T> UnwindSafe for Corners<T>
where T: 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, 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> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more