taffy/style/
compact_length.rs1use super::LengthPercentage;
4use crate::style_helpers::{
5 FromFr, FromLength, FromPercent, TaffyAuto, TaffyFitContent, TaffyMaxContent, TaffyMinContent, TaffyZero,
6};
7
8mod compat {
12 #![allow(unsafe_code)]
13 #![allow(unknown_lints)]
14 #![allow(unnecessary_transmutes)]
15
16 pub const fn f32_to_bits(val: f32) -> u32 {
18 unsafe { core::mem::transmute(val) }
20 }
21 pub const fn f32_from_bits(v: u32) -> f32 {
23 unsafe { core::mem::transmute(v) }
25 }
26
27 #[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 #[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#[cfg(target_pointer_width = "64")]
47mod inner {
48 use super::compat::{f32_from_bits, f32_to_bits, tag_ptr};
49
50 const TAG_MASK: usize = 0b11111111;
52 const CALC_TAG_MASK: usize = 0b111;
54 #[derive(Copy, Clone, Debug, PartialEq)]
62 pub(super) struct CompactLengthInner {
63 tagged_ptr: *const (),
65 }
66 impl CompactLengthInner {
67 #[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 #[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 #[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 #[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 #[inline(always)]
97 pub(super) fn tag(self) -> usize {
98 (self.tagged_ptr as usize) & TAG_MASK
99 }
100
101 #[inline(always)]
103 pub(super) fn ptr(self) -> *const () {
104 self.tagged_ptr
105 }
106
107 #[inline(always)]
109 pub(super) fn value(self) -> f32 {
110 f32_from_bits((self.tagged_ptr as usize >> 32) as u32)
111 }
112
113 #[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 #[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#[cfg(target_pointer_width = "32")]
131mod inner {
132 use super::compat::{f32_from_bits, f32_to_bits};
133
134 #[derive(Copy, Clone, Debug, PartialEq)]
138 pub(super) struct CompactLengthInner {
139 tag: usize,
141 ptr: *const (),
143 }
144
145 impl CompactLengthInner {
146 #[inline(always)]
148 pub(super) fn from_ptr(ptr: *const (), tag: usize) -> Self {
149 Self { ptr, tag }
150 }
151
152 #[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 #[inline(always)]
160 pub(super) const fn from_tag(tag: usize) -> Self {
161 Self { ptr: 0 as *const (), tag }
162 }
163
164 #[inline(always)]
166 #[cfg(feature = "calc")]
167 pub(super) fn calc_tag(self) -> usize {
168 self.tag
169 }
170
171 #[inline(always)]
173 pub(super) fn tag(self) -> usize {
174 self.tag
175 }
176
177 #[inline(always)]
179 pub(super) fn ptr(self) -> *const () {
180 self.ptr
181 }
182
183 #[inline(always)]
185 pub(super) fn value(self) -> f32 {
186 f32_from_bits(self.ptr as u32)
187 }
188
189 #[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 #[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#[derive(Copy, Clone, PartialEq, Debug)]
209#[repr(transparent)]
210pub struct CompactLength(CompactLengthInner);
211
212impl CompactLength {
213 #[cfg(feature = "calc")]
215 pub const CALC_TAG: usize = 0b000;
216 pub const LENGTH_TAG: usize = 0b0000_0001;
218 pub const PERCENT_TAG: usize = 0b0000_0010;
220 pub const AUTO_TAG: usize = 0b0000_0011;
222 pub const FR_TAG: usize = 0b0000_0100;
224 pub const MIN_CONTENT_TAG: usize = 0b00000111;
226 pub const MAX_CONTENT_TAG: usize = 0b00001111;
228 pub const FIT_CONTENT_PX_TAG: usize = 0b00010111;
230 pub const FIT_CONTENT_PERCENT_TAG: usize = 0b00011111;
232}
233
234impl CompactLength {
235 #[inline(always)]
238 pub const fn length(val: f32) -> Self {
239 Self(CompactLengthInner::from_val(val, Self::LENGTH_TAG))
240 }
241
242 #[inline(always)]
246 pub const fn percent(val: f32) -> Self {
247 Self(CompactLengthInner::from_val(val, Self::PERCENT_TAG))
248 }
249
250 #[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 #[inline(always)]
265 pub const fn auto() -> Self {
266 Self(CompactLengthInner::from_tag(Self::AUTO_TAG))
267 }
268
269 #[inline(always)]
273 pub const fn fr(val: f32) -> Self {
274 Self(CompactLengthInner::from_val(val, Self::FR_TAG))
275 }
276
277 #[inline(always)]
280 pub const fn min_content() -> Self {
281 Self(CompactLengthInner::from_tag(Self::MIN_CONTENT_TAG))
282 }
283
284 #[inline(always)]
287 pub const fn max_content() -> Self {
288 Self(CompactLengthInner::from_tag(Self::MAX_CONTENT_TAG))
289 }
290
291 #[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 #[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 #[inline(always)]
321 pub fn tag(self) -> usize {
322 self.0.tag()
323 }
324
325 #[inline(always)]
328 pub fn value(self) -> f32 {
329 self.0.value()
330 }
331
332 #[inline(always)]
334 #[cfg(feature = "calc")]
335 pub fn calc_value(self) -> *const () {
336 self.0.ptr()
337 }
338
339 #[inline(always)]
341 #[cfg(feature = "calc")]
342 pub fn is_calc(self) -> bool {
343 self.0.calc_tag() == 0
344 }
345
346 #[inline(always)]
348 pub fn is_zero(self) -> bool {
349 self.0 == Self::ZERO.0
350 }
351
352 #[inline(always)]
354 pub fn is_length_or_percentage(self) -> bool {
355 matches!(self.tag(), Self::LENGTH_TAG | Self::PERCENT_TAG)
356 }
357
358 #[inline(always)]
360 pub fn is_auto(self) -> bool {
361 self.tag() == Self::AUTO_TAG
362 }
363
364 #[inline(always)]
366 pub fn is_min_content(self) -> bool {
367 matches!(self.tag(), Self::MIN_CONTENT_TAG)
368 }
369
370 #[inline(always)]
372 pub fn is_max_content(self) -> bool {
373 matches!(self.tag(), Self::MAX_CONTENT_TAG)
374 }
375
376 #[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 #[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 #[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 #[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 #[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 #[inline(always)]
423 pub fn is_fr(self) -> bool {
424 self.tag() == Self::FR_TAG
425 }
426
427 #[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 #[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 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}