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
/// A typed wrapper for key of Ops submitted into driver
#[derive(PartialEq, Eq, Hash)]
pub struct Key<T> {
    user_data: usize,
    _p: std::marker::PhantomData<fn(T)>,
}

impl<T> Unpin for Key<T> {}

impl<T> Clone for Key<T> {
    fn clone(&self) -> Self {
        *self
    }
}

impl<T> Copy for Key<T> {}

impl<T> Key<T> {
    /// Create a new `Key` with the given user data.
    ///
    /// # Safety
    ///
    /// Caller needs to ensure that `T` does correspond to `user_data` in driver
    /// this `Key` is created with.
    pub unsafe fn new(user_data: usize) -> Self {
        Self {
            user_data,
            _p: std::marker::PhantomData,
        }
    }
}

impl<T> std::ops::Deref for Key<T> {
    type Target = usize;

    fn deref(&self) -> &Self::Target {
        &self.user_data
    }
}

impl<T> std::fmt::Debug for Key<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Key({})", self.user_data)
    }
}