2024-12-15 03:18:04 -05:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
|
|
|
|
|
|
|
pub use inner::*;
|
|
|
|
|
|
|
|
#[cfg(feature = "sync")]
|
|
|
|
mod inner {
|
|
|
|
#![allow(clippy::disallowed_types)]
|
|
|
|
|
|
|
|
pub use std::sync::Arc as MaybeArc;
|
|
|
|
|
|
|
|
pub use dashmap::DashMap as MaybeDashMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(not(feature = "sync"))]
|
|
|
|
mod inner {
|
2024-12-17 12:36:32 -05:00
|
|
|
use std::cell::Ref;
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::hash::BuildHasher;
|
|
|
|
use std::hash::Hash;
|
2024-12-15 03:18:04 -05:00
|
|
|
use std::hash::RandomState;
|
|
|
|
pub use std::rc::Rc as MaybeArc;
|
|
|
|
|
|
|
|
// Wrapper struct that exposes a subset of `DashMap` API.
|
2024-12-17 12:36:32 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct MaybeDashMap<K, V, S = RandomState>(RefCell<HashMap<K, V, S>>);
|
|
|
|
|
|
|
|
impl<K, V, S> Default for MaybeDashMap<K, V, S>
|
|
|
|
where
|
|
|
|
K: Eq + Hash,
|
|
|
|
S: Default + BuildHasher + Clone,
|
|
|
|
{
|
|
|
|
fn default() -> Self {
|
|
|
|
Self(RefCell::new(Default::default()))
|
|
|
|
}
|
|
|
|
}
|
2024-12-15 03:18:04 -05:00
|
|
|
|
2024-12-17 12:36:32 -05:00
|
|
|
impl<K: Eq + Hash, V, S: BuildHasher> MaybeDashMap<K, V, S> {
|
|
|
|
pub fn get<'a>(&'a self, key: &K) -> Option<Ref<'a, V>> {
|
|
|
|
Ref::filter_map(self.0.borrow(), |map| map.get(key)).ok()
|
2024-12-15 03:18:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn insert(&self, key: K, value: V) -> Option<V> {
|
2024-12-17 12:36:32 -05:00
|
|
|
let mut inner = self.0.borrow_mut();
|
2024-12-15 03:18:04 -05:00
|
|
|
inner.insert(key, value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|