]> git.lizzy.rs Git - rust.git/blob - library/core/src/lib.rs
1a6d1aed2fd1edea75668e17f86fabb92962b917
[rust.git] / library / core / src / lib.rs
1 //! # The Rust Core Library
2 //!
3 //! The Rust Core Library is the dependency-free[^free] foundation of [The
4 //! Rust Standard Library](../std/index.html). It is the portable glue
5 //! between the language and its libraries, defining the intrinsic and
6 //! primitive building blocks of all Rust code. It links to no
7 //! upstream libraries, no system libraries, and no libc.
8 //!
9 //! [^free]: Strictly speaking, there are some symbols which are needed but
10 //!          they aren't always necessary.
11 //!
12 //! The core library is *minimal*: it isn't even aware of heap allocation,
13 //! nor does it provide concurrency or I/O. These things require
14 //! platform integration, and this library is platform-agnostic.
15 //!
16 //! # How to use the core library
17 //!
18 //! Please note that all of these details are currently not considered stable.
19 //!
20 // FIXME: Fill me in with more detail when the interface settles
21 //! This library is built on the assumption of a few existing symbols:
22 //!
23 //! * `memcpy`, `memcmp`, `memset` - These are core memory routines which are
24 //!   often generated by LLVM. Additionally, this library can make explicit
25 //!   calls to these functions. Their signatures are the same as found in C.
26 //!   These functions are often provided by the system libc, but can also be
27 //!   provided by the [compiler-builtins crate](https://crates.io/crates/compiler_builtins).
28 //!
29 //! * `rust_begin_panic` - This function takes four arguments, a
30 //!   `fmt::Arguments`, a `&'static str`, and two `u32`'s. These four arguments
31 //!   dictate the panic message, the file at which panic was invoked, and the
32 //!   line and column inside the file. It is up to consumers of this core
33 //!   library to define this panic function; it is only required to never
34 //!   return. This requires a `lang` attribute named `panic_impl`.
35 //!
36 //! * `rust_eh_personality` - is used by the failure mechanisms of the
37 //!    compiler. This is often mapped to GCC's personality function, but crates
38 //!    which do not trigger a panic can be assured that this function is never
39 //!    called. The `lang` attribute is called `eh_personality`.
40
41 // Since libcore defines many fundamental lang items, all tests live in a
42 // separate crate, libcoretest, to avoid bizarre issues.
43 //
44 // Here we explicitly #[cfg]-out this whole crate when testing. If we don't do
45 // this, both the generated test artifact and the linked libtest (which
46 // transitively includes libcore) will both define the same set of lang items,
47 // and this will cause the E0152 "found duplicate lang item" error. See
48 // discussion in #50466 for details.
49 //
50 // This cfg won't affect doc tests.
51 #![cfg(not(test))]
52 // To run libcore tests without x.py without ending up with two copies of libcore, Miri needs to be
53 // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
54 // rustc itself never sets the feature, so this line has no affect there.
55 #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
56 #![stable(feature = "core", since = "1.6.0")]
57 #![doc(
58     html_playground_url = "https://play.rust-lang.org/",
59     issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
60     test(no_crate_inject, attr(deny(warnings))),
61     test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
62 )]
63 #![no_core]
64 //
65 // Lints:
66 #![deny(rust_2021_incompatible_or_patterns)]
67 #![deny(unsafe_op_in_unsafe_fn)]
68 #![warn(deprecated_in_future)]
69 #![warn(missing_debug_implementations)]
70 #![warn(missing_docs)]
71 #![allow(explicit_outlives_requirements)]
72 #![cfg_attr(bootstrap, allow(incomplete_features))] // if_let_guard
73 //
74 // Library features for const fns:
75 #![feature(const_align_of_val)]
76 #![feature(const_alloc_layout)]
77 #![feature(const_arguments_as_str)]
78 #![feature(const_assert_type)]
79 #![feature(const_caller_location)]
80 #![feature(const_cell_into_inner)]
81 #![feature(const_discriminant)]
82 #![feature(const_float_bits_conv)]
83 #![feature(const_float_classify)]
84 #![feature(const_heap)]
85 #![feature(const_inherent_unchecked_arith)]
86 #![feature(const_int_unchecked_arith)]
87 #![feature(const_intrinsic_copy)]
88 #![feature(const_intrinsic_forget)]
89 #![feature(const_likely)]
90 #![feature(const_maybe_uninit_as_ptr)]
91 #![feature(const_maybe_uninit_assume_init)]
92 #![feature(const_option)]
93 #![feature(const_pin)]
94 #![feature(const_ptr_offset)]
95 #![feature(const_ptr_offset_from)]
96 #![feature(const_ptr_read)]
97 #![feature(const_ptr_write)]
98 #![feature(const_raw_ptr_comparison)]
99 #![feature(const_size_of_val)]
100 #![feature(const_slice_from_raw_parts)]
101 #![feature(const_slice_ptr_len)]
102 #![feature(const_swap)]
103 #![feature(const_trait_impl)]
104 #![feature(const_type_id)]
105 #![feature(const_type_name)]
106 #![feature(const_unreachable_unchecked)]
107 #![feature(const_default_impls)]
108 #![feature(duration_consts_2)]
109 #![feature(ptr_metadata)]
110 #![feature(slice_ptr_get)]
111 #![feature(variant_count)]
112 //
113 // Language features:
114 #![feature(abi_unadjusted)]
115 #![feature(allow_internal_unsafe)]
116 #![feature(allow_internal_unstable)]
117 #![feature(asm)]
118 #![feature(associated_type_bounds)]
119 #![feature(auto_traits)]
120 #![feature(cfg_target_has_atomic)]
121 #![feature(const_fn_floating_point_arithmetic)]
122 #![feature(const_fn_fn_ptr_basics)]
123 #![feature(const_fn_trait_bound)]
124 #![cfg_attr(bootstrap, feature(const_fn_transmute))]
125 #![cfg_attr(bootstrap, feature(const_fn_union))]
126 #![feature(const_impl_trait)]
127 #![feature(const_mut_refs)]
128 #![feature(const_panic)]
129 #![feature(const_precise_live_drops)]
130 #![feature(const_raw_ptr_deref)]
131 #![feature(const_refs_to_cell)]
132 #![feature(decl_macro)]
133 #![feature(doc_cfg)]
134 #![feature(doc_notable_trait)]
135 #![feature(exhaustive_patterns)]
136 #![feature(extern_types)]
137 #![feature(fundamental)]
138 #![feature(if_let_guard)]
139 #![feature(intra_doc_pointers)]
140 #![feature(intrinsics)]
141 #![feature(lang_items)]
142 #![feature(link_llvm_intrinsics)]
143 #![feature(llvm_asm)]
144 #![feature(min_specialization)]
145 #![feature(negative_impls)]
146 #![feature(never_type)]
147 #![feature(no_core)]
148 #![feature(no_coverage)] // rust-lang/rust#84605
149 #![feature(no_niche)] // rust-lang/rust#68303
150 #![feature(platform_intrinsics)]
151 #![feature(prelude_import)]
152 #![feature(repr_simd)]
153 #![feature(rustc_allow_const_fn_unstable)]
154 #![feature(rustc_attrs)]
155 #![feature(simd_ffi)]
156 #![feature(staged_api)]
157 #![feature(stmt_expr_attributes)]
158 #![feature(trait_alias)]
159 #![feature(transparent_unions)]
160 #![feature(try_blocks)]
161 #![feature(unboxed_closures)]
162 #![feature(unsized_fn_params)]
163 //
164 // Target features:
165 #![feature(aarch64_target_feature)]
166 #![feature(adx_target_feature)]
167 #![feature(arm_target_feature)]
168 #![feature(avx512_target_feature)]
169 #![feature(cmpxchg16b_target_feature)]
170 #![feature(f16c_target_feature)]
171 #![feature(hexagon_target_feature)]
172 #![feature(mips_target_feature)]
173 #![feature(powerpc_target_feature)]
174 #![feature(rtm_target_feature)]
175 #![feature(sse4a_target_feature)]
176 #![feature(tbm_target_feature)]
177 #![feature(wasm_target_feature)]
178
179 // allow using `core::` in intra-doc links
180 #[allow(unused_extern_crates)]
181 extern crate self as core;
182
183 #[prelude_import]
184 #[allow(unused)]
185 use prelude::v1::*;
186
187 #[cfg(not(test))] // See #65860
188 #[macro_use]
189 mod macros;
190
191 // We don't export this through #[macro_export] for now, to avoid breakage.
192 // See https://github.com/rust-lang/rust/issues/82913
193 #[cfg(not(test))]
194 #[unstable(feature = "assert_matches", issue = "82775")]
195 /// Unstable module containing the unstable `assert_matches` macro.
196 pub mod assert_matches {
197     #[unstable(feature = "assert_matches", issue = "82775")]
198     pub use crate::macros::{assert_matches, debug_assert_matches};
199 }
200
201 #[macro_use]
202 mod internal_macros;
203
204 #[path = "num/shells/int_macros.rs"]
205 #[macro_use]
206 mod int_macros;
207
208 #[path = "num/shells/i128.rs"]
209 pub mod i128;
210 #[path = "num/shells/i16.rs"]
211 pub mod i16;
212 #[path = "num/shells/i32.rs"]
213 pub mod i32;
214 #[path = "num/shells/i64.rs"]
215 pub mod i64;
216 #[path = "num/shells/i8.rs"]
217 pub mod i8;
218 #[path = "num/shells/isize.rs"]
219 pub mod isize;
220
221 #[path = "num/shells/u128.rs"]
222 pub mod u128;
223 #[path = "num/shells/u16.rs"]
224 pub mod u16;
225 #[path = "num/shells/u32.rs"]
226 pub mod u32;
227 #[path = "num/shells/u64.rs"]
228 pub mod u64;
229 #[path = "num/shells/u8.rs"]
230 pub mod u8;
231 #[path = "num/shells/usize.rs"]
232 pub mod usize;
233
234 #[path = "num/f32.rs"]
235 pub mod f32;
236 #[path = "num/f64.rs"]
237 pub mod f64;
238
239 #[macro_use]
240 pub mod num;
241
242 /* The libcore prelude, not as all-encompassing as the libstd prelude */
243
244 pub mod prelude;
245
246 /* Core modules for ownership management */
247
248 pub mod hint;
249 pub mod intrinsics;
250 pub mod mem;
251 pub mod ptr;
252
253 /* Core language traits */
254
255 pub mod borrow;
256 pub mod clone;
257 pub mod cmp;
258 pub mod convert;
259 pub mod default;
260 pub mod marker;
261 pub mod ops;
262
263 /* Core types and methods on primitives */
264
265 pub mod any;
266 pub mod array;
267 pub mod ascii;
268 pub mod cell;
269 pub mod char;
270 pub mod ffi;
271 pub mod iter;
272 #[unstable(feature = "once_cell", issue = "74465")]
273 pub mod lazy;
274 pub mod option;
275 pub mod panic;
276 pub mod panicking;
277 pub mod pin;
278 pub mod result;
279 #[unstable(feature = "async_stream", issue = "79024")]
280 pub mod stream;
281 pub mod sync;
282
283 pub mod fmt;
284 pub mod hash;
285 pub mod slice;
286 pub mod str;
287 pub mod time;
288
289 pub mod unicode;
290
291 /* Async */
292 pub mod future;
293 pub mod task;
294
295 /* Heap memory allocator trait */
296 #[allow(missing_docs)]
297 pub mod alloc;
298
299 // note: does not need to be public
300 mod bool;
301 mod tuple;
302 mod unit;
303
304 #[stable(feature = "core_primitive", since = "1.43.0")]
305 pub mod primitive;
306
307 // Pull in the `core_arch` crate directly into libcore. The contents of
308 // `core_arch` are in a different repository: rust-lang/stdarch.
309 //
310 // `core_arch` depends on libcore, but the contents of this module are
311 // set up in such a way that directly pulling it here works such that the
312 // crate uses the this crate as its libcore.
313 #[path = "../../stdarch/crates/core_arch/src/mod.rs"]
314 #[allow(
315     missing_docs,
316     missing_debug_implementations,
317     dead_code,
318     unused_imports,
319     unsafe_op_in_unsafe_fn
320 )]
321 #[allow(rustdoc::bare_urls)]
322 // FIXME: This annotation should be moved into rust-lang/stdarch after clashing_extern_declarations is
323 // merged. It currently cannot because bootstrap fails as the lint hasn't been defined yet.
324 #[allow(clashing_extern_declarations)]
325 #[unstable(feature = "stdsimd", issue = "48556")]
326 mod core_arch;
327
328 #[doc = include_str!("../../stdarch/crates/core_arch/src/core_arch_docs.md")]
329 #[stable(feature = "simd_arch", since = "1.27.0")]
330 pub mod arch {
331     #[stable(feature = "simd_arch", since = "1.27.0")]
332     pub use crate::core_arch::arch::*;
333
334     /// Inline assembly.
335     ///
336     /// Read the [unstable book] for the usage.
337     ///
338     /// [unstable book]: ../../unstable-book/library-features/asm.html
339     #[unstable(
340         feature = "asm",
341         issue = "72016",
342         reason = "inline assembly is not stable enough for use and is subject to change"
343     )]
344     #[rustc_builtin_macro]
345     pub macro asm("assembly template", $(operands,)* $(options($(option),*))?) {
346         /* compiler built-in */
347     }
348
349     /// Module-level inline assembly.
350     #[unstable(
351         feature = "global_asm",
352         issue = "35119",
353         reason = "`global_asm!` is not stable enough for use and is subject to change"
354     )]
355     #[rustc_builtin_macro]
356     pub macro global_asm("assembly template", $(operands,)* $(options($(option),*))?) {
357         /* compiler built-in */
358     }
359 }