1#![deny(unsafe_code)]
6
7pub 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#[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 fn as_u16(&self) -> u16 {
111 (self.0 % u16::MAX as u32) as u16
112 }
113}
114
115pub type IpcSendResult = Result<(), IpcError>;
116
117pub trait IpcSend<T>
121where
122 T: serde::Serialize + for<'de> serde::Deserialize<'de>,
123{
124 fn send(&self, _: T) -> IpcSendResult;
126 fn sender(&self) -> IpcSender<T>;
128}