-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathref_cell_hash_map.rs
More file actions
31 lines (23 loc) · 855 Bytes
/
Copy pathref_cell_hash_map.rs
File metadata and controls
31 lines (23 loc) · 855 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::{cell::RefCell, path::PathBuf};
use std::collections::HashMap;
use anyhow::Result;
use crate::graph::{FileId};
use super::{FileHandle, FileMap};
#[derive(Default)]
pub struct RefCellHashMapFileMap(RefCell<HashMap<FileId, FileHandle>>);
impl FileMap for RefCellHashMapFileMap {
fn new() -> Self where Self : Sized {
RefCellHashMapFileMap(RefCell::new(HashMap::with_capacity(16)))
}
fn read_file(&self, file_id: FileId, file_path: &PathBuf) -> Result<FileHandle> {
let file = { self.0.borrow().get(&file_id).map(Clone::clone) };
match file {
Some(handle) => Ok(handle),
None => {
let handle = FileHandle::from_path(file_path)?;
let _ = self.0.borrow_mut().insert(file_id, handle.clone());
Ok(handle)
}
}
}
}