style/values/computed/
time.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//! Computed time values.
6
7use crate::values::CSSFloat;
8use crate::Zero;
9use std::fmt::{self, Write};
10use style_traits::{CssWriter, ToCss};
11
12/// A computed `<time>` value.
13#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue)]
14#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
15#[repr(C)]
16pub struct Time {
17    seconds: CSSFloat,
18}
19
20impl Time {
21    /// Creates a time value from a seconds amount.
22    pub fn from_seconds(seconds: CSSFloat) -> Self {
23        Time { seconds }
24    }
25
26    /// Returns the amount of seconds this time represents.
27    #[inline]
28    pub fn seconds(&self) -> CSSFloat {
29        self.seconds
30    }
31}
32
33impl ToCss for Time {
34    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
35    where
36        W: Write,
37    {
38        self.seconds().to_css(dest)?;
39        dest.write_char('s')
40    }
41}
42
43impl Zero for Time {
44    fn zero() -> Self {
45        Self::from_seconds(0.0)
46    }
47
48    fn is_zero(&self) -> bool {
49        self.seconds == 0.
50    }
51}