style/values/computed/
time.rs1use crate::derives::*;
8use crate::values::CSSFloat;
9use crate::Zero;
10use std::fmt::{self, Write};
11use style_traits::{CssString, CssWriter, NumericValue, ToCss, ToTyped, TypedValue, UnitValue};
12use thin_vec::ThinVec;
13
14#[derive(Animate, Clone, Copy, Debug, MallocSizeOf, PartialEq, PartialOrd, ToResolvedValue)]
16#[cfg_attr(feature = "servo", derive(Deserialize, Serialize))]
17#[repr(C)]
18pub struct Time {
19 seconds: CSSFloat,
20}
21
22impl Time {
23 pub fn from_seconds(seconds: CSSFloat) -> Self {
25 Time { seconds }
26 }
27
28 #[inline]
30 pub fn seconds(&self) -> CSSFloat {
31 self.seconds
32 }
33}
34
35impl ToCss for Time {
36 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
37 where
38 W: Write,
39 {
40 self.seconds().to_css(dest)?;
41 dest.write_char('s')
42 }
43}
44
45impl ToTyped for Time {
46 fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
47 dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
48 value: self.seconds(),
49 unit: CssString::from("s"),
50 })));
51 Ok(())
52 }
53}
54
55impl Zero for Time {
56 fn zero() -> Self {
57 Self::from_seconds(0.0)
58 }
59
60 fn is_zero(&self) -> bool {
61 self.seconds == 0.
62 }
63}