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
#![cfg_attr(not(feature="std"), no_std)]
mod lib {
#[cfg(feature="std")]
pub use std::*;
#[cfg(not(feature="std"))]
pub use core::*;
}
use lib::ops::DerefMut;
use lib::cell::RefCell;
pub trait InteriorMut<'a, T> {
type RefMut: DerefMut<Target=T> + 'a;
type Error;
fn borrow_int_mut(&'a self) -> Result<Self::RefMut, Self::Error>;
}
impl<'a, T: 'a> InteriorMut<'a, T> for RefCell<T> {
type RefMut = lib::cell::RefMut<'a, T>;
type Error = lib::cell::BorrowMutError;
fn borrow_int_mut(&'a self) -> Result<Self::RefMut, Self::Error> {
RefCell::try_borrow_mut(self)
}
}
#[cfg(feature="std")]
impl<'a, T: 'a> InteriorMut<'a, T> for std::sync::Mutex<T> {
type RefMut = std::sync::MutexGuard<'a, T>;
type Error = std::sync::PoisonError<std::sync::MutexGuard<'a, T>>;
fn borrow_int_mut(&'a self) -> std::sync::LockResult<std::sync::MutexGuard<'a, T>> {
self.lock()
}
}
#[cfg(feature="std")]
impl<'a, T: 'a> InteriorMut<'a, T> for std::sync::RwLock<T> {
type RefMut = std::sync::RwLockWriteGuard<'a, T>;
type Error = std::sync::PoisonError<std::sync::RwLockWriteGuard<'a, T>>;
fn borrow_int_mut(&'a self) -> std::sync::LockResult<std::sync::RwLockWriteGuard<'a, T>> {
self.write()
}
}