#[repr(C)]pub struct NumericType {
exponents: [i32; 7],
percent_hint: Optional<NumericBaseType>,
non_zero_count: u8,
non_zero_except_percent_count: u8,
}Expand description
https://drafts.css-houdini.org/css-typed-om-1/#numeric-typing
The spec models the per-base-type exponents as an ordered map keyed by base
type. We use a fixed-size array indexed by NumericBaseType instead. A
missing entry in the spec’s map and a zero entry are observably equivalent
for every operation the spec defines (comparisons and iteration only
consider non-zero entries), so the array representation is simpler, avoids
allocations, and produces the same results.
non_zero_count and non_zero_except_percent_count are derived fields
maintained in sync with exponents, allowing O(1) type compatibility
checks. They fit without padding into the 2 bytes following percent_hint,
so the struct remains 32 bytes.
Fields§
§exponents: [i32; 7]§percent_hint: Optional<NumericBaseType>§non_zero_count: u8§non_zero_except_percent_count: u8Implementations§
Source§impl NumericType
impl NumericType
fn empty() -> Self
Sourcefn with_base_type(base_type: NumericBaseType) -> Self
fn with_base_type(base_type: NumericBaseType) -> Self
Constructs a numeric type from a single base type.
Keep Gecko’s StyleNumericType::WithBaseType() in sync with this implementation.
Sourcepub fn resolution() -> Self
pub fn resolution() -> Self
A numeric type whose resolution exponent is 1.
Sourcepub fn try_from_unit(unit: &str) -> Result<Self, ()>
pub fn try_from_unit(unit: &str) -> Result<Self, ()>
Sourcepub fn from_unit_unchecked(unit: &str) -> Self
pub fn from_unit_unchecked(unit: &str) -> Self
Creates a numeric type from a previously validated unit string.
fn exponent(&self, base_type: NumericBaseType) -> i32
fn set_exponent(&mut self, base_type: NumericBaseType, new_value: i32)
fn add_exponent(&mut self, base_type: NumericBaseType, delta: i32)
Sourcefn apply_percent_hint(&mut self, hint: NumericBaseType)
fn apply_percent_hint(&mut self, hint: NumericBaseType)
Sourcefn add_two_types(type1: &NumericType, type2: &NumericType) -> Result<Self, ()>
fn add_two_types(type1: &NumericType, type2: &NumericType) -> Result<Self, ()>
https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-add-two-types
The algorithm has some complexity and uses branches rather than numbered sub-steps, so the implementation below quotes spec text inline. This is more verbose than usual, but should help map each branch back to the spec when reviewing or debugging.
Sourcefn multiply_two_types(
type1: &NumericType,
type2: &NumericType,
) -> Result<Self, ()>
fn multiply_two_types( type1: &NumericType, type2: &NumericType, ) -> Result<Self, ()>
https://drafts.css-houdini.org/css-typed-om-1/#cssnumericvalue-multiply-two-types
Spec text is quoted inline to make each step easy to map back to the algorithm during review.
fn combine_types<'a, I>(
types: I,
combine: fn(&NumericType, &NumericType) -> Result<NumericType, ()>,
) -> Result<Self, ()>where
I: Iterator<Item = &'a NumericType>,
Sourcepub fn add_types<'a, I>(types: I) -> Result<Self, ()>where
I: Iterator<Item = &'a NumericType>,
pub fn add_types<'a, I>(types: I) -> Result<Self, ()>where
I: Iterator<Item = &'a NumericType>,
Applies the add two types algorithm repeatedly across a sequence of numeric types, returning the combined type.
Sourcepub fn multiply_types<'a, I>(types: I) -> Result<Self, ()>where
I: Iterator<Item = &'a NumericType>,
pub fn multiply_types<'a, I>(types: I) -> Result<Self, ()>where
I: Iterator<Item = &'a NumericType>,
Applies the multiply two types algorithm repeatedly across a sequence of numeric types, returning the combined type.
Trait Implementations§
Source§impl Clone for NumericType
impl Clone for NumericType
Source§fn clone(&self) -> NumericType
fn clone(&self) -> NumericType
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for NumericType
impl RefUnwindSafe for NumericType
impl Send for NumericType
impl Sync for NumericType
impl Unpin for NumericType
impl UnsafeUnpin for NumericType
impl UnwindSafe for NumericType
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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