#[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.
Sourcepub fn with_percent_hint(self, hint: NumericBaseType) -> Self
pub fn with_percent_hint(self, hint: NumericBaseType) -> Self
Consumes the type and constructs a new type, applying the given percent hint.
Sourcepub fn percent_hint(&self) -> Optional<NumericBaseType>
pub fn percent_hint(&self) -> Optional<NumericBaseType>
Returns the percent hint for this type.
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)
Sourcepub fn invert(&mut self)
pub fn invert(&mut self)
Inverts this numeric type as described in the CSSMathInvert branch of https://drafts.css-houdini.org/css-typed-om-1/#type-of-a-cssmathvalue
fn has_null_percent_hint(&self) -> bool
fn only_non_zero_entry_is( &self, base_type: NumericBaseType, exponent: i32, ) -> bool
fn has_no_non_zero_entries(&self) -> bool
fn matches_length_in_percentage_context(&self) -> bool
Source#[unsafe(export_name = "Servo_NumericType_MatchesLength")]pub extern "C" fn matches_length(&self) -> bool
#[unsafe(export_name = "Servo_NumericType_MatchesLength")]pub extern "C" fn matches_length(&self) -> bool
Matches
Source#[unsafe(export_name = "Servo_NumericType_MatchesAngle")]pub extern "C" fn matches_angle(&self) -> bool
#[unsafe(export_name = "Servo_NumericType_MatchesAngle")]pub extern "C" fn matches_angle(&self) -> bool
Matches
Source#[unsafe(export_name = "Servo_NumericType_MatchesPercentage")]pub extern "C" fn matches_percentage(&self) -> bool
#[unsafe(export_name = "Servo_NumericType_MatchesPercentage")]pub extern "C" fn matches_percentage(&self) -> bool
Matches
Source#[unsafe(export_name = "Servo_NumericType_MatchesLengthPercentage")]pub extern "C" fn matches_length_percentage(&self) -> bool
#[unsafe(export_name = "Servo_NumericType_MatchesLengthPercentage")]pub extern "C" fn matches_length_percentage(&self) -> bool
Matches
Source#[unsafe(export_name = "Servo_NumericType_MatchesNumber")]pub extern "C" fn matches_number(&self) -> bool
#[unsafe(export_name = "Servo_NumericType_MatchesNumber")]pub extern "C" fn matches_number(&self) -> bool
Matches
Sourcepub fn apply_percent_hint(&mut self, hint: NumericBaseType)
pub fn apply_percent_hint(&mut self, hint: NumericBaseType)
Applies the given percent hint to this type. Note that the spec algorithm specifically says “to a type without a percent hint”, so this will not modify the type if it alreay has a percent hint.
https://drafts.css-houdini.org/css-typed-om-1/#apply-the-percent-hint
Sourcepub fn add_two_types(
type1: &NumericType,
type2: &NumericType,
) -> Result<Self, ()>
pub 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.
Sourcepub fn multiply_two_types(
type1: &NumericType,
type2: &NumericType,
) -> Result<Self, ()>
pub 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.
Sourcepub fn as_calc_type(&self) -> Result<CalcType, ()>
pub fn as_calc_type(&self) -> Result<CalcType, ()>
Returns a CalcType if this type represents a single data type,
like
Trait Implementations§
Source§impl Clone for NumericType
impl Clone for NumericType
Source§fn clone(&self) -> NumericType
fn clone(&self) -> NumericType
1.0.0 (const: unstable) · 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