Skip to content

Commit 5fe5062

Browse files
committed
types: replace LockedContext with a mutex guard around ContextInner
Now that we have the dedicated ContextInner type, we don't need a dedicated LockedContext type. It's also conceptually simpler to have a "Context, which mutex-wraps a ContextInner, which implements all the real methods" rather than having this auxiliary "locked context" type which isn't directly related to Context. (And ofc there is WithGhostToken in the middle, but the reader of the code can hopefully mostly ignore this.) This gets rid of a lot of `.inner`s which is nice.
1 parent 3a7d71d commit 5fe5062

File tree

1 file changed

+11
-21
lines changed

1 file changed

+11
-21
lines changed

src/types/context.rs

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,8 @@ impl<'brand> Context<'brand> {
232232
}
233233

234234
/// Locks the underlying slab mutex.
235-
fn lock(&self) -> LockedContext<'_, 'brand> {
236-
LockedContext {
237-
inner: self.inner.lock().unwrap(),
238-
}
235+
fn lock(&self) -> MutexGuard<'_, WithGhostToken<'brand, ContextInner<'brand>>> {
236+
self.inner.lock().unwrap()
239237
}
240238
}
241239

@@ -297,18 +295,10 @@ struct BindError<'brand> {
297295
new: Bound<'brand>,
298296
}
299297

300-
/// Structure representing an inference context with its slab allocator mutex locked.
301-
///
302-
/// This type is never exposed outside of this module and should only exist
303-
/// ephemerally within function calls into this module.
304-
struct LockedContext<'ctx, 'brand> {
305-
inner: MutexGuard<'ctx, WithGhostToken<'brand, ContextInner<'brand>>>,
306-
}
307-
308-
impl<'brand> LockedContext<'_, 'brand> {
298+
impl<'brand> ContextInner<'brand> {
309299
fn alloc_bound(&mut self, bound: Bound<'brand>) -> BoundRef<'brand> {
310-
self.inner.slab.push(bound);
311-
let index = self.inner.slab.len() - 1;
300+
self.slab.push(bound);
301+
let index = self.slab.len() - 1;
312302

313303
BoundRef {
314304
phantom: InvariantLifetime::default(),
@@ -318,10 +308,10 @@ impl<'brand> LockedContext<'_, 'brand> {
318308

319309
fn reassign_non_complete(&mut self, bound: BoundRef<'brand>, new: Bound<'brand>) {
320310
assert!(
321-
!matches!(self.inner.slab[bound.index], Bound::Complete(..)),
311+
!matches!(self.slab[bound.index], Bound::Complete(..)),
322312
"tried to modify finalized type",
323313
);
324-
self.inner.slab[bound.index] = new;
314+
self.slab[bound.index] = new;
325315
}
326316

327317
/// It is a common situation that we are pairing two types, and in the
@@ -334,8 +324,8 @@ impl<'brand> LockedContext<'_, 'brand> {
334324
inn1: &TypeInner<'brand>,
335325
inn2: &TypeInner<'brand>,
336326
) -> Option<(Arc<Final>, Arc<Final>)> {
337-
let bound1 = &self.inner.slab[inn1.bound.root().index];
338-
let bound2 = &self.inner.slab[inn2.bound.root().index];
327+
let bound1 = &self.slab[inn1.bound.root().index];
328+
let bound2 = &self.slab[inn2.bound.root().index];
339329
if let (Bound::Complete(ref data1), Bound::Complete(ref data2)) = (bound1, bound2) {
340330
Some((Arc::clone(data1), Arc::clone(data2)))
341331
} else {
@@ -352,7 +342,7 @@ impl<'brand> LockedContext<'_, 'brand> {
352342
other: &TypeInner<'brand>,
353343
) -> Result<(), BindError<'brand>> {
354344
existing.bound.unify(&other.bound, |x_bound, y_bound| {
355-
self.bind(x_bound, self.inner.slab[y_bound.index].shallow_clone())
345+
self.bind(x_bound, self.slab[y_bound.index].shallow_clone())
356346
})
357347
}
358348

@@ -361,7 +351,7 @@ impl<'brand> LockedContext<'_, 'brand> {
361351
existing: BoundRef<'brand>,
362352
new: Bound<'brand>,
363353
) -> Result<(), BindError<'brand>> {
364-
let existing_bound = self.inner.slab[existing.index].shallow_clone();
354+
let existing_bound = self.slab[existing.index].shallow_clone();
365355
let bind_error = || BindError {
366356
existing: existing.clone(),
367357
new: new.shallow_clone(),

0 commit comments

Comments
 (0)