]> git.lizzy.rs Git - rust.git/blob - crates/test_utils/src/minicore.rs
5ff60178ce651e087616c9c2fd0307f7f4852a59
[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 //!     slice:
13 //!     range:
14 //!     unsize: sized
15 //!     deref: sized
16 //!     coerce_unsized: unsize
17 //!     pin:
18 //!     future: pin
19 //!     option:
20 //!     result:
21
22 pub mod marker {
23     // region:sized
24     #[lang = "sized"]
25     #[fundamental]
26     #[rustc_specialization_trait]
27     pub trait Sized {}
28     // endregion:sized
29
30     // region:unsize
31     #[lang = "unsize"]
32     pub trait Unsize<T: ?Sized> {}
33     // endregion:unsize
34 }
35
36 pub mod ops {
37     // region:coerce_unsized
38     mod unsize {
39         use crate::marker::Unsize;
40
41         #[lang = "coerce_unsized"]
42         pub trait CoerceUnsized<T: ?Sized> {}
43
44         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a mut U> for &'a mut T {}
45         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b mut T {}
46         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for &'a mut T {}
47         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a mut T {}
48
49         impl<'a, 'b: 'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<&'a U> for &'b T {}
50         impl<'a, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for &'a T {}
51
52         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*mut U> for *mut T {}
53         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *mut T {}
54         impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<*const U> for *const T {}
55     }
56     pub use self::unsize::CoerceUnsized;
57     // endregion:coerce_unsized
58
59     // region:deref
60     mod deref {
61         #[lang = "deref"]
62         pub trait Deref {
63             #[lang = "deref_target"]
64             type Target: ?Sized;
65             fn deref(&self) -> &Self::Target;
66         }
67     }
68     pub use self::deref::Deref;
69     // endregion:deref
70
71     // region:range
72     mod range {
73         #[lang = "RangeFull"]
74         pub struct RangeFull;
75
76         #[lang = "Range"]
77         pub struct Range<Idx> {
78             pub start: Idx,
79             pub end: Idx,
80         }
81
82         #[lang = "RangeFrom"]
83         pub struct RangeFrom<Idx> {
84             pub start: Idx,
85         }
86
87         #[lang = "RangeTo"]
88         pub struct RangeTo<Idx> {
89             pub end: Idx,
90         }
91
92         #[lang = "RangeInclusive"]
93         pub struct RangeInclusive<Idx> {
94             pub(crate) start: Idx,
95             pub(crate) end: Idx,
96             pub(crate) exhausted: bool,
97         }
98
99         #[lang = "RangeToInclusive"]
100         pub struct RangeToInclusive<Idx> {
101             pub end: Idx,
102         }
103     }
104     pub use self::range::{Range, RangeFrom, RangeFull, RangeTo};
105     pub use self::range::{RangeInclusive, RangeToInclusive};
106     // endregion:range
107 }
108
109 // region:slice
110 pub mod slice {
111     #[lang = "slice"]
112     impl<T> [T] {
113         pub fn len(&self) -> usize {
114             loop {}
115         }
116     }
117 }
118 // endregion:slice
119
120 // region:option
121 pub mod option {
122     pub enum Option<T> {
123         #[lang = "None"]
124         None,
125         #[lang = "Some"]
126         Some(T),
127     }
128 }
129 // endregion:option
130
131 // region:result
132 pub mod result {
133     pub enum Result<T, E> {
134         #[lang = "Ok"]
135         Ok(T),
136         #[lang = "Err"]
137         Err(E),
138     }
139 }
140 // endregion:result
141
142 // region:pin
143 pub mod pin {
144     #[lang = "pin"]
145     #[fundamental]
146     pub struct Pin<P> {
147         pointer: P,
148     }
149 }
150 // endregion:pin
151
152 // region:future
153 pub mod future {
154     use crate::{
155         pin::Pin,
156         task::{Context, Poll},
157     };
158
159     #[lang = "future_trait"]
160     pub trait Future {
161         type Output;
162         #[lang = "poll"]
163         fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output>;
164     }
165 }
166 pub mod task {
167     pub enum Poll<T> {
168         #[lang = "Ready"]
169         Ready(T),
170         #[lang = "Pending"]
171         Pending,
172     }
173
174     pub struct Context<'a> {
175         waker: &'a (),
176     }
177 }
178 // endregion:future
179
180 pub mod prelude {
181     pub mod v1 {
182         pub use crate::{
183             marker::Sized,                      // :sized
184             option::Option::{self, None, Some}, // :option
185             result::Result::{self, Err, Ok},    // :result
186         };
187     }
188
189     pub mod rust_2015 {
190         pub use super::v1::*;
191     }
192
193     pub mod rust_2018 {
194         pub use super::v1::*;
195     }
196
197     pub mod rust_2021 {
198         pub use super::v1::*;
199     }
200 }
201
202 #[prelude_import]
203 #[allow(unused)]
204 use prelude::v1::*;