Enum style::values::generics::calc::GenericCalcNode

source ·
#[repr(u8)]
pub enum GenericCalcNode<L> { Leaf(L), Negate(Box<GenericCalcNode<L>>), Invert(Box<GenericCalcNode<L>>), Sum(OwnedSlice<GenericCalcNode<L>>), Product(OwnedSlice<GenericCalcNode<L>>), MinMax(OwnedSlice<GenericCalcNode<L>>, MinMaxOp), Clamp { min: Box<GenericCalcNode<L>>, center: Box<GenericCalcNode<L>>, max: Box<GenericCalcNode<L>>, }, Round { strategy: RoundingStrategy, value: Box<GenericCalcNode<L>>, step: Box<GenericCalcNode<L>>, }, ModRem { dividend: Box<GenericCalcNode<L>>, divisor: Box<GenericCalcNode<L>>, op: ModRemOp, }, Hypot(OwnedSlice<GenericCalcNode<L>>), Abs(Box<GenericCalcNode<L>>), Sign(Box<GenericCalcNode<L>>), }
Expand description

A generic node in a calc expression.

FIXME: This would be much more elegant if we used Self in the types below, but we can’t because of https://github.com/serde-rs/serde/issues/1565.

FIXME: The following annotations are to workaround an LLVM inlining bug, see bug 1631929.

cbindgen:destructor-attributes=MOZ_NEVER_INLINE cbindgen:copy-constructor-attributes=MOZ_NEVER_INLINE cbindgen:eq-attributes=MOZ_NEVER_INLINE

Variants§

§

Leaf(L)

A leaf node.

§

Negate(Box<GenericCalcNode<L>>)

A node that negates its child, e.g. Negate(1) == -1.

§

Invert(Box<GenericCalcNode<L>>)

A node that inverts its child, e.g. Invert(10) == 1 / 10 == 0.1. The child must always resolve to a number unit.

§

Sum(OwnedSlice<GenericCalcNode<L>>)

A sum node, representing a + b + c where a, b, and c are the arguments.

§

Product(OwnedSlice<GenericCalcNode<L>>)

A product node, representing a * b * c where a, b, and c are the arguments.

§

MinMax(OwnedSlice<GenericCalcNode<L>>, MinMaxOp)

A min or max function.

§

Clamp

A clamp() function.

Fields

§min: Box<GenericCalcNode<L>>

The minimum value.

§center: Box<GenericCalcNode<L>>

The central value.

§max: Box<GenericCalcNode<L>>

The maximum value.

§

Round

A round() function.

Fields

§strategy: RoundingStrategy

The rounding strategy.

§value: Box<GenericCalcNode<L>>

The value to round.

§step: Box<GenericCalcNode<L>>

The step value.

§

ModRem

A mod() or rem() function.

Fields

§dividend: Box<GenericCalcNode<L>>

The dividend calculation.

§divisor: Box<GenericCalcNode<L>>

The divisor calculation.

§op: ModRemOp

Is the function mod or rem?

§

Hypot(OwnedSlice<GenericCalcNode<L>>)

A hypot() function

§

Abs(Box<GenericCalcNode<L>>)

An abs() function.

§

Sign(Box<GenericCalcNode<L>>)

A sign() function.

Implementations§

source§

impl<L: CalcNodeLeaf> CalcNode<L>

source

fn dummy() -> Self

Create a dummy CalcNode that can be used to do replacements of other nodes.

source

fn coerce_to_value(&mut self, value: f32) -> Result<(), ()>

Change all the leaf nodes to have the given value. This is useful when you have calc(1px * nan) and you want to replace the product node with calc(nan), in which case the unit will be retained.

source

pub fn is_product_distributive(&self) -> bool

Return true if a product is distributive over this node. Is distributive: (2 + 3) * 4 = 8 + 12 Not distributive: sign(2 + 3) * 4 != sign(8 + 12)

source

pub fn unit(&self) -> Result<CalcUnits, ()>

If the node has a valid unit outcome, then return it, otherwise fail.

source

pub fn negate(&mut self)

Negate the node inline. If the node is distributive, it is replaced by the result, otherwise the node is wrapped in a [Negate] node.

source

fn sort_key(&self) -> SortKey

source

pub fn as_leaf(&self) -> Option<&L>

Returns the leaf if we can (if simplification has allowed it).

source

fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>

Tries to merge one node into another using the sum, that is, perform x + y.

source

pub fn try_product_in_place(&mut self, other: &mut Self) -> bool

Tries to merge one node into another using the product, that is, perform x * y.

source

fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
where O: Fn(f32, f32) -> f32,

Tries to apply a generic arithmetic operator

source

pub fn map(&mut self, op: impl FnMut(f32) -> f32) -> Result<(), ()>

Map the value of this node with the given operation.

source

pub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>
where O: CalcNodeLeaf, F: FnMut(&L) -> O,

Convert this CalcNode into a CalcNode with a different leaf kind.

source

fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>
where O: CalcNodeLeaf, F: FnMut(&L) -> O,

source

pub fn resolve(&self) -> Result<L, ()>

Resolve this node into a value.

source

pub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>
where F: FnMut(&L) -> Result<L, ()>,

Resolve this node into a value, given a function that maps the leaf values.

source

fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>
where F: FnMut(&L) -> Result<L, ()>,

source

fn is_negative_leaf(&self) -> Result<bool, ()>

source

fn is_zero_leaf(&self) -> Result<bool, ()>

source

fn is_infinite_leaf(&self) -> Result<bool, ()>

source

fn is_nan_leaf(&self) -> Result<bool, ()>

source

pub fn visit_depth_first(&mut self, f: impl FnMut(&mut Self))

Visits all the nodes in this calculation tree recursively, starting by the leaves and bubbling all the way up.

This is useful for simplification, but can also be used for validation and such.

source

fn visit_depth_first_internal(&mut self, f: &mut impl FnMut(&mut Self))

source

pub fn simplify_and_sort_direct_children(&mut self)

This function simplifies and sorts the calculation of the specified node. It simplifies directly nested nodes while assuming that all nodes below it have already been simplified. It is recommended to use this function in combination with visit_depth_first().

This function is necessary only if the node needs to be preserved after parsing, specifically for <length-percentage> cases where the calculation contains percentages or relative units. Otherwise, the node can be evaluated using resolve(), which will automatically provide a simplified value.

https://drafts.csswg.org/css-values-4/#calc-simplification

source

pub fn simplify_and_sort(&mut self)

Simplifies and sorts the kids in the whole calculation subtree.

source

fn to_css_impl<W>( &self, dest: &mut CssWriter<'_, W>, level: ArgumentLevel, ) -> Result
where W: Write,

source

fn compare( &self, other: &Self, basis_positive: PositivePercentageBasis, ) -> Option<Ordering>

source

fn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is greater than another.

source

fn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is less than another.

source

fn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool

Return whether a leaf is smaller or equal than another.

source§

impl GenericCalcNode<Leaf>

source

fn parse_one<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits, ) -> Result<Self, ParseError<'i>>

Tries to parse a single element in the expression, that is, a <length>, <angle>, <time>, <percentage>, <resolution>, etc.

May return a “complex” CalcNode, in the presence of a parenthesized expression, for example.

source

pub fn parse<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, allowed_units: CalcUnits, ) -> Result<Self, ParseError<'i>>

Parse a top-level calc expression, with all nested sub-expressions.

This is in charge of parsing, for example, 2 + 3 * 100%.

source

fn parse_angle_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, ) -> Result<CSSFloat, ParseError<'i>>

source

fn parse_number_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, ) -> Result<CSSFloat, ParseError<'i>>

source

fn parse_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits, ) -> Result<Self, ParseError<'i>>

source

fn parse_product<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits, ) -> Result<Self, ParseError<'i>>

Parse a top-level calc expression, and all the products that may follow, and stop as soon as a non-product expression is found.

This should parse correctly:

  • 2
  • 2 * 2
  • 2 * 2 + 2 (but will leave the + 2 unparsed).
source

fn try_resolve<'i, 't, F>( input: &Parser<'i, 't>, closure: F, ) -> Result<CSSFloat, ParseError<'i>>
where F: FnOnce() -> Result<CSSFloat, ()>,

source

pub fn into_length_or_percentage( self, clamping_mode: AllowedNumericType, ) -> Result<CalcLengthPercentage, ()>

Tries to simplify this expression into a <length> or <percentage> value.

source

fn to_time(&self, clamping_mode: Option<AllowedNumericType>) -> Result<Time, ()>

Tries to simplify this expression into a <time> value.

source

fn to_resolution(&self) -> Result<Resolution, ()>

Tries to simplify the expression into a <resolution> value.

source

fn to_angle(&self) -> Result<Angle, ()>

Tries to simplify this expression into an Angle value.

source

fn to_number(&self) -> Result<CSSFloat, ()>

Tries to simplify this expression into a <number> value.

source

fn to_percentage(&self) -> Result<CSSFloat, ()>

Tries to simplify this expression into a <percentage> value.

source

pub fn math_function<'i>( _: &ParserContext<'_>, name: &CowRcStr<'i>, location: SourceLocation, ) -> Result<MathFunction, ParseError<'i>>

Given a function name, and the location from where the token came from, return a mathematical function corresponding to that name or an error.

source

pub fn parse_integer<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSInteger, ParseError<'i>>

Convenience parsing function for integers.

source

pub fn parse_length_or_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction, ) -> Result<CalcLengthPercentage, ParseError<'i>>

Convenience parsing function for <length> | <percentage>.

source

pub fn parse_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSFloat, ParseError<'i>>

Convenience parsing function for percentages.

source

pub fn parse_length<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction, ) -> Result<CalcLengthPercentage, ParseError<'i>>

Convenience parsing function for <length>.

source

pub fn parse_number<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSFloat, ParseError<'i>>

Convenience parsing function for <number>.

source

pub fn parse_angle<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<Angle, ParseError<'i>>

Convenience parsing function for <angle>.

source

pub fn parse_time<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, clamping_mode: AllowedNumericType, function: MathFunction, ) -> Result<Time, ParseError<'i>>

Convenience parsing function for <time>.

source

pub fn parse_resolution<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<Resolution, ParseError<'i>>

Convenience parsing function for <resolution>.

Trait Implementations§

source§

impl<L: Clone> Clone for GenericCalcNode<L>

source§

fn clone(&self) -> GenericCalcNode<L>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<L: Debug> Debug for GenericCalcNode<L>

source§

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

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

impl<'de, L> Deserialize<'de> for GenericCalcNode<L>
where L: Deserialize<'de>,

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<L> MallocSizeOf for GenericCalcNode<L>
where L: MallocSizeOf,

source§

fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize

Measure the heap usage of all descendant heap-allocated structures, but not the space taken up by the value itself.
source§

impl<L: PartialEq> PartialEq for GenericCalcNode<L>

source§

fn eq(&self, other: &GenericCalcNode<L>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<L> Serialize for GenericCalcNode<L>
where L: Serialize,

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl<L> ToAnimatedZero for GenericCalcNode<L>
where L: ToAnimatedZero,

source§

fn to_animated_zero(&self) -> Result<Self, ()>

Returns a value that, when added with an underlying value, will produce the underlying value. This is used for SMIL animation’s “by-animation” where SMIL first interpolates from the zero value to the ‘by’ value, and then adds the result to the underlying value. Read more
source§

impl<L: CalcNodeLeaf> ToCss for CalcNode<L>

source§

fn to_css<W>(&self, dest: &mut CssWriter<'_, W>) -> Result
where W: Write,

source§

fn to_css_string(&self) -> String

Serialize self in CSS syntax and return a string. Read more
source§

impl<L> ToResolvedValue for GenericCalcNode<L>
where L: ToResolvedValue,

§

type ResolvedValue = GenericCalcNode<<L as ToResolvedValue>::ResolvedValue>

The resolved value type we’re going to be converted to.
source§

fn from_resolved_value(from: Self::ResolvedValue) -> Self

Convert a resolved value to resolved value form.
source§

fn to_resolved_value(self, context: &Context<'_>) -> Self::ResolvedValue

Convert a resolved value to a resolved value.
source§

impl<L> ToShmem for GenericCalcNode<L>
where L: ToShmem,

source§

fn to_shmem(&self, builder: &mut SharedMemoryBuilder) -> Result<Self>

Clones this value into a form suitable for writing into a SharedMemoryBuilder. Read more
source§

impl<L> StructuralPartialEq for GenericCalcNode<L>

Auto Trait Implementations§

§

impl<L> Freeze for GenericCalcNode<L>
where L: Freeze,

§

impl<L> RefUnwindSafe for GenericCalcNode<L>
where L: RefUnwindSafe,

§

impl<L> Send for GenericCalcNode<L>
where L: Send,

§

impl<L> Sync for GenericCalcNode<L>
where L: Sync,

§

impl<L> Unpin for GenericCalcNode<L>
where L: Unpin,

§

impl<L> UnwindSafe for GenericCalcNode<L>

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> 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.
§

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

§

type Output = T

Should always be Self
source§

impl<T> ToOwned for T
where T: Clone,

§

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>,

§

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>,

§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

source§

impl<T> ErasedDestructor for T
where T: 'static,

source§

impl<T> MaybeSendSync for T