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;
17pub mod threadpool;
18mod unicode_block;
19
20use std::fs::File;
21use std::io::{BufWriter, Read};
22use std::path::Path;
23
24use ipc_channel::ipc::{IpcError, IpcSender};
25use log::{trace, warn};
26use malloc_size_of_derive::MallocSizeOf;
27use serde::{Deserialize, Serialize};
28use webrender_api::Epoch as WebRenderEpoch;
29
30pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
31where
32    T: for<'de> Deserialize<'de>,
33{
34    let path = config_dir.join(filename);
35    let display = path.display();
36
37    let mut file = match File::open(&path) {
38        Err(why) => {
39            warn!("couldn't open {}: {}", display, why);
40            return;
41        },
42        Ok(file) => file,
43    };
44
45    let mut string_buffer: String = String::new();
46    match file.read_to_string(&mut string_buffer) {
47        Err(why) => panic!("couldn't read from {}: {}", display, why),
48        Ok(_) => trace!("successfully read from {}", display),
49    }
50
51    match serde_json::from_str(&string_buffer) {
52        Ok(decoded_buffer) => *data = decoded_buffer,
53        Err(why) => warn!("Could not decode buffer{}", why),
54    }
55}
56
57pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
58where
59    T: Serialize,
60{
61    let path = config_dir.join(filename);
62    let display = path.display();
63
64    let mut file = match File::create(&path) {
65        Err(why) => panic!("couldn't create {}: {}", display, why),
66        Ok(file) => file,
67    };
68    let mut writer = BufWriter::new(&mut file);
69    serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
70    trace!("successfully wrote to {display}");
71}
72
73/// A struct for denoting the age of messages; prevents race conditions.
74#[derive(
75    Clone,
76    Copy,
77    Debug,
78    Default,
79    Deserialize,
80    Eq,
81    Hash,
82    Ord,
83    PartialEq,
84    PartialOrd,
85    Serialize,
86    MallocSizeOf,
87)]
88pub struct Epoch(pub u32);
89
90impl Epoch {
91    pub fn next(&self) -> Self {
92        Self(self.0 + 1)
93    }
94}
95
96impl From<Epoch> for WebRenderEpoch {
97    fn from(val: Epoch) -> Self {
98        WebRenderEpoch(val.0)
99    }
100}
101
102pub trait WebRenderEpochToU16 {
103    fn as_u16(&self) -> u16;
104}
105
106impl WebRenderEpochToU16 for WebRenderEpoch {
107    /// The value of this [`Epoch`] as a u16 value. Note that if this Epoch's
108    /// value is more than u16::MAX, then the return value will be modulo
109    /// u16::MAX.
110    fn as_u16(&self) -> u16 {
111        (self.0 % u16::MAX as u32) as u16
112    }
113}
114
115pub type IpcSendResult = Result<(), IpcError>;
116
117/// Abstraction of the ability to send a particular type of message,
118/// used by net_traits::ResourceThreads to ease the use its IpcSender sub-fields
119/// XXX: If this trait will be used more in future, some auto derive might be appealing
120pub trait IpcSend<T>
121where
122    T: serde::Serialize + for<'de> serde::Deserialize<'de>,
123{
124    /// send message T
125    fn send(&self, _: T) -> IpcSendResult;
126    /// get underlying sender
127    fn sender(&self) -> IpcSender<T>;
128}