1use super::super::err::Error;
2use super::from_raw::{FromRawResult, RawResult};
3use std::marker::PhantomData;
4use std::mem::transmute_copy;
5use std::ops::{Deref, DerefMut};
6
7#[derive(Debug, Clone, Copy)]
13pub struct Symbol<'lib, T: 'lib> {
14 symbol: T,
15 pd: PhantomData<&'lib T>,
16}
17
18impl<'lib, T> Symbol<'lib, T> {
19 pub fn new(symbol: T) -> Symbol<'lib, T> {
20 Symbol {
21 symbol,
22 pd: PhantomData,
23 }
24 }
25}
26
27impl<'lib, T> FromRawResult for Symbol<'lib, T> {
28 unsafe fn from_raw_result(raw_result: RawResult) -> Result<Self, Error> {
29 unsafe {
30 match raw_result {
31 Ok(ptr) => {
32 if ptr.is_null() {
33 Err(Error::NullSymbol)
34 } else {
35 let raw: *const () = *ptr;
36 Ok(Symbol {
37 symbol: transmute_copy(&raw),
38 pd: PhantomData,
39 })
40 }
41 }
42 Err(err) => Err(err),
43 }
44 }
45 }
46}
47
48impl<'lib, T> Deref for Symbol<'lib, T> {
49 type Target = T;
50 fn deref(&self) -> &T {
51 &self.symbol
52 }
53}
54
55impl<'lib, T> DerefMut for Symbol<'lib, T> {
56 fn deref_mut(&mut self) -> &mut T {
58 &mut self.symbol
59 }
60}
61
62unsafe impl<'lib, T: Send> Send for Symbol<'lib, T> {}
63unsafe impl<'lib, T: Sync> Sync for Symbol<'lib, T> {}