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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
use crate::lock::{
    MapImmutable, PyImmutableMappedMutexGuard, PyMappedMutexGuard, PyMappedRwLockReadGuard,
    PyMappedRwLockWriteGuard, PyMutexGuard, PyRwLockReadGuard, PyRwLockWriteGuard,
};
use std::{
    fmt,
    ops::{Deref, DerefMut},
};

macro_rules! impl_from {
    ($lt:lifetime, $gen:ident, $t:ty, $($var:ident($from:ty),)*) => {
        $(
            impl<$lt, $gen: ?Sized> From<$from> for $t {
                fn from(t: $from) -> Self {
                    Self::$var(t)
                }
            }
        )*
    };
}

#[derive(Debug)]
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_from!('a, T, BorrowedValue<'a, T>,
    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,
        }
    }
}

impl fmt::Display for BorrowedValue<'_, str> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self.deref(), f)
    }
}

#[derive(Debug)]
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_from!('a, T, BorrowedValueMut<'a, T>,
    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,
        }
    }
}