Skip to content

Commit a491ef2

Browse files
committed
Bump dashmap and fix lifetime issue
Fixes parcel-bundler#83
1 parent e45e495 commit a491ef2

File tree

2 files changed

+124
-45
lines changed

2 files changed

+124
-45
lines changed

Cargo.lock

Lines changed: 101 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/bundler.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,37 @@ pub trait SourceProvider: Send + Sync {
2828
}
2929

3030
pub struct FileProvider {
31-
inputs: DashMap<PathBuf, String>
31+
inputs: Mutex<Vec<*mut String>>
3232
}
3333

3434
impl FileProvider {
3535
pub fn new() -> FileProvider {
3636
FileProvider {
37-
inputs: DashMap::new()
37+
inputs: Mutex::new(Vec::new()),
3838
}
3939
}
4040
}
4141

42+
unsafe impl Sync for FileProvider {}
43+
unsafe impl Send for FileProvider {}
44+
4245
impl SourceProvider for FileProvider {
4346
fn read<'a>(&'a self, file: &Path) -> std::io::Result<&'a str> {
4447
let source = fs::read_to_string(file)?;
45-
let res = self.inputs.entry(file.to_owned())
46-
.or_insert(source)
47-
.downgrade()
48-
.value();
49-
Ok(res)
48+
let ptr = Box::into_raw(Box::new(source));
49+
self.inputs.lock().unwrap().push(ptr);
50+
// SAFETY: this is safe because the pointer is not dropped
51+
// until the FileProvider is, and we never remove from the
52+
// list of pointers stored in the vector.
53+
Ok(unsafe { &*ptr })
54+
}
55+
}
56+
57+
impl Drop for FileProvider {
58+
fn drop(&mut self) {
59+
for ptr in self.inputs.lock().unwrap().iter() {
60+
std::mem::drop(unsafe { Box::from_raw(*ptr) })
61+
}
5062
}
5163
}
5264

@@ -298,22 +310,23 @@ mod tests {
298310
use super::*;
299311
use crate::{stylesheet::{PrinterOptions, MinifyOptions}, targets::Browsers};
300312
use indoc::indoc;
313+
use std::collections::HashMap;
301314

302315
struct TestProvider {
303-
map: DashMap<PathBuf, String>
316+
map: HashMap<PathBuf, String>
304317
}
305318

306319
impl SourceProvider for TestProvider {
307320
fn read<'a>(&'a self, file: &Path) -> std::io::Result<&'a str> {
308-
Ok(self.map.get(file).unwrap().value())
321+
Ok(self.map.get(file).unwrap())
309322
}
310323
}
311324

312325
macro_rules! fs(
313326
{ $($key:literal: $value:expr),* } => {
314327
{
315328
#[allow(unused_mut)]
316-
let mut m = DashMap::new();
329+
let mut m = HashMap::new();
317330
$(
318331
m.insert(PathBuf::from($key), $value.to_owned());
319332
)*

0 commit comments

Comments
 (0)