#[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.
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>
impl<L: CalcNodeLeaf> CalcNode<L>
sourcefn coerce_to_value(&mut self, value: f32) -> Result<(), ()>
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.
sourcepub fn is_product_distributive(&self) -> bool
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)
sourcepub fn unit(&self) -> Result<CalcUnits, ()>
pub fn unit(&self) -> Result<CalcUnits, ()>
If the node has a valid unit outcome, then return it, otherwise fail.
sourcepub fn negate(&mut self)
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.
fn sort_key(&self) -> SortKey
sourcepub fn as_leaf(&self) -> Option<&L>
pub fn as_leaf(&self) -> Option<&L>
Returns the leaf if we can (if simplification has allowed it).
sourcepub fn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>
pub 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
.
sourcepub fn try_product_in_place(&mut self, other: &mut Self) -> bool
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
.
sourcefn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>
Tries to apply a generic arithmetic operator
sourcepub fn map(&mut self, op: impl FnMut(f32) -> f32) -> Result<(), ()>
pub fn map(&mut self, op: impl FnMut(f32) -> f32) -> Result<(), ()>
Map the value of this node with the given operation.
sourcepub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>
pub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>
Convert this CalcNode
into a CalcNode
with a different leaf kind.
fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>
sourcepub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>
pub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>
Resolve this node into a value, given a function that maps the leaf values.
fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>
fn is_negative_leaf(&self) -> Result<bool, ()>
fn is_zero_leaf(&self) -> Result<bool, ()>
fn is_infinite_leaf(&self) -> Result<bool, ()>
fn is_nan_leaf(&self) -> Result<bool, ()>
sourcepub fn visit_depth_first(&mut self, f: impl FnMut(&mut Self))
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.
fn visit_depth_first_internal(&mut self, f: &mut impl FnMut(&mut Self))
sourcepub fn simplify_and_sort_direct_children(&mut self)
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.
sourcepub fn simplify_and_sort(&mut self)
pub fn simplify_and_sort(&mut self)
Simplifies and sorts the kids in the whole calculation subtree.
fn to_css_impl<W>(
&self,
dest: &mut CssWriter<'_, W>,
level: ArgumentLevel,
) -> Resultwhere
W: Write,
fn compare( &self, other: &Self, basis_positive: PositivePercentageBasis, ) -> Option<Ordering>
sourcefn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
fn gt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
Return whether a leaf is greater than another.
sourcefn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
fn lt(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
Return whether a leaf is less than another.
sourcefn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
fn lte(&self, other: &Self, basis_positive: PositivePercentageBasis) -> bool
Return whether a leaf is smaller or equal than another.
source§impl GenericCalcNode<Leaf>
impl GenericCalcNode<Leaf>
sourcefn parse_one<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
allowed_units: CalcUnits,
) -> Result<Self, ParseError<'i>>
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.
sourcepub fn parse<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
allowed_units: CalcUnits,
) -> Result<Self, ParseError<'i>>
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%
.
fn parse_angle_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, ) -> Result<CSSFloat, ParseError<'i>>
fn parse_number_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, ) -> Result<CSSFloat, ParseError<'i>>
fn parse_argument<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, allowed_units: CalcUnits, ) -> Result<Self, ParseError<'i>>
sourcefn parse_product<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
allowed_units: CalcUnits,
) -> Result<Self, ParseError<'i>>
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).
fn try_resolve<'i, 't, F>( input: &Parser<'i, 't>, closure: F, ) -> Result<CSSFloat, ParseError<'i>>
sourcepub fn into_length_or_percentage(
self,
clamping_mode: AllowedNumericType,
) -> Result<CalcLengthPercentage, ()>
pub fn into_length_or_percentage( self, clamping_mode: AllowedNumericType, ) -> Result<CalcLengthPercentage, ()>
Tries to simplify this expression into a <length>
or <percentage>
value.
sourcefn to_time(&self, clamping_mode: Option<AllowedNumericType>) -> Result<Time, ()>
fn to_time(&self, clamping_mode: Option<AllowedNumericType>) -> Result<Time, ()>
Tries to simplify this expression into a <time>
value.
sourcefn to_resolution(&self) -> Result<Resolution, ()>
fn to_resolution(&self) -> Result<Resolution, ()>
Tries to simplify the expression into a <resolution>
value.
sourcefn to_number(&self) -> Result<CSSFloat, ()>
fn to_number(&self) -> Result<CSSFloat, ()>
Tries to simplify this expression into a <number>
value.
sourcefn to_percentage(&self) -> Result<CSSFloat, ()>
fn to_percentage(&self) -> Result<CSSFloat, ()>
Tries to simplify this expression into a <percentage>
value.
sourcepub fn math_function<'i>(
_: &ParserContext<'_>,
name: &CowRcStr<'i>,
location: SourceLocation,
) -> Result<MathFunction, ParseError<'i>>
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.
sourcepub fn parse_integer<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
) -> Result<CSSInteger, ParseError<'i>>
pub fn parse_integer<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSInteger, ParseError<'i>>
Convenience parsing function for integers.
sourcepub fn parse_length_or_percentage<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType,
function: MathFunction,
) -> Result<CalcLengthPercentage, ParseError<'i>>
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>
.
sourcepub fn parse_percentage<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
) -> Result<CSSFloat, ParseError<'i>>
pub fn parse_percentage<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSFloat, ParseError<'i>>
Convenience parsing function for percentages.
sourcepub fn parse_length<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType,
function: MathFunction,
) -> Result<CalcLengthPercentage, ParseError<'i>>
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>
.
sourcepub fn parse_number<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
) -> Result<CSSFloat, ParseError<'i>>
pub fn parse_number<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<CSSFloat, ParseError<'i>>
Convenience parsing function for <number>
.
sourcepub fn parse_angle<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
) -> Result<Angle, ParseError<'i>>
pub fn parse_angle<'i, 't>( context: &ParserContext<'_>, input: &mut Parser<'i, 't>, function: MathFunction, ) -> Result<Angle, ParseError<'i>>
Convenience parsing function for <angle>
.
sourcepub fn parse_time<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
clamping_mode: AllowedNumericType,
function: MathFunction,
) -> Result<Time, ParseError<'i>>
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>
.
sourcepub fn parse_resolution<'i, 't>(
context: &ParserContext<'_>,
input: &mut Parser<'i, 't>,
function: MathFunction,
) -> Result<Resolution, ParseError<'i>>
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>
impl<L: Clone> Clone for GenericCalcNode<L>
source§fn clone(&self) -> GenericCalcNode<L>
fn clone(&self) -> GenericCalcNode<L>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<L: Debug> Debug for GenericCalcNode<L>
impl<L: Debug> Debug for GenericCalcNode<L>
source§impl<'de, L> Deserialize<'de> for GenericCalcNode<L>where
L: Deserialize<'de>,
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>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl<L> MallocSizeOf for GenericCalcNode<L>where
L: MallocSizeOf,
impl<L> MallocSizeOf for GenericCalcNode<L>where
L: MallocSizeOf,
source§fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize
source§impl<L: PartialEq> PartialEq for GenericCalcNode<L>
impl<L: PartialEq> PartialEq for GenericCalcNode<L>
source§fn eq(&self, other: &GenericCalcNode<L>) -> bool
fn eq(&self, other: &GenericCalcNode<L>) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl<L> Serialize for GenericCalcNode<L>where
L: Serialize,
impl<L> Serialize for GenericCalcNode<L>where
L: Serialize,
source§impl<L> ToAnimatedZero for GenericCalcNode<L>where
L: ToAnimatedZero,
impl<L> ToAnimatedZero for GenericCalcNode<L>where
L: ToAnimatedZero,
source§fn to_animated_zero(&self) -> Result<Self, ()>
fn to_animated_zero(&self) -> Result<Self, ()>
source§impl<L: CalcNodeLeaf> ToCss for CalcNode<L>
impl<L: CalcNodeLeaf> ToCss for CalcNode<L>
source§impl<L> ToResolvedValue for GenericCalcNode<L>where
L: ToResolvedValue,
impl<L> ToResolvedValue for GenericCalcNode<L>where
L: ToResolvedValue,
§type ResolvedValue = GenericCalcNode<<L as ToResolvedValue>::ResolvedValue>
type ResolvedValue = GenericCalcNode<<L as ToResolvedValue>::ResolvedValue>
source§fn from_resolved_value(from: Self::ResolvedValue) -> Self
fn from_resolved_value(from: Self::ResolvedValue) -> Self
source§fn to_resolved_value(self, context: &Context<'_>) -> Self::ResolvedValue
fn to_resolved_value(self, context: &Context<'_>) -> Self::ResolvedValue
source§impl<L> ToShmem for GenericCalcNode<L>where
L: ToShmem,
impl<L> ToShmem for GenericCalcNode<L>where
L: ToShmem,
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>where
L: UnwindSafe + RefUnwindSafe,
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> 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