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