style/values/computed/
time.rs1use crate::derives::*;
8use crate::typed_om::{NumericType, NumericValue, ToTyped, TypedValue, UnitValue};
9use crate::values::CSSFloat;
10use crate::Zero;
11use std::fmt::{self, Write};
12use std::ops::AddAssign;
13use style_traits::{CssString, CssWriter, ToCss};
14use thin_vec::ThinVec;
15
16#[derive(
18 Animate,
19 Clone,
20 Copy,
21 Debug,
22 Deserialize,
23 MallocSizeOf,
24 PartialEq,
25 PartialOrd,
26 Serialize,
27 ToAnimatedZero,
28 ToResolvedValue,
29)]
30#[repr(C)]
31pub struct Time {
32 seconds: CSSFloat,
33}
34
35impl Time {
36 pub fn from_seconds(seconds: CSSFloat) -> Self {
38 Time { seconds }
39 }
40
41 #[inline]
43 pub fn seconds(&self) -> CSSFloat {
44 self.seconds
45 }
46}
47
48impl ToCss for Time {
49 fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
50 where
51 W: Write,
52 {
53 self.seconds().to_css(dest)?;
54 dest.write_char('s')
55 }
56}
57
58impl ToTyped for Time {
59 fn to_typed(&self, dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
60 dest.push(TypedValue::Numeric(NumericValue::Unit(UnitValue {
61 numeric_type: NumericType::time(),
62 value: self.seconds(),
63 unit: CssString::from("s"),
64 })));
65 Ok(())
66 }
67}
68
69impl Zero for Time {
70 fn zero() -> Self {
71 Self::from_seconds(0.0)
72 }
73
74 fn is_zero(&self) -> bool {
75 self.seconds == 0.
76 }
77}
78
79impl AddAssign for Time {
80 fn add_assign(&mut self, rhs: Self) {
81 self.seconds += rhs.seconds
82 }
83}