Skip to content

Commit e2bc53f

Browse files
authored
Merge pull request RustPython#2239 from RustPython/coolreader18/unified-common-locks
Unified lock types for rustpython_common
2 parents 465702d + 7a39500 commit e2bc53f

36 files changed

+408
-311
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

common/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ edition = "2018"
88
threading = ["parking_lot"]
99

1010
[dependencies]
11+
lock_api = "0.4"
1112
parking_lot = { version = "0.11.0", optional = true }
1213
num-traits = "0.2"
1314
num-complex = "0.3"

common/src/borrow.rs

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use crate::cell::{
2-
PyMappedMutexGuard, PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutexGuard,
3-
PyRwLockReadGuard, PyRwLockWriteGuard,
1+
use crate::lock::{
2+
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
3+
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
44
};
55
use std::ops::{Deref, DerefMut};
66

@@ -13,11 +13,30 @@ pub trait BorrowValue<'a> {
1313
pub enum BorrowedValue<'a, T: ?Sized> {
1414
Ref(&'a T),
1515
MuLock(PyMutexGuard<'a, T>),
16-
MappedMuLock(PyMappedMutexGuard<'a, T>),
16+
MappedMuLock(PyImmutableMappedMutexGuard<'a, T>),
1717
ReadLock(PyRwLockReadGuard<'a, T>),
1818
MappedReadLock(PyMappedRwLockReadGuard<'a, T>),
1919
}
2020

21+
impl<'a, T: ?Sized> BorrowedValue<'a, T> {
22+
pub fn map<U: ?Sized, F>(s: Self, f: F) -> BorrowedValue<'a, U>
23+
where
24+
F: FnOnce(&T) -> &U,
25+
{
26+
match s {
27+
Self::Ref(r) => BorrowedValue::Ref(f(r)),
28+
Self::MuLock(m) => BorrowedValue::MappedMuLock(PyMutexGuard::map_immutable(m, f)),
29+
Self::MappedMuLock(m) => {
30+
BorrowedValue::MappedMuLock(PyImmutableMappedMutexGuard::map(m, f))
31+
}
32+
Self::ReadLock(r) => BorrowedValue::MappedReadLock(PyRwLockReadGuard::map(r, f)),
33+
Self::MappedReadLock(m) => {
34+
BorrowedValue::MappedReadLock(PyMappedRwLockReadGuard::map(m, f))
35+
}
36+
}
37+
}
38+
}
39+
2140
impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
2241
type Target = T;
2342
fn deref(&self) -> &T {
@@ -40,6 +59,23 @@ pub enum BorrowedValueMut<'a, T: ?Sized> {
4059
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
4160
}
4261

62+
impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {
63+
pub fn map<U: ?Sized, F>(s: Self, f: F) -> BorrowedValueMut<'a, U>
64+
where
65+
F: FnOnce(&mut T) -> &mut U,
66+
{
67+
match s {
68+
Self::RefMut(r) => BorrowedValueMut::RefMut(f(r)),
69+
Self::MuLock(m) => BorrowedValueMut::MappedMuLock(PyMutexGuard::map(m, f)),
70+
Self::MappedMuLock(m) => BorrowedValueMut::MappedMuLock(PyMappedMutexGuard::map(m, f)),
71+
Self::WriteLock(r) => BorrowedValueMut::MappedWriteLock(PyRwLockWriteGuard::map(r, f)),
72+
Self::MappedWriteLock(m) => {
73+
BorrowedValueMut::MappedWriteLock(PyMappedRwLockWriteGuard::map(m, f))
74+
}
75+
}
76+
}
77+
}
78+
4379
impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
4480
type Target = T;
4581
fn deref(&self) -> &T {

common/src/cell.rs

Lines changed: 0 additions & 268 deletions
This file was deleted.

common/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! A crate to hold types and functions common to all rustpython components.
22
33
pub mod borrow;
4-
pub mod cell;
54
pub mod float_ops;
65
pub mod hash;
6+
pub mod lock;
77
pub mod rc;
88
pub mod str;

0 commit comments

Comments
 (0)