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
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
use std::mem::transmute_copy;
use super::from_raw::{FromRawResult, RawResult};
use super::super::err::Error;
#[derive(Debug, Clone, Copy)]
pub struct Symbol<'lib, T: 'lib> {
symbol: T,
pd: PhantomData<&'lib T>,
}
impl<'lib, T> Symbol<'lib, T> {
pub fn new(symbol: T) -> Symbol<'lib, T> {
Symbol {
symbol: symbol,
pd: PhantomData,
}
}
}
impl<'lib, T> FromRawResult for Symbol<'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 {
let raw: *const () = *ptr;
Ok(Symbol {
symbol: transmute_copy(&raw),
pd: PhantomData,
})
},
Err(err) => Err(err),
}
}
}
impl<'lib, T> Deref for Symbol<'lib, T> {
type Target = T;
fn deref(&self) -> &T {
return &self.symbol;
}
}
impl<'lib, T> DerefMut for Symbol<'lib, T> {
fn deref_mut(&mut self) -> &mut T {
return &mut self.symbol;
}
}
unsafe impl<'lib, T: Send> Send for Symbol<'lib, T> {}
unsafe impl<'lib, T: Sync> Sync for Symbol<'lib, T> {}