Skip to main content

NumericType

Struct NumericType 

Source
#[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: u8

Implementations§

Source§

impl NumericType

Source

fn empty() -> Self

Source

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.

Source

pub fn number() -> Self

A numeric type whose exponent map is empty.

Source

pub fn percent() -> Self

A numeric type whose percent exponent is 1.

Source

pub fn length() -> Self

A numeric type whose length exponent is 1.

Source

pub fn angle() -> Self

A numeric type whose angle exponent is 1.

Source

pub fn time() -> Self

A numeric type whose time exponent is 1.

Source

pub fn frequency() -> Self

A numeric type whose frequency exponent is 1.

Source

pub fn resolution() -> Self

A numeric type whose resolution exponent is 1.

Source

pub fn flex() -> Self

A numeric type whose flex exponent is 1.

Source

pub fn try_from_unit(unit: &str) -> Result<Self, ()>

Source

pub fn from_unit_unchecked(unit: &str) -> Self

Creates a numeric type from a previously validated unit string.

Source

pub fn with_percent_hint(self, hint: NumericBaseType) -> Self

Consumes the type and constructs a new type, applying the given percent hint.

Source

pub fn percent_hint(&self) -> Optional<NumericBaseType>

Returns the percent hint for this type.

Source

fn exponent(&self, base_type: NumericBaseType) -> i32

Source

fn set_exponent(&mut self, base_type: NumericBaseType, new_value: i32)

Source

fn add_exponent(&mut self, base_type: NumericBaseType, delta: i32)

Source

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

Source

fn has_null_percent_hint(&self) -> bool

Source

fn only_non_zero_entry_is( &self, base_type: NumericBaseType, exponent: i32, ) -> bool

Source

fn has_no_non_zero_entries(&self) -> bool

Source

fn matches_length_in_percentage_context(&self) -> bool

Source

#[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

Matches .

Source

#[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

Matches .

Source

#[unsafe(export_name = "Servo_NumericType_MatchesNumber")]
pub extern "C" fn matches_number(&self) -> bool

Matches .

Source

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

Source

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.

Source

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.

Source

fn combine_types<'a, I>( types: I, combine: fn(&NumericType, &NumericType) -> Result<NumericType, ()>, ) -> Result<Self, ()>
where I: Iterator<Item = &'a NumericType>,

Source

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.

Source

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.

Source

pub fn is_number(&self) -> bool

Returns whether this type is a dimensionless number.

Source

pub fn as_calc_type(&self) -> Result<CalcType, ()>

Returns a CalcType if this type represents a single data type, like or .

Trait Implementations§

Source§

impl Clone for NumericType

Source§

fn clone(&self) -> NumericType

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 Debug for NumericType

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> MaybeBoxed<Box<T>> for T

Source§

fn maybe_boxed(self) -> Box<T>

Convert
Source§

impl<T> MaybeBoxed<T> for T

Source§

fn maybe_boxed(self) -> T

Convert
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<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<T> ErasedDestructor for T
where T: 'static,