style/values/generics/
mod.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5//! Generic types that share their serialization implementations
6//! for both specified and computed values.
7
8use crate::Zero;
9use std::ops::Add;
10
11pub mod animation;
12pub mod background;
13pub mod basic_shape;
14pub mod border;
15#[path = "box.rs"]
16pub mod box_;
17pub mod calc;
18pub mod color;
19pub mod column;
20pub mod counters;
21pub mod easing;
22pub mod effects;
23pub mod flex;
24pub mod font;
25pub mod grid;
26pub mod image;
27pub mod length;
28pub mod motion;
29pub mod page;
30pub mod position;
31pub mod ratio;
32pub mod rect;
33pub mod size;
34pub mod svg;
35pub mod text;
36pub mod transform;
37pub mod ui;
38pub mod url;
39
40/// A wrapper of Non-negative values.
41#[derive(
42    Animate,
43    Clone,
44    ComputeSquaredDistance,
45    Copy,
46    Debug,
47    Deserialize,
48    Hash,
49    MallocSizeOf,
50    PartialEq,
51    PartialOrd,
52    SpecifiedValueInfo,
53    Serialize,
54    ToAnimatedZero,
55    ToComputedValue,
56    ToCss,
57    ToResolvedValue,
58    ToShmem,
59)]
60#[repr(transparent)]
61pub struct NonNegative<T>(pub T);
62
63impl<T: Add<Output = T>> Add<NonNegative<T>> for NonNegative<T> {
64    type Output = Self;
65
66    fn add(self, other: Self) -> Self {
67        NonNegative(self.0 + other.0)
68    }
69}
70
71impl<T: Zero> Zero for NonNegative<T> {
72    fn is_zero(&self) -> bool {
73        self.0.is_zero()
74    }
75
76    fn zero() -> Self {
77        NonNegative(T::zero())
78    }
79}
80
81/// A wrapper of greater-than-or-equal-to-one values.
82#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
83#[derive(
84    Animate,
85    Clone,
86    ComputeSquaredDistance,
87    Copy,
88    Debug,
89    MallocSizeOf,
90    PartialEq,
91    PartialOrd,
92    SpecifiedValueInfo,
93    ToAnimatedZero,
94    ToComputedValue,
95    ToCss,
96    ToResolvedValue,
97    ToShmem,
98)]
99#[repr(transparent)]
100pub struct GreaterThanOrEqualToOne<T>(pub T);
101
102/// A wrapper of values between zero and one.
103#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
104#[derive(
105    Animate,
106    Clone,
107    ComputeSquaredDistance,
108    Copy,
109    Debug,
110    Hash,
111    MallocSizeOf,
112    PartialEq,
113    PartialOrd,
114    SpecifiedValueInfo,
115    ToAnimatedZero,
116    ToComputedValue,
117    ToCss,
118    ToResolvedValue,
119    ToShmem,
120)]
121#[repr(transparent)]
122pub struct ZeroToOne<T>(pub T);
123
124/// A clip rect for clip and image-region
125#[allow(missing_docs)]
126#[derive(
127    Clone,
128    ComputeSquaredDistance,
129    Copy,
130    Debug,
131    MallocSizeOf,
132    PartialEq,
133    SpecifiedValueInfo,
134    ToAnimatedValue,
135    ToAnimatedZero,
136    ToComputedValue,
137    ToCss,
138    ToResolvedValue,
139    ToShmem,
140)]
141#[css(function = "rect", comma)]
142#[repr(C)]
143pub struct GenericClipRect<LengthOrAuto> {
144    pub top: LengthOrAuto,
145    pub right: LengthOrAuto,
146    pub bottom: LengthOrAuto,
147    pub left: LengthOrAuto,
148}
149
150pub use self::GenericClipRect as ClipRect;
151
152/// Either a clip-rect or `auto`.
153#[allow(missing_docs)]
154#[derive(
155    Animate,
156    Clone,
157    ComputeSquaredDistance,
158    Copy,
159    Debug,
160    MallocSizeOf,
161    Parse,
162    PartialEq,
163    SpecifiedValueInfo,
164    ToAnimatedValue,
165    ToAnimatedZero,
166    ToComputedValue,
167    ToCss,
168    ToResolvedValue,
169    ToShmem,
170)]
171#[repr(C, u8)]
172pub enum GenericClipRectOrAuto<R> {
173    Auto,
174    Rect(R),
175}
176
177pub use self::GenericClipRectOrAuto as ClipRectOrAuto;
178
179impl<L> ClipRectOrAuto<L> {
180    /// Returns the `auto` value.
181    #[inline]
182    pub fn auto() -> Self {
183        ClipRectOrAuto::Auto
184    }
185
186    /// Returns whether this value is the `auto` value.
187    #[inline]
188    pub fn is_auto(&self) -> bool {
189        matches!(*self, ClipRectOrAuto::Auto)
190    }
191}
192
193pub use page::PageSize;
194
195pub use text::NumberOrAuto;
196
197/// An optional value, much like `Option<T>`, but with a defined struct layout
198/// to be able to use it from C++ as well.
199///
200/// Note that this is relatively inefficient, struct-layout-wise, as you have
201/// one byte for the tag, but padding to the alignment of T. If you have
202/// multiple optional values and care about struct compactness, you might be
203/// better off "coalescing" the combinations into a parent enum. But that
204/// shouldn't matter for most use cases.
205#[allow(missing_docs)]
206#[derive(
207    Animate,
208    Clone,
209    ComputeSquaredDistance,
210    Copy,
211    Debug,
212    MallocSizeOf,
213    Parse,
214    PartialEq,
215    SpecifiedValueInfo,
216    ToAnimatedValue,
217    ToAnimatedZero,
218    ToComputedValue,
219    ToCss,
220    ToResolvedValue,
221    ToShmem,
222    Serialize,
223    Deserialize,
224)]
225#[repr(C, u8)]
226pub enum Optional<T> {
227    #[css(skip)]
228    None,
229    Some(T),
230}
231
232impl<T> Optional<T> {
233    /// Returns whether this value is present.
234    pub fn is_some(&self) -> bool {
235        matches!(*self, Self::Some(..))
236    }
237
238    /// Returns whether this value is not present.
239    pub fn is_none(&self) -> bool {
240        matches!(*self, Self::None)
241    }
242
243    /// Turns this Optional<> into a regular rust Option<>.
244    pub fn into_rust(self) -> Option<T> {
245        match self {
246            Self::Some(v) => Some(v),
247            Self::None => None,
248        }
249    }
250
251    /// Return a reference to the containing value, if any, as a plain rust
252    /// Option<>.
253    pub fn as_ref(&self) -> Option<&T> {
254        match *self {
255            Self::Some(ref v) => Some(v),
256            Self::None => None,
257        }
258    }
259
260    /// Return a mutable reference to the containing value, if any, as a plain
261    /// rust Option<>.
262    pub fn as_mut(&mut self) -> Option<&mut T> {
263        match *self {
264            Self::Some(ref mut v) => Some(v),
265            Self::None => None,
266        }
267    }
268}
269
270impl<T> From<Option<T>> for Optional<T> {
271    fn from(rust: Option<T>) -> Self {
272        match rust {
273            Some(t) => Self::Some(t),
274            None => Self::None,
275        }
276    }
277}