taffy/style/
compact_length.rs

1//! A tagged-pointer abstraction that allows size styles in Taffy to be represented
2//! in just 64 bits. Wrapped by types in the `super::dimension` and `super::grid` modules.
3use super::LengthPercentage;
4use crate::style_helpers::{
5    FromFr, FromLength, FromPercent, TaffyAuto, TaffyFitContent, TaffyMaxContent, TaffyMinContent, TaffyZero,
6};
7
8/// Note: these two functions are copied directly from the std (core) library. But by duplicating them
9/// here we can reduce MSRV from 1.84 all the way down to 1.65 while retaining const constructors and
10/// strict pointer provenance
11mod compat {
12    #![allow(unsafe_code)]
13    #![allow(unknown_lints)]
14    #![allow(unnecessary_transmutes)]
15
16    /// Raw transmutation from `f32` to `u32`.
17    pub const fn f32_to_bits(val: f32) -> u32 {
18        // SAFETY: `u32` is a plain old datatype so we can always transmute to it.
19        unsafe { core::mem::transmute(val) }
20    }
21    /// Raw transmutation from `u32` to `f32`.
22    pub const fn f32_from_bits(v: u32) -> f32 {
23        // SAFETY: `u32` is a plain old datatype so we can always transmute from it.
24        unsafe { core::mem::transmute(v) }
25    }
26
27    /// Tag a pointer preserving provenance (requires Rust 1.84)
28    #[inline(always)]
29    #[cfg(all(target_pointer_width = "64", feature = "strict_provenance"))]
30    pub fn tag_ptr(ptr: *const (), tag: usize) -> *const () {
31        ptr.map_addr(|a| a | tag)
32    }
33
34    /// Tag a pointer exposing provenance (works back to Rust 1.0)
35    #[inline(always)]
36    #[cfg(all(target_pointer_width = "64", not(feature = "strict_provenance")))]
37    pub fn tag_ptr(ptr: *const (), tag: usize) -> *const () {
38        (ptr as usize | tag) as *const ()
39    }
40}
41
42#[cfg(not(any(target_pointer_width = "32", target_pointer_width = "64")))]
43std::compile_error!("Taffy only supports targets with a pointer width of 32 or 64 bits");
44
45/// CompactLengthInner implementation for 64 bit platforms
46#[cfg(target_pointer_width = "64")]
47mod inner {
48    use super::compat::{f32_from_bits, f32_to_bits, tag_ptr};
49
50    /// The low byte (8 bits)
51    const TAG_MASK: usize = 0b11111111;
52    /// The low 3 bits
53    const CALC_TAG_MASK: usize = 0b111;
54    // The high 63 bits
55    // const CALC_PTR_MASK: usize = usize::MAX ^ 0b111;
56
57    /// On 64 bit platforms the tag, value and pointer are packed into a single 64 bit pointer
58    ///
59    /// The tagged pointer always has a tag and may contain an f32 value or a pointer
60    /// (or neither) depending on the variant indicated by the tag.
61    #[derive(Copy, Clone, Debug, PartialEq)]
62    pub(super) struct CompactLengthInner {
63        /// The tagged pointer
64        tagged_ptr: *const (),
65    }
66    impl CompactLengthInner {
67        /// Construct a `CompactLengthInner` from a tag and pointer
68        #[inline(always)]
69        pub(super) fn from_ptr(ptr: *const (), tag: usize) -> Self {
70            let tagged_ptr = tag_ptr(ptr, tag);
71            Self { tagged_ptr }
72        }
73
74        /// Construct a `CompactLengthInner` from a tag and numeric value
75        #[inline(always)]
76        pub(super) const fn from_val(val: f32, tag: usize) -> Self {
77            let tagged_ptr = (((f32_to_bits(val) as usize) << 32) | tag) as *const ();
78            Self { tagged_ptr }
79        }
80
81        /// Construct a `CompactLengthInner` from only a tag
82        #[inline(always)]
83        pub(super) const fn from_tag(tag: usize) -> Self {
84            let tagged_ptr = tag as *const ();
85            Self { tagged_ptr }
86        }
87
88        /// Get the calc tag (low 3 bits)
89        #[inline(always)]
90        #[cfg(feature = "calc")]
91        pub(super) fn calc_tag(self) -> usize {
92            (self.tagged_ptr as usize) & CALC_TAG_MASK
93        }
94
95        /// Get the general tag (low 8 bits)
96        #[inline(always)]
97        pub(super) fn tag(self) -> usize {
98            (self.tagged_ptr as usize) & TAG_MASK
99        }
100
101        /// Get the pointer value
102        #[inline(always)]
103        pub(super) fn ptr(self) -> *const () {
104            self.tagged_ptr
105        }
106
107        /// Get the numeric value
108        #[inline(always)]
109        pub(super) fn value(self) -> f32 {
110            f32_from_bits((self.tagged_ptr as usize >> 32) as u32)
111        }
112
113        /// Get the serialized value.
114        #[inline(always)]
115        #[cfg(feature = "serde")]
116        pub(super) fn serialized(self) -> u64 {
117            (self.tagged_ptr as usize as u64).rotate_left(32)
118        }
119
120        /// Derialized from a value.
121        #[inline(always)]
122        #[cfg(feature = "serde")]
123        pub(super) fn from_serialized(value: u64) -> Self {
124            Self { tagged_ptr: value.rotate_right(32) as usize as *const () }
125        }
126    }
127}
128
129/// CompactLengthInner implementation for 32 bit platforms
130#[cfg(target_pointer_width = "32")]
131mod inner {
132    use super::compat::{f32_from_bits, f32_to_bits};
133
134    /// On 32 bit platforms the tag is stored separately.
135    /// Either an f32 value or a pointer (or neither) are packed into the ptr field
136    /// depending on the variant indicated by the tag
137    #[derive(Copy, Clone, Debug, PartialEq)]
138    pub(super) struct CompactLengthInner {
139        /// The tag indicating what kind of value we are storing
140        tag: usize,
141        /// The pointer of numeric value
142        ptr: *const (),
143    }
144
145    impl CompactLengthInner {
146        /// Construct a `CompactLengthInner` from a tag and pointer
147        #[inline(always)]
148        pub(super) fn from_ptr(ptr: *const (), tag: usize) -> Self {
149            Self { ptr, tag }
150        }
151
152        /// Construct a `CompactLengthInner` from a tag and numeric value
153        #[inline(always)]
154        pub(super) const fn from_val(val: f32, tag: usize) -> Self {
155            Self { ptr: f32_to_bits(val) as usize as *const (), tag }
156        }
157
158        /// Construct a `CompactLengthInner` from only a tag
159        #[inline(always)]
160        pub(super) const fn from_tag(tag: usize) -> Self {
161            Self { ptr: 0 as *const (), tag }
162        }
163
164        /// Get the calc tag (low 3 bits)
165        #[inline(always)]
166        #[cfg(feature = "calc")]
167        pub(super) fn calc_tag(self) -> usize {
168            self.tag
169        }
170
171        /// Get the general tag (low 8 bits)
172        #[inline(always)]
173        pub(super) fn tag(self) -> usize {
174            self.tag
175        }
176
177        /// Get the pointer value
178        #[inline(always)]
179        pub(super) fn ptr(self) -> *const () {
180            self.ptr
181        }
182
183        /// Get the numeric value
184        #[inline(always)]
185        pub(super) fn value(self) -> f32 {
186            f32_from_bits(self.ptr as u32)
187        }
188
189        /// Get the serialized value.
190        #[inline(always)]
191        #[cfg(feature = "serde")]
192        pub(super) fn serialized(self) -> u64 {
193            (self.tag as u64) << 32 | (self.ptr as u64)
194        }
195
196        /// Derialized from a value.
197        #[inline(always)]
198        #[cfg(feature = "serde")]
199        pub(super) fn from_serialized(value: u64) -> Self {
200            Self { tag: (value >> 32) as usize, ptr: (value & 0xFFFFFFFF) as usize as *const () }
201        }
202    }
203}
204
205use inner::CompactLengthInner;
206
207/// A representation of a length as a compact 64-bit tagged pointer
208#[derive(Copy, Clone, PartialEq, Debug)]
209#[repr(transparent)]
210pub struct CompactLength(CompactLengthInner);
211
212impl CompactLength {
213    /// The tag indicating a calc() value
214    #[cfg(feature = "calc")]
215    pub const CALC_TAG: usize = 0b000;
216    /// The tag indicating a length value
217    pub const LENGTH_TAG: usize = 0b0000_0001;
218    /// The tag indicating a percentage value
219    pub const PERCENT_TAG: usize = 0b0000_0010;
220    /// The tag indicating an auto value
221    pub const AUTO_TAG: usize = 0b0000_0011;
222    /// The tag indicating an fr value
223    pub const FR_TAG: usize = 0b0000_0100;
224    /// The tag indicating a min-content value
225    pub const MIN_CONTENT_TAG: usize = 0b00000111;
226    /// The tag indicating a max-content value
227    pub const MAX_CONTENT_TAG: usize = 0b00001111;
228    /// The tag indicating a fit-content value with px limit
229    pub const FIT_CONTENT_PX_TAG: usize = 0b00010111;
230    /// The tag indicating a fit-content value with percent limit
231    pub const FIT_CONTENT_PERCENT_TAG: usize = 0b00011111;
232}
233
234impl CompactLength {
235    /// An absolute length in some abstract units. Users of Taffy may define what they correspond
236    /// to in their application (pixels, logical pixels, mm, etc) as they see fit.
237    #[inline(always)]
238    pub const fn length(val: f32) -> Self {
239        Self(CompactLengthInner::from_val(val, Self::LENGTH_TAG))
240    }
241
242    /// A percentage length relative to the size of the containing block.
243    ///
244    /// **NOTE: percentages are represented as a f32 value in the range [0.0, 1.0] NOT the range [0.0, 100.0]**
245    #[inline(always)]
246    pub const fn percent(val: f32) -> Self {
247        Self(CompactLengthInner::from_val(val, Self::PERCENT_TAG))
248    }
249
250    /// A `calc()` value. The value passed here is treated as an opaque handle to
251    /// the actual calc representation and may be a pointer, index, etc.
252    ///
253    /// The low 3 bits are used as a tag value and will be returned as 0.
254    #[inline]
255    #[cfg(feature = "calc")]
256    pub fn calc(ptr: *const ()) -> Self {
257        assert_ne!(ptr as u64, 0);
258        assert_eq!(ptr as u64 & 0b111, 0);
259        Self(CompactLengthInner::from_ptr(ptr, Self::CALC_TAG))
260    }
261
262    /// The dimension should be automatically computed according to algorithm-specific rules
263    /// regarding the default size of boxes.
264    #[inline(always)]
265    pub const fn auto() -> Self {
266        Self(CompactLengthInner::from_tag(Self::AUTO_TAG))
267    }
268
269    /// The dimension as a fraction of the total available grid space (`fr` units in CSS)
270    /// Specified value is the numerator of the fraction. Denominator is the sum of all fraction specified in that grid dimension
271    /// Spec: <https://www.w3.org/TR/css3-grid-layout/#fr-unit>
272    #[inline(always)]
273    pub const fn fr(val: f32) -> Self {
274        Self(CompactLengthInner::from_val(val, Self::FR_TAG))
275    }
276
277    /// The size should be the "min-content" size.
278    /// This is the smallest size that can fit the item's contents with ALL soft line-wrapping opportunities taken
279    #[inline(always)]
280    pub const fn min_content() -> Self {
281        Self(CompactLengthInner::from_tag(Self::MIN_CONTENT_TAG))
282    }
283
284    /// The size should be the "max-content" size.
285    /// This is the smallest size that can fit the item's contents with NO soft line-wrapping opportunities taken
286    #[inline(always)]
287    pub const fn max_content() -> Self {
288        Self(CompactLengthInner::from_tag(Self::MAX_CONTENT_TAG))
289    }
290
291    /// The size should be computed according to the "fit content" formula:
292    ///    `max(min_content, min(max_content, limit))`
293    /// where:
294    ///    - `min_content` is the [min-content](Self::min_content) size
295    ///    - `max_content` is the [max-content](Self::max_content) size
296    ///    - `limit` is a LENGTH value passed to this function
297    ///
298    /// The effect of this is that the item takes the size of `limit` clamped
299    /// by the min-content and max-content sizes.
300    #[inline(always)]
301    pub const fn fit_content_px(limit: f32) -> Self {
302        Self(CompactLengthInner::from_val(limit, Self::FIT_CONTENT_PX_TAG))
303    }
304
305    /// The size should be computed according to the "fit content" formula:
306    ///    `max(min_content, min(max_content, limit))`
307    /// where:
308    ///    - `min_content` is the [min-content](Self::min_content) size
309    ///    - `max_content` is the [max-content](Self::max_content) size
310    ///    - `limit` is a PERCENTAGE value passed to this function
311    ///
312    /// The effect of this is that the item takes the size of `limit` clamped
313    /// by the min-content and max-content sizes.
314    #[inline(always)]
315    pub const fn fit_content_percent(limit: f32) -> Self {
316        Self(CompactLengthInner::from_val(limit, Self::FIT_CONTENT_PERCENT_TAG))
317    }
318
319    /// Get the primary tag
320    #[inline(always)]
321    pub fn tag(self) -> usize {
322        self.0.tag()
323    }
324
325    /// Get the numeric value associated with the `CompactLength`
326    /// (e.g. the pixel value for a LENGTH variant)
327    #[inline(always)]
328    pub fn value(self) -> f32 {
329        self.0.value()
330    }
331
332    /// Get the calc pointer of the `CompactLength`
333    #[inline(always)]
334    #[cfg(feature = "calc")]
335    pub fn calc_value(self) -> *const () {
336        self.0.ptr()
337    }
338
339    /// Returns true if the value is 0 px
340    #[inline(always)]
341    #[cfg(feature = "calc")]
342    pub fn is_calc(self) -> bool {
343        self.0.calc_tag() == 0
344    }
345
346    /// Returns true if the value is 0 px
347    #[inline(always)]
348    pub fn is_zero(self) -> bool {
349        self.0 == Self::ZERO.0
350    }
351
352    /// Returns true if the value is a length or percentage value
353    #[inline(always)]
354    pub fn is_length_or_percentage(self) -> bool {
355        matches!(self.tag(), Self::LENGTH_TAG | Self::PERCENT_TAG)
356    }
357
358    /// Returns true if the value is auto
359    #[inline(always)]
360    pub fn is_auto(self) -> bool {
361        self.tag() == Self::AUTO_TAG
362    }
363
364    /// Returns true if the value is min-content
365    #[inline(always)]
366    pub fn is_min_content(self) -> bool {
367        matches!(self.tag(), Self::MIN_CONTENT_TAG)
368    }
369
370    /// Returns true if the value is max-content
371    #[inline(always)]
372    pub fn is_max_content(self) -> bool {
373        matches!(self.tag(), Self::MAX_CONTENT_TAG)
374    }
375
376    /// Returns true if the value is a fit-content(...) value
377    #[inline(always)]
378    pub fn is_fit_content(self) -> bool {
379        matches!(self.tag(), Self::FIT_CONTENT_PX_TAG | Self::FIT_CONTENT_PERCENT_TAG)
380    }
381
382    /// Returns true if the value is max-content or a fit-content(...) value
383    #[inline(always)]
384    pub fn is_max_or_fit_content(self) -> bool {
385        matches!(self.tag(), Self::MAX_CONTENT_TAG | Self::FIT_CONTENT_PX_TAG | Self::FIT_CONTENT_PERCENT_TAG)
386    }
387
388    /// Returns true if the max track sizing function is `MaxContent`, `FitContent` or `Auto` else false.
389    /// "In all cases, treat auto and fit-content() as max-content, except where specified otherwise for fit-content()."
390    /// See: <https://www.w3.org/TR/css-grid-1/#algo-terms>
391    #[inline(always)]
392    pub fn is_max_content_alike(&self) -> bool {
393        matches!(
394            self.tag(),
395            CompactLength::AUTO_TAG
396                | CompactLength::MAX_CONTENT_TAG
397                | CompactLength::FIT_CONTENT_PX_TAG
398                | CompactLength::FIT_CONTENT_PERCENT_TAG
399        )
400    }
401
402    /// Returns true if the min track sizing function is `MinContent` or `MaxContent`, else false.
403    #[inline(always)]
404    pub fn is_min_or_max_content(&self) -> bool {
405        matches!(self.tag(), Self::MIN_CONTENT_TAG | Self::MAX_CONTENT_TAG)
406    }
407
408    /// Returns true if the value is auto, min-content, max-content, or fit-content(...)
409    #[inline(always)]
410    pub fn is_intrinsic(self) -> bool {
411        matches!(
412            self.tag(),
413            Self::AUTO_TAG
414                | Self::MIN_CONTENT_TAG
415                | Self::MAX_CONTENT_TAG
416                | Self::FIT_CONTENT_PX_TAG
417                | Self::FIT_CONTENT_PERCENT_TAG
418        )
419    }
420
421    /// Returns true if the value is and fr value
422    #[inline(always)]
423    pub fn is_fr(self) -> bool {
424        self.tag() == Self::FR_TAG
425    }
426
427    /// Whether the track sizing functions depends on the size of the parent node
428    #[inline(always)]
429    pub fn uses_percentage(self) -> bool {
430        #[cfg(feature = "calc")]
431        {
432            matches!(self.tag(), CompactLength::PERCENT_TAG | CompactLength::FIT_CONTENT_PERCENT_TAG) || self.is_calc()
433        }
434        #[cfg(not(feature = "calc"))]
435        {
436            matches!(self.tag(), CompactLength::PERCENT_TAG | CompactLength::FIT_CONTENT_PERCENT_TAG)
437        }
438    }
439
440    /// Resolve percentage values against the passed parent_size, returning Some(value)
441    /// Non-percentage values always return None.
442    #[inline(always)]
443    pub fn resolved_percentage_size(
444        self,
445        parent_size: f32,
446        calc_resolver: impl Fn(*const (), f32) -> f32,
447    ) -> Option<f32> {
448        match self.tag() {
449            CompactLength::PERCENT_TAG => Some(self.value() * parent_size),
450            #[cfg(feature = "calc")]
451            _ if self.is_calc() => Some(calc_resolver(self.0.ptr(), parent_size)),
452            _ => None,
453        }
454    }
455}
456
457impl TaffyZero for CompactLength {
458    const ZERO: Self = Self::length(0.0);
459}
460impl TaffyAuto for CompactLength {
461    const AUTO: Self = Self::auto();
462}
463impl TaffyMinContent for CompactLength {
464    const MIN_CONTENT: Self = Self::min_content();
465}
466impl TaffyMaxContent for CompactLength {
467    const MAX_CONTENT: Self = Self::max_content();
468}
469impl FromLength for CompactLength {
470    fn from_length<Input: Into<f32> + Copy>(value: Input) -> Self {
471        Self::length(value.into())
472    }
473}
474impl FromPercent for CompactLength {
475    fn from_percent<Input: Into<f32> + Copy>(value: Input) -> Self {
476        Self::percent(value.into())
477    }
478}
479impl FromFr for CompactLength {
480    fn from_fr<Input: Into<f32> + Copy>(value: Input) -> Self {
481        Self::fr(value.into())
482    }
483}
484impl TaffyFitContent for CompactLength {
485    fn fit_content(lp: LengthPercentage) -> Self {
486        let value = lp.0.value();
487        match lp.0.tag() {
488            Self::LENGTH_TAG => Self::fit_content_px(value),
489            Self::PERCENT_TAG => Self::fit_content_percent(value),
490            _ => unreachable!(),
491        }
492    }
493}
494
495#[cfg(feature = "serde")]
496impl serde::Serialize for CompactLength {
497    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
498    where
499        S: serde::Serializer,
500    {
501        #[cfg(feature = "calc")]
502        {
503            if self.tag() == Self::CALC_TAG {
504                Err(serde::ser::Error::custom("Cannot serialize Calc value"))
505            } else {
506                serializer.serialize_u64(self.0.serialized())
507            }
508        }
509
510        #[cfg(not(feature = "calc"))]
511        serializer.serialize_u64(self.0.serialized())
512    }
513}
514
515#[cfg(feature = "serde")]
516impl<'de> serde::Deserialize<'de> for CompactLength {
517    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
518    where
519        D: serde::Deserializer<'de>,
520    {
521        let bits: u64 = u64::deserialize(deserializer)?;
522        let value = Self(CompactLengthInner::from_serialized(bits));
523        // Note: validation intentionally excludes the CALC_TAG as deserializing calc() values is not supported
524        if matches!(
525            value.tag(),
526            CompactLength::LENGTH_TAG
527                | CompactLength::PERCENT_TAG
528                | CompactLength::AUTO_TAG
529                | CompactLength::MIN_CONTENT_TAG
530                | CompactLength::MAX_CONTENT_TAG
531                | CompactLength::FIT_CONTENT_PX_TAG
532                | CompactLength::FIT_CONTENT_PERCENT_TAG
533                | CompactLength::FR_TAG
534        ) {
535            Ok(value)
536        } else {
537            Err(serde::de::Error::custom("Cannot deserialize Calc value"))
538        }
539    }
540}