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
use std::ops::{Deref, DerefMut};
use super::from_raw::{FromRawResult, RawResult};
use super::super::err::Error;
use std::mem::transmute;
#[derive(Debug)]
pub struct RefMut<'lib, T: 'lib> {
reference: &'lib mut T,
}
impl<'lib, T> RefMut<'lib, T> {
pub fn new(reference: &'lib mut T) -> RefMut<'lib, T> {
RefMut {
reference: reference,
}
}
}
impl<'lib, T> FromRawResult for RefMut<'lib, T> {
unsafe fn from_raw_result(raw_result: RawResult) -> Result<Self, Error> {
match raw_result {
Ok(ptr) => if ptr.is_null() {
Err(Error::NullSymbol)
} else {
Ok(RefMut {
reference: transmute(*ptr),
})
},
Err(err) => Err(err),
}
}
}
impl<'lib, T> Deref for RefMut<'lib, T> {
type Target = T;
fn deref(&self) -> &T {
self.reference
}
}
impl<'lib, T> DerefMut for RefMut<'lib, T> {
fn deref_mut(&mut self) -> &mut T {
self.reference
}
}
unsafe impl<'lib, T: Send> Send for RefMut<'lib, T> {}
unsafe impl<'lib, T: Sync> Sync for RefMut<'lib, T> {}