pub type CalcNode = GenericCalcNode<CalcLengthPercentageLeaf>;
Expand description
The computed version of a calc() node for <length-percentage>
values.
Aliased Type§
enum CalcNode {
Leaf(CalcLengthPercentageLeaf),
Negate(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
Invert(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
Sum(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
Product(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
MinMax(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>, MinMaxOp),
Clamp {
min: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
center: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
max: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
},
Round {
strategy: RoundingStrategy,
value: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
step: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
},
ModRem {
dividend: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
divisor: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>,
op: ModRemOp,
},
Hypot(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>),
Abs(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
Sign(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>),
}
Variants§
Leaf(CalcLengthPercentageLeaf)
A leaf node.
Negate(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)
A node that negates its child, e.g. Negate(1) == -1.
Invert(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)
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<CalcLengthPercentageLeaf>>)
A sum node, representing a + b + c
where a, b, and c are the
arguments.
Product(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>)
A product node, representing a * b * c
where a, b, and c are the
arguments.
MinMax(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>, MinMaxOp)
A min
or max
function.
Clamp
Fields
min: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The minimum value.
center: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The central value.
max: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The maximum value.
A clamp()
function.
Round
Fields
strategy: RoundingStrategy
The rounding strategy.
value: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The value to round.
step: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The step value.
A round()
function.
ModRem
Fields
dividend: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The dividend calculation.
divisor: Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>
The divisor calculation.
A mod()
or rem()
function.
Hypot(OwnedSlice<GenericCalcNode<CalcLengthPercentageLeaf>>)
A hypot()
function
Abs(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)
An abs()
function.
Sign(Box<GenericCalcNode<CalcLengthPercentageLeaf>, Global>)
A sign()
function.
Implementations§
source§impl<L: CalcNodeLeaf> CalcNode<L>
impl<L: CalcNodeLeaf> CalcNode<L>
sourcefn coerce_to_value(&mut self, value: f32)
fn coerce_to_value(&mut self, value: f32)
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).
sourcefn try_sum_in_place(&mut self, other: &Self) -> Result<(), ()>
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, ()>where
O: Fn(f32, f32) -> f32,
fn try_op<O>(&self, other: &Self, op: O) -> Result<Self, ()>where O: Fn(f32, f32) -> f32,
Tries to apply a generic arithmetic operator
sourcepub fn map(&mut self, op: impl FnMut(f32) -> f32)
pub fn map(&mut self, op: impl FnMut(f32) -> f32)
Map the value of this node with the given operation.
sourcepub fn map_leaves<O, F>(&self, map: F) -> CalcNode<O>where
O: CalcNodeLeaf,
F: FnMut(&L) -> O,
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.
fn map_leaves_internal<O, F>(&self, map: &mut F) -> CalcNode<O>where O: CalcNodeLeaf, F: FnMut(&L) -> O,
sourcepub fn resolve_map<F>(&self, leaf_to_output_fn: F) -> Result<L, ()>where
F: FnMut(&L) -> Result<L, ()>,
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.
fn resolve_internal<F>(&self, leaf_to_output_fn: &mut F) -> Result<L, ()>where F: FnMut(&L) -> Result<L, ()>,
fn is_negative_leaf(&self) -> bool
fn is_zero_leaf(&self) -> bool
fn is_infinite_leaf(&self) -> 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.
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<GenericCalcNode<L>> for GenericCalcNode<L>
impl<L: PartialEq> PartialEq<GenericCalcNode<L>> 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 ==
.