Skip to main content

servo_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;
14#[macro_use]
15pub mod id;
16pub mod print_tree;
17mod rope;
18pub mod text;
19pub mod threadboost;
20pub mod threadpool;
21mod unicode_block;
22
23use std::fs::File;
24use std::io::{BufReader, BufWriter};
25use std::path::Path;
26
27use log::{trace, warn};
28use malloc_size_of_derive::MallocSizeOf;
29pub use rope::{Rope, RopeChars, RopeIndex, RopeMovement, RopeSlice};
30use serde::{Deserialize, Serialize};
31use webrender_api::Epoch as WebRenderEpoch;
32
33pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
34where
35    T: for<'de> Deserialize<'de>,
36{
37    let path = config_dir.join(filename);
38    let display = path.display();
39
40    let file = match File::open(&path) {
41        Err(why) => {
42            warn!("couldn't open {}: {}", display, why);
43            return;
44        },
45        Ok(file) => file,
46    };
47    let buf_reader = BufReader::new(file);
48
49    match serde_json::from_reader(buf_reader) {
50        Ok(decoded_buffer) => *data = decoded_buffer,
51        Err(why) => warn!("Could not deserialize file {}", why),
52    }
53}
54
55pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
56where
57    T: Serialize,
58{
59    let path = config_dir.join(filename);
60    let display = path.display();
61
62    let mut file = match File::create(&path) {
63        Err(why) => panic!("couldn't create {}: {}", display, why),
64        Ok(file) => file,
65    };
66    let mut writer = BufWriter::new(&mut file);
67    serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
68    trace!("successfully wrote to {display}");
69}
70
71/// A struct for denoting the age of messages; prevents race conditions.
72#[derive(
73    Clone,
74    Copy,
75    Debug,
76    Default,
77    Deserialize,
78    Eq,
79    Hash,
80    Ord,
81    PartialEq,
82    PartialOrd,
83    Serialize,
84    MallocSizeOf,
85)]
86pub struct Epoch(pub u32);
87
88impl Epoch {
89    pub fn next(&self) -> Self {
90        Self(self.0 + 1)
91    }
92}
93
94impl From<Epoch> for WebRenderEpoch {
95    fn from(val: Epoch) -> Self {
96        WebRenderEpoch(val.0)
97    }
98}
99
100pub trait WebRenderEpochToU16 {
101    fn as_u16(&self) -> u16;
102}
103
104impl WebRenderEpochToU16 for WebRenderEpoch {
105    /// The value of this [`Epoch`] as a u16 value. Note that if this Epoch's
106    /// value is more than u16::MAX, then the return value will be modulo
107    /// u16::MAX.
108    fn as_u16(&self) -> u16 {
109        (self.0 % u16::MAX as u32) as u16
110    }
111}