// Copyright 2013 The Servo Project Developers. See the COPYRIGHT // file at the top-level directory of this distribution. // // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your // option. This file may not be copied, modified, or distributed // except according to those terms.
//! A group of side offsets, which correspond to top/left/bottom/right for borders, padding, //! and margins in CSS.
use core::cmp::{Eq, PartialEq}; use core::fmt; use core::hash::Hash; use core::marker::PhantomData; use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[cfg(feature = "bytemuck")] use bytemuck::{Pod, Zeroable}; #[cfg(feature = "serde")] use serde::{Deserialize, Serialize};
/// A group of 2D side offsets, which correspond to top/right/bottom/left for borders, padding, /// and margins in CSS, optionally tagged with a unit. #[repr(C)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Serialize", deserialize = "T: Deserialize<'de>"))
)] pubstruct SideOffsets2D<T, U> { pub top: T, pub right: T, pub bottom: T, pub left: T, #[doc(hidden)] pub _unit: PhantomData<U>,
}
impl<T, U> SideOffsets2D<T, U> { /// Constructor taking a scalar for each side. /// /// Sides are specified in top-right-bottom-left order following /// CSS's convention. pubconstfn new(top: T, right: T, bottom: T, left: T) -> Self {
SideOffsets2D {
top,
right,
bottom,
left,
_unit: PhantomData,
}
}
/// Constructor taking a typed Length for each side. /// /// Sides are specified in top-right-bottom-left order following /// CSS's convention. pubfn from_lengths(
top: Length<T, U>,
right: Length<T, U>,
bottom: Length<T, U>,
left: Length<T, U>,
) -> Self {
SideOffsets2D::new(top.0, right.0, bottom.0, left.0)
}
/// Construct side offsets from min and a max vector offsets. /// /// The outer rect of the resulting side offsets is equivalent to translating /// a rectangle's upper-left corner with the min vector and translating the /// bottom-right corner with the max vector. pubfn from_vectors_outer(min: Vector2D<T, U>, max: Vector2D<T, U>) -> Self where
T: Neg<Output = T>,
{
SideOffsets2D {
left: -min.x,
top: -min.y,
right: max.x,
bottom: max.y,
_unit: PhantomData,
}
}
/// Construct side offsets from min and a max vector offsets. /// /// The inner rect of the resulting side offsets is equivalent to translating /// a rectangle's upper-left corner with the min vector and translating the /// bottom-right corner with the max vector. pubfn from_vectors_inner(min: Vector2D<T, U>, max: Vector2D<T, U>) -> Self where
T: Neg<Output = T>,
{
SideOffsets2D {
left: min.x,
top: min.y,
right: -max.x,
bottom: -max.y,
_unit: PhantomData,
}
}
/// Constructor, setting all sides to zero. pubfn zero() -> Self where
T: Zero,
{
SideOffsets2D::new(Zero::zero(), Zero::zero(), Zero::zero(), Zero::zero())
}
/// Returns `true` if all side offsets are zero. pubfn is_zero(&self) -> bool where
T: Zero + PartialEq,
{ let zero = T::zero(); self.top == zero && self.right == zero && self.bottom == zero && self.left == zero
}
/// Constructor setting the same value to all sides, taking a scalar value directly. pubfn new_all_same(all: T) -> Self where
T: Copy,
{
SideOffsets2D::new(all, all, all, all)
}
/// Constructor setting the same value to all sides, taking a typed Length. pubfn from_length_all_same(all: Length<T, U>) -> Self where
T: Copy,
{
SideOffsets2D::new_all_same(all.0)
}
pubfn horizontal(&self) -> T where
T: Copy + Add<T, Output = T>,
{ self.left + self.right
}
pubfn vertical(&self) -> T where
T: Copy + Add<T, Output = T>,
{ self.top + self.bottom
}
}
type SideOffsets2D<T> = crate::default::SideOffsets2D<T>; type SideOffsets2DMm<T> = crate::SideOffsets2D<T, Mm>; type SideOffsets2DCm<T> = crate::SideOffsets2D<T, Cm>;
#[test] fn test_mul_scalar() { let s = SideOffsets2D::new(1.0, 2.0, 3.0, 4.0);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.