Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2018"
threading = ["parking_lot"]

[dependencies]
lock_api = "0.4"
parking_lot = { version = "0.11.0", optional = true }
num-traits = "0.2"
num-complex = "0.3"
Expand Down
44 changes: 40 additions & 4 deletions common/src/borrow.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cell::{
PyMappedMutexGuard, PyMappedRwLockReadGuard, PyMappedRwLockWriteGuard, PyMutexGuard,
PyRwLockReadGuard, PyRwLockWriteGuard,
use crate::lock::{
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use std::ops::{Deref, DerefMut};

Expand All @@ -13,11 +13,30 @@ pub trait BorrowValue<'a> {
pub enum BorrowedValue<'a, T: ?Sized> {
Ref(&'a T),
MuLock(PyMutexGuard<'a, T>),
MappedMuLock(PyMappedMutexGuard<'a, T>),
MappedMuLock(PyImmutableMappedMutexGuard<'a, T>),
ReadLock(PyRwLockReadGuard<'a, T>),
MappedReadLock(PyMappedRwLockReadGuard<'a, T>),
}

impl<'a, T: ?Sized> BorrowedValue<'a, T> {
pub fn map<U: ?Sized, F>(s: Self, f: F) -> BorrowedValue<'a, U>
where
F: FnOnce(&T) -> &U,
{
match s {
Self::Ref(r) => BorrowedValue::Ref(f(r)),
Self::MuLock(m) => BorrowedValue::MappedMuLock(PyMutexGuard::map_immutable(m, f)),
Self::MappedMuLock(m) => {
BorrowedValue::MappedMuLock(PyImmutableMappedMutexGuard::map(m, f))
}
Self::ReadLock(r) => BorrowedValue::MappedReadLock(PyRwLockReadGuard::map(r, f)),
Self::MappedReadLock(m) => {
BorrowedValue::MappedReadLock(PyMappedRwLockReadGuard::map(m, f))
}
}
}
}

impl<T: ?Sized> Deref for BorrowedValue<'_, T> {
type Target = T;
fn deref(&self) -> &T {
Expand All @@ -40,6 +59,23 @@ pub enum BorrowedValueMut<'a, T: ?Sized> {
MappedWriteLock(PyMappedRwLockWriteGuard<'a, T>),
}

impl<'a, T: ?Sized> BorrowedValueMut<'a, T> {
pub fn map<U: ?Sized, F>(s: Self, f: F) -> BorrowedValueMut<'a, U>
where
F: FnOnce(&mut T) -> &mut U,
{
match s {
Self::RefMut(r) => BorrowedValueMut::RefMut(f(r)),
Self::MuLock(m) => BorrowedValueMut::MappedMuLock(PyMutexGuard::map(m, f)),
Self::MappedMuLock(m) => BorrowedValueMut::MappedMuLock(PyMappedMutexGuard::map(m, f)),
Self::WriteLock(r) => BorrowedValueMut::MappedWriteLock(PyRwLockWriteGuard::map(r, f)),
Self::MappedWriteLock(m) => {
BorrowedValueMut::MappedWriteLock(PyMappedRwLockWriteGuard::map(m, f))
}
}
}
}

impl<T: ?Sized> Deref for BorrowedValueMut<'_, T> {
type Target = T;
fn deref(&self) -> &T {
Expand Down
268 changes: 0 additions & 268 deletions common/src/cell.rs

This file was deleted.

2 changes: 1 addition & 1 deletion common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! A crate to hold types and functions common to all rustpython components.

pub mod borrow;
pub mod cell;
pub mod float_ops;
pub mod hash;
pub mod lock;
pub mod rc;
pub mod str;
Loading