forked from RustPython/RustPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathborrow.rs
More file actions
102 lines (94 loc) · 3.23 KB
/
borrow.rs
File metadata and controls
102 lines (94 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use crate::lock::{
MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use std::ops::{Deref, DerefMut};
pub trait BorrowValue<'a> {
type Borrowed: 'a + Deref;
fn borrow_value(&'a self) -> Self::Borrowed;
}
#[derive(Debug, derive_more::From)]
pub enum BorrowedValue<'a, T: ?Sized> {
Ref(&'a T),
MuLock(PyMutexGuard<'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 {
match self {
Self::Ref(r) => r,
Self::MuLock(m) => &m,
Self::MappedMuLock(m) => &m,
Self::ReadLock(r) => &r,
Self::MappedReadLock(m) => &m,
}
}
}
#[derive(Debug, derive_more::From)]
pub enum BorrowedValueMut<'a, T: ?Sized> {
RefMut(&'a mut T),
MuLock(PyMutexGuard<'a, T>),
MappedMuLock(PyMappedMutexGuard<'a, T>),
WriteLock(PyRwLockWriteGuard<'a, T>),
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 {
match self {
Self::RefMut(r) => r,
Self::MuLock(m) => &m,
Self::MappedMuLock(m) => &m,
Self::WriteLock(w) => &w,
Self::MappedWriteLock(w) => &w,
}
}
}
impl<T: ?Sized> DerefMut for BorrowedValueMut<'_, T> {
fn deref_mut(&mut self) -> &mut T {
match self {
Self::RefMut(r) => r,
Self::MuLock(m) => &mut *m,
Self::MappedMuLock(m) => &mut *m,
Self::WriteLock(w) => &mut *w,
Self::MappedWriteLock(w) => &mut *w,
}
}
}