base/
lib.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#![deny(unsafe_code)]
6
7//! A crate to hold very common types in Servo.
8//!
9//! You should almost never need to add a data type to this crate. Instead look for
10//! a more shared crate that has fewer dependents.
11
12pub mod cross_process_instant;
13pub mod generic_channel;
14pub mod id;
15pub mod print_tree;
16pub mod text;
17mod unicode_block;
18
19use malloc_size_of_derive::MallocSizeOf;
20use serde::{Deserialize, Serialize};
21use webrender_api::Epoch as WebRenderEpoch;
22
23/// A struct for denoting the age of messages; prevents race conditions.
24#[derive(
25    Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, MallocSizeOf,
26)]
27pub struct Epoch(pub u32);
28
29impl Epoch {
30    pub fn next(&mut self) {
31        self.0 += 1;
32    }
33}
34
35impl From<Epoch> for WebRenderEpoch {
36    fn from(val: Epoch) -> Self {
37        WebRenderEpoch(val.0)
38    }
39}
40
41pub trait WebRenderEpochToU16 {
42    fn as_u16(&self) -> u16;
43}
44
45impl WebRenderEpochToU16 for WebRenderEpoch {
46    /// The value of this [`Epoch`] as a u16 value. Note that if this Epoch's
47    /// value is more than u16::MAX, then the return value will be modulo
48    /// u16::MAX.
49    fn as_u16(&self) -> u16 {
50        (self.0 % u16::MAX as u32) as u16
51    }
52}