Skip to content

Commit 210c923

Browse files
authored
refactor: replace UnsafeCell with RefCell (#346)
There's a slight cost of the runtime borrow check but I guess it's negligible.
1 parent df0bd1c commit 210c923

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

src/cache.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{
22
borrow::Cow,
3-
cell::UnsafeCell,
3+
cell::RefCell,
44
convert::AsRef,
55
hash::{BuildHasherDefault, Hash, Hasher},
66
io,
@@ -27,7 +27,7 @@ static THREAD_COUNT: AtomicU64 = AtomicU64::new(1);
2727
thread_local! {
2828
/// Per-thread pre-allocated path that is used to perform operations on paths more quickly.
2929
/// Learned from parcel <https://github.com/parcel-bundler/parcel/blob/a53f8f3ba1025c7ea8653e9719e0a61ef9717079/crates/parcel-resolver/src/cache.rs#L394>
30-
pub static SCRATCH_PATH: UnsafeCell<PathBuf> = UnsafeCell::new(PathBuf::with_capacity(256));
30+
pub static SCRATCH_PATH: RefCell<PathBuf> = RefCell::new(PathBuf::with_capacity(256));
3131
pub static THREAD_ID: u64 = THREAD_COUNT.fetch_add(1, Ordering::SeqCst);
3232
}
3333

@@ -382,9 +382,7 @@ impl CachedPath {
382382
}
383383

384384
pub fn add_extension<Fs: FileSystem>(&self, ext: &str, cache: &Cache<Fs>) -> Self {
385-
SCRATCH_PATH.with(|path| {
386-
// SAFETY: ???
387-
let path = unsafe { &mut *path.get() };
385+
SCRATCH_PATH.with_borrow_mut(|path| {
388386
path.clear();
389387
let s = path.as_mut_os_string();
390388
s.push(self.path.as_os_str());
@@ -394,9 +392,7 @@ impl CachedPath {
394392
}
395393

396394
pub fn replace_extension<Fs: FileSystem>(&self, ext: &str, cache: &Cache<Fs>) -> Self {
397-
SCRATCH_PATH.with(|path| {
398-
// SAFETY: ???
399-
let path = unsafe { &mut *path.get() };
395+
SCRATCH_PATH.with_borrow_mut(|path| {
400396
path.clear();
401397
let s = path.as_mut_os_string();
402398
let self_len = self.path.as_os_str().len();
@@ -423,9 +419,7 @@ impl CachedPath {
423419
if matches!(head, Component::Prefix(..) | Component::RootDir) {
424420
return cache.value(subpath);
425421
}
426-
SCRATCH_PATH.with(|path| {
427-
// SAFETY: ???
428-
let path = unsafe { &mut *path.get() };
422+
SCRATCH_PATH.with_borrow_mut(|path| {
429423
path.clear();
430424
path.push(&self.path);
431425
for component in std::iter::once(head).chain(components) {

0 commit comments

Comments
 (0)