mirror of
https://github.com/servo/servo.git
synced 2025-09-30 08:39:16 +01:00
storage: Move shared functionality to base (#39419)
Part of #39418. See that PR for a full description. Moves: - `read_json_from_file` - `write_json_to_file` - `IpcSendResult` - `IpcSend` Renames: - `CoreResourceThreadPool` to `ThreadPool` (shorter and more descriptive, as we use it for more than the core resource thread now) Signed-off-by: Ashwin Naren <arihant2math@gmail.com>
This commit is contained in:
parent
ba208b19cc
commit
f766b66a97
25 changed files with 254 additions and 98 deletions
|
@ -14,12 +14,62 @@ pub mod generic_channel;
|
|||
pub mod id;
|
||||
pub mod print_tree;
|
||||
pub mod text;
|
||||
pub mod threadpool;
|
||||
mod unicode_block;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufWriter, Read};
|
||||
use std::path::Path;
|
||||
|
||||
use ipc_channel::ipc::{IpcError, IpcSender};
|
||||
use log::{trace, warn};
|
||||
use malloc_size_of_derive::MallocSizeOf;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use webrender_api::Epoch as WebRenderEpoch;
|
||||
|
||||
pub fn read_json_from_file<T>(data: &mut T, config_dir: &Path, filename: &str)
|
||||
where
|
||||
T: for<'de> Deserialize<'de>,
|
||||
{
|
||||
let path = config_dir.join(filename);
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match File::open(&path) {
|
||||
Err(why) => {
|
||||
warn!("couldn't open {}: {}", display, why);
|
||||
return;
|
||||
},
|
||||
Ok(file) => file,
|
||||
};
|
||||
|
||||
let mut string_buffer: String = String::new();
|
||||
match file.read_to_string(&mut string_buffer) {
|
||||
Err(why) => panic!("couldn't read from {}: {}", display, why),
|
||||
Ok(_) => trace!("successfully read from {}", display),
|
||||
}
|
||||
|
||||
match serde_json::from_str(&string_buffer) {
|
||||
Ok(decoded_buffer) => *data = decoded_buffer,
|
||||
Err(why) => warn!("Could not decode buffer{}", why),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn write_json_to_file<T>(data: &T, config_dir: &Path, filename: &str)
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let path = config_dir.join(filename);
|
||||
let display = path.display();
|
||||
|
||||
let mut file = match File::create(&path) {
|
||||
Err(why) => panic!("couldn't create {}: {}", display, why),
|
||||
Ok(file) => file,
|
||||
};
|
||||
let mut writer = BufWriter::new(&mut file);
|
||||
serde_json::to_writer_pretty(&mut writer, data).expect("Could not serialize to file");
|
||||
trace!("successfully wrote to {display}");
|
||||
}
|
||||
|
||||
/// A struct for denoting the age of messages; prevents race conditions.
|
||||
#[derive(
|
||||
Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, MallocSizeOf,
|
||||
|
@ -50,3 +100,18 @@ impl WebRenderEpochToU16 for WebRenderEpoch {
|
|||
(self.0 % u16::MAX as u32) as u16
|
||||
}
|
||||
}
|
||||
|
||||
pub type IpcSendResult = Result<(), IpcError>;
|
||||
|
||||
/// Abstraction of the ability to send a particular type of message,
|
||||
/// used by net_traits::ResourceThreads to ease the use its IpcSender sub-fields
|
||||
/// XXX: If this trait will be used more in future, some auto derive might be appealing
|
||||
pub trait IpcSend<T>
|
||||
where
|
||||
T: serde::Serialize + for<'de> serde::Deserialize<'de>,
|
||||
{
|
||||
/// send message T
|
||||
fn send(&self, _: T) -> IpcSendResult;
|
||||
/// get underlying sender
|
||||
fn sender(&self) -> IpcSender<T>;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue