]> git.lizzy.rs Git - rust.git/blob - crates/test_utils/src/minicore.rs
internal: switch some tests to minicore
[rust.git] / crates / test_utils / src / minicore.rs
1 //! This is a fixture we use for tests that need lang items.
2 //!
3 //! We want to include the minimal subset of core for each test, so this file
4 //! supports "conditional compilation". Tests use the following syntax to include minicore:
5 //!
6 //!  //- minicore: flag1, flag2
7 //!
8 //! We then strip all the code marked with other flags.
9 //!
10 //! Available flags:
11 //!     sized:
12 //!     unsize: sized
13 //!     coerce_unsized: unsize
14 //!     slice:
15 //!     range:
16 //!     deref: sized
17 //!     deref_mut: deref
18 //!     fn:
19 //!     pin:
20 //!     future: pin
21 //!     option:
22 //!     result:
23 //!     iterator: option
24
25 pub mod marker {
26     // region:sized
27     #[lang = "sized"]
28     #[fundamental]
29     #[rustc_specialization_trait]
30     pub trait Sized {}
31     // endregion:sized
32
33     // region:unsize
34     #[lang = "unsize"]
35     pub trait Unsize<T: ?Sized> {}
36     // endregion:unsize
37 }
38
39 pub mod ops {
40     // region:coerce_unsized
41     mod unsize {
42         use crate::marker::Unsize;
43
44         #[lang = "coerce_unsized"]
45         pub trait CoerceUnsized<T: ?Sized> {}
46
47         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
48         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
49         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
50         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
51
52         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
53         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
54
55         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
56         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
57         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
58     }
59     pub use self::unsize::CoerceUnsized;
60     // endregion:coerce_unsized
61
62     // region:deref
63     mod deref {
64         #[lang = "deref"]
65         pub trait Deref {
66             #[lang = "deref_target"]
67             type Target: ?Sized;
68             fn deref(&self) -> &Self::Target;
69         }
70         // region:deref_mut
71         #[lang = "deref_mut"]
72         pub trait DerefMut: Deref {
73             fn deref_mut(&mut self) -> &mut Self::Target;
74         }
75         // endregion:deref_mut
76     }
77     pub use self::deref::Deref;
78     pub use self::deref::DerefMut; //:deref_mut
79                                    // endregion:deref
80
81     // region:range
82     mod range {
83         #[lang = "RangeFull"]
84         pub struct RangeFull;
85
86         #[lang = "Range"]
87         pub struct Range<Idx> {
88             pub start: Idx,
89             pub end: Idx,
90         }
91
92         #[lang = "RangeFrom"]
93         pub struct RangeFrom<Idx> {
94             pub start: Idx,
95         }
96
97         #[lang = "RangeTo"]
98         pub struct RangeTo<Idx> {
99             pub end: Idx,
100         }
101
102         #[lang = "RangeInclusive"]
103         pub struct RangeInclusive<Idx> {
104             pub(crate) start: Idx,
105             pub(crate) end: Idx,
106             pub(crate) exhausted: bool,
107         }
108
109         #[lang = "RangeToInclusive"]
110         pub struct RangeToInclusive<Idx> {
111             pub end: Idx,
112         }
113     }
114     pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
115     pub use self::range::{RangeInclusive, RangeToInclusive};
116     // endregion:range
117
118     // region:fn
119     mod function {
120         #[lang = "fn"]
121         #[fundamental]
122         pub trait Fn<Args>: FnMut<Args> {}
123
124         #[lang = "fn_mut"]
125         #[fundamental]
126         pub trait FnMut<Args>: FnOnce<Args> {}
127
128         #[lang = "fn_once"]
129         #[fundamental]
130         pub trait FnOnce<Args> {
131             #[lang = "fn_once_output"]
132             type Output;
133         }
134     }
135     pub use self::function::{Fn, FnMut, FnOnce};
136     // endregion:fn
137 }
138
139 // region:slice
140 pub mod slice {
141     #[lang = "slice"]
142     impl<T> [T] {
143         pub fn len(&self) -> usize {
144             loop {}
145         }
146     }
147 }
148 // endregion:slice
149
150 // region:option
151 pub mod option {
152     pub enum Option<T> {
153         #[lang = "None"]
154         None,
155         #[lang = "Some"]
156         Some(T),
157     }
158 }
159 // endregion:option
160
161 // region:result
162 pub mod result {
163     pub enum Result<T, E> {
164         #[lang = "Ok"]
165         Ok(T),
166         #[lang = "Err"]
167         Err(E),
168     }
169 }
170 // endregion:result
171
172 // region:pin
173 pub mod pin {
174     #[lang = "pin"]
175     #[fundamental]
176     pub struct Pin<P> {
177         pointer: P,
178     }
179 }
180 // endregion:pin
181
182 // region:future
183 pub mod future {
184     use crate::{
185         pin::Pin,
186         task::{Context, Poll},
187     };
188
189     #[lang = "future_trait"]
190     pub trait Future {
191         type Output;
192         #[lang = "poll"]
193         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
194     }
195 }
196 pub mod task {
197     pub enum Poll<T> {
198         #[lang = "Ready"]
199         Ready(T),
200         #[lang = "Pending"]
201         Pending,
202     }
203
204     pub struct Context<'a> {
205         waker: &'a (),
206     }
207 }
208 // endregion:future
209
210 // region:iterator
211 pub mod iter {
212     mod traits {
213         mod iterator {
214             pub trait Iterator {
215                 type Item;
216                 #[lang = "next"]
217                 fn next(&mut self) -> Option<Self::Item>;
218                 fn nth(&mut self, n: usize) -> Option<Self::Item> {
219                     loop {}
220                 }
221             }
222         }
223         mod collect {
224             pub trait IntoIterator {
225                 type Item;
226                 type IntoIter: Iterator<Item = Self::Item>;
227                 #[lang = "into_iter"]
228                 fn into_iter(self) -> Self::IntoIter;
229             }
230             impl<I: Iterator> IntoIterator for I {
231                 type Item = I::Item;
232                 type IntoIter = I;
233                 fn into_iter(self) -> I {
234                     self
235                 }
236             }
237         }
238         pub use self::collect::IntoIterator;
239         pub use self::iterator::Iterator;
240     }
241     pub use self::traits::{IntoIterator, Iterator};
242 }
243 // endregion:iterator
244
245 pub mod prelude {
246     pub mod v1 {
247         pub use crate::{
248             iter::{IntoIterator, Iterator},     // :iterator
249             marker::Sized,                      // :sized
250             ops::{Fn, FnMut, FnOnce},           // :fn
251             option::Option::{self, None, Some}, // :option
252             result::Result::{self, Err, Ok},    // :result
253         };
254     }
255
256     pub mod rust_2015 {
257         pub use super::v1::*;
258     }
259
260     pub mod rust_2018 {
261         pub use super::v1::*;
262     }
263
264     pub mod rust_2021 {
265         pub use super::v1::*;
266     }
267 }
268
269 #[prelude_import]
270 #[allow(unused)]
271 use prelude::v1::*;