torch_sys/
traits.rs

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
macro_rules! list_trait {
    ($trait: ident, $ty: ident) => {
        pub trait $trait {
            fn as_ptr(&self) -> *const $ty;
            fn len_i32(&self) -> i32;
        }

        impl $trait for $ty {
            fn as_ptr(&self) -> *const $ty {
                self
            }
            fn len_i32(&self) -> i32 {
                1
            }
        }

        impl $trait for &[$ty] {
            fn as_ptr(&self) -> *const $ty {
                (*self).as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl $trait for Vec<$ty> {
            fn as_ptr(&self) -> *const $ty {
                self.as_slice().as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl $trait for &Vec<$ty> {
            fn as_ptr(&self) -> *const $ty {
                self.as_slice().as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl $trait for &Box<[$ty]> {
            fn as_ptr(&self) -> *const $ty {
                (**self).as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl $trait for [$ty] {
            fn as_ptr(&self) -> *const $ty {
                self.as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl<const N: usize> $trait for &[$ty; N] {
            fn as_ptr(&self) -> *const $ty {
                self.as_slice().as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }

        impl<const N: usize> $trait for [$ty; N] {
            fn as_ptr(&self) -> *const $ty {
                self.as_slice().as_ptr()
            }
            fn len_i32(&self) -> i32 {
                self.len() as i32
            }
        }
    };
}

list_trait!(DoubleList, f64);
list_trait!(IntList, i64);

pub trait IntListOption {
    fn as_ptr(&self) -> *const i64;
    fn len_i32(&self) -> i32;
}

trait IntListOption_: IntList {}

impl<T: IntListOption_> IntListOption for T {
    fn as_ptr(&self) -> *const i64 {
        self.as_ptr()
    }
    fn len_i32(&self) -> i32 {
        self.len_i32()
    }
}

impl<T: IntListOption_> IntListOption for Option<T> {
    fn as_ptr(&self) -> *const i64 {
        match self {
            Some(v) => v.as_ptr(),
            None => std::ptr::null(),
        }
    }

    fn len_i32(&self) -> i32 {
        self.as_ref().map_or(-1, |v| v.len_i32())
    }
}

impl IntListOption_ for i64 {}
impl IntListOption_ for [i64] {}
impl IntListOption_ for &[i64] {}
impl IntListOption_ for Vec<i64> {}
impl IntListOption_ for &Vec<i64> {}
impl IntListOption_ for &Box<[i64]> {}