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
use FuncUnsafe;
use Symbol;

/// A pointer to a shared function which allows a user-provided ref-counting implementation to avoid outliving its library.
#[derive(Debug)]
pub struct FuncTracked<T, TLib> {
    func: FuncUnsafe<T>,
    _lib: TLib,
}

impl <T, TLib> FuncTracked<T, TLib> {
    /// Creates a new [FuncTracked](struct.FuncTracked.html).
    /// This should only be called within the library.
    pub fn new(func: FuncUnsafe<T>, lib: TLib) -> Self {
        FuncTracked {
            func: func,
            _lib: lib,
        }
    }
}

impl <T, TLib> Symbol<T> for FuncTracked<T, TLib>
    where T: Copy {
    unsafe fn get(&self) -> T {
        self.func
    }
}

impl <T, TLib> Clone for FuncTracked<T, TLib>
    where T: Copy,
          TLib: Clone {
    fn clone(&self) -> Self {
        FuncTracked {
            func: self.func,
            _lib: self._lib.clone(),
        }
    }
}