1 | use libc::c_int; |
2 | use std::marker::PhantomData; |
3 | |
4 | /// A slot in a type's "extra data" structure. |
5 | /// |
6 | /// It is parameterized over the type containing the extra data as well as the |
7 | /// type of the data in the slot. |
8 | pub struct Index<T, U>(c_int, PhantomData<(T, U)>); |
9 | |
10 | impl<T, U> Copy for Index<T, U> {} |
11 | |
12 | impl<T, U> Clone for Index<T, U> { |
13 | fn clone(&self) -> Index<T, U> { |
14 | *self |
15 | } |
16 | } |
17 | |
18 | impl<T, U> Index<T, U> { |
19 | /// Creates an `Index` from a raw integer index. |
20 | /// |
21 | /// # Safety |
22 | /// |
23 | /// The caller must ensure that the index correctly maps to a `U` value stored in a `T`. |
24 | pub unsafe fn from_raw(idx: c_int) -> Index<T, U> { |
25 | Index(idx, PhantomData) |
26 | } |
27 | |
28 | #[allow (clippy::trivially_copy_pass_by_ref)] |
29 | pub fn as_raw(&self) -> c_int { |
30 | self.0 |
31 | } |
32 | } |
33 | |