torch_sys/
traits.rs

1macro_rules! list_trait {
2    ($trait: ident, $ty: ident) => {
3        pub trait $trait {
4            fn as_ptr(&self) -> *const $ty;
5            fn len_i32(&self) -> i32;
6        }
7
8        impl $trait for $ty {
9            fn as_ptr(&self) -> *const $ty {
10                self
11            }
12            fn len_i32(&self) -> i32 {
13                1
14            }
15        }
16
17        impl $trait for &[$ty] {
18            fn as_ptr(&self) -> *const $ty {
19                (*self).as_ptr()
20            }
21            fn len_i32(&self) -> i32 {
22                self.len() as i32
23            }
24        }
25
26        impl $trait for Vec<$ty> {
27            fn as_ptr(&self) -> *const $ty {
28                self.as_slice().as_ptr()
29            }
30            fn len_i32(&self) -> i32 {
31                self.len() as i32
32            }
33        }
34
35        impl $trait for &Vec<$ty> {
36            fn as_ptr(&self) -> *const $ty {
37                self.as_slice().as_ptr()
38            }
39            fn len_i32(&self) -> i32 {
40                self.len() as i32
41            }
42        }
43
44        impl $trait for &Box<[$ty]> {
45            fn as_ptr(&self) -> *const $ty {
46                (**self).as_ptr()
47            }
48            fn len_i32(&self) -> i32 {
49                self.len() as i32
50            }
51        }
52
53        impl $trait for [$ty] {
54            fn as_ptr(&self) -> *const $ty {
55                self.as_ptr()
56            }
57            fn len_i32(&self) -> i32 {
58                self.len() as i32
59            }
60        }
61
62        impl<const N: usize> $trait for &[$ty; N] {
63            fn as_ptr(&self) -> *const $ty {
64                self.as_slice().as_ptr()
65            }
66            fn len_i32(&self) -> i32 {
67                self.len() as i32
68            }
69        }
70
71        impl<const N: usize> $trait for [$ty; N] {
72            fn as_ptr(&self) -> *const $ty {
73                self.as_slice().as_ptr()
74            }
75            fn len_i32(&self) -> i32 {
76                self.len() as i32
77            }
78        }
79    };
80}
81
82list_trait!(DoubleList, f64);
83list_trait!(IntList, i64);
84
85pub trait IntListOption {
86    fn as_ptr(&self) -> *const i64;
87    fn len_i32(&self) -> i32;
88}
89
90trait IntListOption_: IntList {}
91
92impl<T: IntListOption_> IntListOption for T {
93    fn as_ptr(&self) -> *const i64 {
94        self.as_ptr()
95    }
96    fn len_i32(&self) -> i32 {
97        self.len_i32()
98    }
99}
100
101impl<T: IntListOption_> IntListOption for Option<T> {
102    fn as_ptr(&self) -> *const i64 {
103        match self {
104            Some(v) => v.as_ptr(),
105            None => std::ptr::null(),
106        }
107    }
108
109    fn len_i32(&self) -> i32 {
110        self.as_ref().map_or(-1, |v| v.len_i32())
111    }
112}
113
114impl IntListOption_ for i64 {}
115impl IntListOption_ for [i64] {}
116impl IntListOption_ for &[i64] {}
117impl IntListOption_ for Vec<i64> {}
118impl IntListOption_ for &Vec<i64> {}
119impl IntListOption_ for &Box<[i64]> {}