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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
//! Contains the ffi-safe equivalent of `std::ops::Range*` types.

use std::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};

////////////////////////////////////////////////////////////////

macro_rules! impl_into_iterator {
    ( $from: ident, $to: ident ) => {
        impl<T> IntoIterator for $from<T>
        where
            $to<T>: Iterator<Item = T>,
        {
            type IntoIter = $to<T>;
            type Item = T;

            #[inline]
            fn into_iter(self) -> $to<T> {
                self.into()
            }
        }
    };
}

////////////////////////////////////////////////////////////////

/// Ffi-safe equivalent of `::std::ops::Range`
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(C)]
#[derive(StableAbi)]
pub struct RRange<T> {
    pub start: T,
    pub end: T,
}

impl RRange<usize> {
    pub const fn from_std(v: Range<usize>) -> Self {
        Self {
            start: v.start,
            end: v.end,
        }
    }
}

impl_from_rust_repr! {
    impl[T] From<Range<T>> for RRange<T> {
        fn(v){
            Self {
                start: v.start,
                end: v.end,
            }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<Range<T>> for RRange<T> {
        fn(this){
            Range {
                start: this.start,
                end: this.end,
            }
        }
    }
}

impl_into_iterator! { RRange, Range }

////////////////////////////////////////////////////////////////

/// Ffi-safe equivalent of `::std::ops::RangeInclusive`
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(C)]
#[derive(StableAbi)]
pub struct RRangeInclusive<T> {
    pub start: T,
    pub end: T,
}

impl_from_rust_repr! {
    impl[T] From<RangeInclusive<T>> for RRangeInclusive<T> {
        fn(v){
            let (start, end) = v.into_inner();
            Self { start, end }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<RangeInclusive<T>> for RRangeInclusive<T> {
        fn(this){
            RangeInclusive::new(this.start, this.end)
        }
    }
}

impl_into_iterator! { RRangeInclusive, RangeInclusive }

////////////////////////////////////////////////////////////////

/// Ffi-safe equivalent of `::std::ops::RangeFrom`
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(C)]
#[derive(StableAbi)]
pub struct RRangeFrom<T> {
    pub start: T,
}

impl_from_rust_repr! {
    impl[T] From<RangeFrom<T>> for RRangeFrom<T> {
        fn(v){
            Self { start: v.start }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<RangeFrom<T>> for RRangeFrom<T> {
        fn(this){
            this.start..
        }
    }
}

impl_into_iterator! { RRangeFrom, RangeFrom }

////////////////////////////////////////////////////////////////

/// Ffi-safe equivalent of `::std::ops::RangeTo`
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(C)]
#[derive(StableAbi)]
pub struct RRangeTo<T> {
    pub end: T,
}

impl_from_rust_repr! {
    impl[T] From<RangeTo<T>> for RRangeTo<T> {
        fn(v){
            Self { end: v.end }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<RangeTo<T>> for RRangeTo<T> {
        fn(this){
            ..this.end
        }
    }
}

////////////////////////////////////////////////////////////////

/// Ffi-safe equivalent of `::std::ops::RangeToInclusive`
#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
#[repr(C)]
#[derive(StableAbi)]
pub struct RRangeToInclusive<T> {
    pub end: T,
}

impl_from_rust_repr! {
    impl[T] From<RangeToInclusive<T>> for RRangeToInclusive<T> {
        fn(v){
            Self { end: v.end }
        }
    }
}

impl_into_rust_repr! {
    impl[T] Into<RangeToInclusive<T>> for RRangeToInclusive<T> {
        fn(this){
             ..= this.end
        }
    }
}

////////////////////////////////////////////////////////////////