abi_stable/std_types/
range.rs

1//! Contains the ffi-safe equivalent of `std::ops::Range*` types.
2
3use std::ops::{Range, RangeFrom, RangeInclusive, RangeTo, RangeToInclusive};
4
5////////////////////////////////////////////////////////////////
6
7macro_rules! impl_into_iterator {
8    ( $from: ident, $to: ident ) => {
9        impl<T> IntoIterator for $from<T>
10        where
11            $to<T>: Iterator<Item = T>,
12        {
13            type IntoIter = $to<T>;
14            type Item = T;
15
16            #[inline]
17            fn into_iter(self) -> $to<T> {
18                self.into()
19            }
20        }
21    };
22}
23
24////////////////////////////////////////////////////////////////
25
26/// Ffi-safe equivalent of `::std::ops::Range`
27#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
28#[repr(C)]
29#[derive(StableAbi)]
30pub struct RRange<T> {
31    pub start: T,
32    pub end: T,
33}
34
35impl RRange<usize> {
36    pub const fn from_std(v: Range<usize>) -> Self {
37        Self {
38            start: v.start,
39            end: v.end,
40        }
41    }
42}
43
44impl_from_rust_repr! {
45    impl[T] From<Range<T>> for RRange<T> {
46        fn(v){
47            Self {
48                start: v.start,
49                end: v.end,
50            }
51        }
52    }
53}
54
55impl_into_rust_repr! {
56    impl[T] Into<Range<T>> for RRange<T> {
57        fn(this){
58            Range {
59                start: this.start,
60                end: this.end,
61            }
62        }
63    }
64}
65
66impl_into_iterator! { RRange, Range }
67
68////////////////////////////////////////////////////////////////
69
70/// Ffi-safe equivalent of `::std::ops::RangeInclusive`
71#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
72#[repr(C)]
73#[derive(StableAbi)]
74pub struct RRangeInclusive<T> {
75    pub start: T,
76    pub end: T,
77}
78
79impl_from_rust_repr! {
80    impl[T] From<RangeInclusive<T>> for RRangeInclusive<T> {
81        fn(v){
82            let (start, end) = v.into_inner();
83            Self { start, end }
84        }
85    }
86}
87
88impl_into_rust_repr! {
89    impl[T] Into<RangeInclusive<T>> for RRangeInclusive<T> {
90        fn(this){
91            RangeInclusive::new(this.start, this.end)
92        }
93    }
94}
95
96impl_into_iterator! { RRangeInclusive, RangeInclusive }
97
98////////////////////////////////////////////////////////////////
99
100/// Ffi-safe equivalent of `::std::ops::RangeFrom`
101#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
102#[repr(C)]
103#[derive(StableAbi)]
104pub struct RRangeFrom<T> {
105    pub start: T,
106}
107
108impl_from_rust_repr! {
109    impl[T] From<RangeFrom<T>> for RRangeFrom<T> {
110        fn(v){
111            Self { start: v.start }
112        }
113    }
114}
115
116impl_into_rust_repr! {
117    impl[T] Into<RangeFrom<T>> for RRangeFrom<T> {
118        fn(this){
119            this.start..
120        }
121    }
122}
123
124impl_into_iterator! { RRangeFrom, RangeFrom }
125
126////////////////////////////////////////////////////////////////
127
128/// Ffi-safe equivalent of `::std::ops::RangeTo`
129#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
130#[repr(C)]
131#[derive(StableAbi)]
132pub struct RRangeTo<T> {
133    pub end: T,
134}
135
136impl_from_rust_repr! {
137    impl[T] From<RangeTo<T>> for RRangeTo<T> {
138        fn(v){
139            Self { end: v.end }
140        }
141    }
142}
143
144impl_into_rust_repr! {
145    impl[T] Into<RangeTo<T>> for RRangeTo<T> {
146        fn(this){
147            ..this.end
148        }
149    }
150}
151
152////////////////////////////////////////////////////////////////
153
154/// Ffi-safe equivalent of `::std::ops::RangeToInclusive`
155#[derive(Debug, Copy, Clone, PartialEq, Eq, Ord, PartialOrd, Hash, Deserialize, Serialize)]
156#[repr(C)]
157#[derive(StableAbi)]
158pub struct RRangeToInclusive<T> {
159    pub end: T,
160}
161
162impl_from_rust_repr! {
163    impl[T] From<RangeToInclusive<T>> for RRangeToInclusive<T> {
164        fn(v){
165            Self { end: v.end }
166        }
167    }
168}
169
170impl_into_rust_repr! {
171    impl[T] Into<RangeToInclusive<T>> for RRangeToInclusive<T> {
172        fn(this){
173             ..= this.end
174        }
175    }
176}
177
178////////////////////////////////////////////////////////////////