Skip to content

Commit

Permalink
fix: use RwLockReadGuard::try_map
Browse files Browse the repository at this point in the history
  • Loading branch information
mtshiba committed Sep 7, 2024
1 parent ffe02f1 commit cb6931e
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 95 deletions.
8 changes: 1 addition & 7 deletions crates/els/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,13 +442,7 @@ impl CompletionCache {
}

pub fn get(&self, namespace: &str) -> Option<MappedRwLockReadGuard<Vec<CompletionItem>>> {
if self.cache.borrow().get(namespace).is_some() {
Some(RwLockReadGuard::map(self.cache.borrow(), |cache| {
cache.get(namespace).unwrap()
}))
} else {
None
}
RwLockReadGuard::try_map(self.cache.borrow(), |cache| cache.get(namespace)).ok()
}

pub fn insert(&self, namespace: String, items: Vec<CompletionItem>) {
Expand Down
10 changes: 2 additions & 8 deletions crates/els/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1151,10 +1151,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
pub fn get_hir(&self, uri: &NormalizedUrl) -> Option<MappedRwLockReadGuard<HIR>> {
let path = uri.to_file_path().ok()?;
let ent = self.shared.get_module(&path)?;
ent.hir.as_ref()?;
Some(MappedRwLockReadGuard::map(ent, |ent| {
ent.hir.as_ref().unwrap()
}))
MappedRwLockReadGuard::try_map(ent, |ent| ent.hir.as_ref()).ok()
}

pub fn steal_entry(&self, uri: &NormalizedUrl) -> Option<ModuleEntry> {
Expand All @@ -1170,10 +1167,7 @@ impl<Checker: BuildRunnable, Parser: Parsable> Server<Checker, Parser> {
pub fn get_ast(&self, uri: &NormalizedUrl) -> Option<MappedRwLockReadGuard<Module>> {
let path = uri.to_file_path().ok()?;
let ent = self.shared.get_module(&path)?;
ent.ast.as_ref()?;
Some(MappedRwLockReadGuard::map(ent, |ent| {
ent.ast.as_ref().unwrap()
}))
MappedRwLockReadGuard::try_map(ent, |ent| ent.ast.as_ref()).ok()
}

pub fn get_warns(&self, uri: &NormalizedUrl) -> Option<Vec<&CompileWarning>> {
Expand Down
27 changes: 6 additions & 21 deletions crates/erg_compiler/module/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,7 @@ impl SharedModuleCache {
where
NormalizedPathBuf: Borrow<Q>,
{
if self.0.borrow().get(path).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |cache| {
cache.get(path).unwrap()
}))
} else {
None
}
RwLockReadGuard::try_map(self.0.borrow(), |cache| cache.get(path)).ok()
}

pub fn get_mut<Q: Eq + Hash + ?Sized>(
Expand All @@ -276,13 +270,7 @@ impl SharedModuleCache {
where
NormalizedPathBuf: Borrow<Q>,
{
if self.0.borrow().get(path).is_some() {
Some(RwLockWriteGuard::map(self.0.borrow_mut(), |cache| {
cache.get_mut(path).unwrap()
}))
} else {
None
}
RwLockWriteGuard::try_map(self.0.borrow_mut(), |cache| cache.get_mut(path)).ok()
}

pub fn ref_ctx<Q: Eq + Hash + ?Sized>(
Expand All @@ -292,13 +280,10 @@ impl SharedModuleCache {
where
NormalizedPathBuf: Borrow<Q>,
{
if self.0.borrow().get(path).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |cache| {
&cache.get(path).unwrap().module
}))
} else {
None
}
RwLockReadGuard::try_map(self.0.borrow(), |cache| {
cache.get(path).map(|ent| &ent.module)
})
.ok()
}

/// FIXME: see the comment in this function
Expand Down
8 changes: 1 addition & 7 deletions crates/erg_compiler/module/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,7 @@ impl SharedModuleGraph {
&self,
path: &NormalizedPathBuf,
) -> Option<MappedRwLockReadGuard<Node<NormalizedPathBuf, ()>>> {
if self.0.borrow().get_node(path).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |graph| {
graph.get_node(path).unwrap()
}))
} else {
None
}
RwLockReadGuard::try_map(self.0.borrow(), |graph| graph.get_node(path)).ok()
}

pub fn depends_on(&self, path: &NormalizedPathBuf, target: &NormalizedPathBuf) -> bool {
Expand Down
16 changes: 2 additions & 14 deletions crates/erg_compiler/module/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,7 @@ impl SharedTraitImpls {
where
Str: Borrow<Q>,
{
if self.0.borrow().get(path).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |tis| {
tis.get(path).unwrap()
}))
} else {
None
}
RwLockReadGuard::try_map(self.0.borrow(), |tis| tis.get(path)).ok()
}

pub fn get_mut<Q: Eq + Hash + ?Sized>(
Expand All @@ -135,13 +129,7 @@ impl SharedTraitImpls {
where
Str: Borrow<Q>,
{
if self.0.borrow().get(path).is_some() {
Some(RwLockWriteGuard::map(self.0.borrow_mut(), |tis| {
tis.get_mut(path).unwrap()
}))
} else {
None
}
RwLockWriteGuard::try_map(self.0.borrow_mut(), |tis| tis.get_mut(path)).ok()
}

pub fn register(&self, name: Str, impls: Set<TraitImpl>) {
Expand Down
8 changes: 1 addition & 7 deletions crates/erg_compiler/module/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,13 +171,7 @@ impl SharedModuleIndex {
&self,
referee: &AbsLocation,
) -> Option<MappedRwLockReadGuard<ModuleIndexValue>> {
if self.0.borrow().get_refs(referee).is_some() {
Some(RwLockReadGuard::map(self.0.borrow(), |index| {
index.get_refs(referee).unwrap()
}))
} else {
None
}
RwLockReadGuard::try_map(self.0.borrow(), |index| index.get_refs(referee)).ok()
}

pub fn members(&self) -> Members {
Expand Down
42 changes: 15 additions & 27 deletions crates/erg_compiler/ty/free.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1124,41 +1124,29 @@ impl<T: Clone + Send + Sync + 'static> Free<T> {

#[track_caller]
pub fn get_linked_ref(&self) -> Option<Ref<T>> {
if !self.is_linked() {
None
} else {
let mapped = Ref::map(self.borrow(), |f| match f {
FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => t,
FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => unreachable!(),
});
Some(mapped)
}
Ref::filter_map(self.borrow(), |f| match f {
FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => Some(t),
FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => None,
})
.ok()
}

#[track_caller]
pub fn get_linked_refmut(&self) -> Option<RefMut<T>> {
if !self.is_linked() {
None
} else {
let mapped = RefMut::map(self.borrow_mut(), |f| match f {
FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => t,
FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => unreachable!(),
});
Some(mapped)
}
RefMut::filter_map(self.borrow_mut(), |f| match f {
FreeKind::Linked(t) | FreeKind::UndoableLinked { t, .. } => Some(t),
FreeKind::Unbound { .. } | FreeKind::NamedUnbound { .. } => None,
})
.ok()
}

#[track_caller]
pub fn get_previous(&self) -> Option<Ref<Box<FreeKind<T>>>> {
if !self.is_undoable_linked() {
None
} else {
let mapped = Ref::map(self.borrow(), |f| match f {
FreeKind::UndoableLinked { previous, .. } => previous,
_ => unreachable!(),
});
Some(mapped)
}
Ref::filter_map(self.borrow(), |f| match f {
FreeKind::UndoableLinked { previous, .. } => Some(previous),
_ => None,
})
.ok()
}

pub fn detach(&self) -> Self {
Expand Down
6 changes: 2 additions & 4 deletions crates/erg_compiler/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3990,10 +3990,8 @@ impl Type {

pub fn tyvar_mut_return_t(&mut self) -> Option<RefMut<Type>> {
match self {
Self::FreeVar(fv) if fv.get_linked()?.return_t().is_some() => {
Some(RefMut::map(fv.borrow_mut(), |fk| {
fk.linked_mut().unwrap().mut_return_t().unwrap()
}))
Self::FreeVar(fv) => {
RefMut::filter_map(fv.borrow_mut(), |fv| fv.linked_mut()?.mut_return_t()).ok()
}
_ => None,
}
Expand Down

0 comments on commit cb6931e

Please sign in to comment.