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