]> git.lizzy.rs Git - rust.git/blob - library/core/src/lib.rs
3b0872378c6e9f41f45806312464e0b149a8b938
[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 #![cfg_attr(
64     not(bootstrap),
65     doc(cfg_hide(
66         not(test),
67         any(not(feature = "miri-test-libstd"), test, doctest),
68         no_fp_fmt_parse,
69         target_pointer_width = "16",
70         target_pointer_width = "32",
71         target_pointer_width = "64",
72         target_has_atomic = "8",
73         target_has_atomic = "16",
74         target_has_atomic = "32",
75         target_has_atomic = "64",
76         target_has_atomic = "ptr",
77         target_has_atomic_equal_alignment = "8",
78         target_has_atomic_equal_alignment = "16",
79         target_has_atomic_equal_alignment = "32",
80         target_has_atomic_equal_alignment = "64",
81         target_has_atomic_equal_alignment = "ptr",
82         target_has_atomic_load_store = "8",
83         target_has_atomic_load_store = "16",
84         target_has_atomic_load_store = "32",
85         target_has_atomic_load_store = "64",
86         target_has_atomic_load_store = "ptr",
87     ))
88 )]
89 #![no_core]
90 //
91 // Lints:
92 #![deny(rust_2021_incompatible_or_patterns)]
93 #![deny(unsafe_op_in_unsafe_fn)]
94 #![warn(deprecated_in_future)]
95 #![warn(missing_debug_implementations)]
96 #![warn(missing_docs)]
97 #![allow(explicit_outlives_requirements)]
98 //
99 // Library features for const fns:
100 #![feature(const_align_offset)]
101 #![feature(const_align_of_val)]
102 #![feature(const_alloc_layout)]
103 #![feature(const_arguments_as_str)]
104 #![feature(const_assert_type)]
105 #![feature(const_bigint_helper_methods)]
106 #![feature(const_caller_location)]
107 #![feature(const_cell_into_inner)]
108 #![feature(const_discriminant)]
109 #![feature(const_eval_select)]
110 #![feature(const_float_bits_conv)]
111 #![feature(const_float_classify)]
112 #![feature(const_fmt_arguments_new)]
113 #![feature(const_heap)]
114 #![feature(const_inherent_unchecked_arith)]
115 #![feature(const_int_unchecked_arith)]
116 #![feature(const_intrinsic_copy)]
117 #![feature(const_intrinsic_forget)]
118 #![feature(const_likely)]
119 #![feature(const_maybe_uninit_as_ptr)]
120 #![feature(const_maybe_uninit_assume_init)]
121 #![feature(const_num_from_num)]
122 #![feature(const_ops)]
123 #![feature(const_option)]
124 #![feature(const_pin)]
125 #![feature(const_replace)]
126 #![feature(const_ptr_offset)]
127 #![feature(const_ptr_offset_from)]
128 #![feature(const_ptr_read)]
129 #![feature(const_ptr_write)]
130 #![feature(const_raw_ptr_comparison)]
131 #![feature(const_size_of_val)]
132 #![feature(const_slice_from_raw_parts)]
133 #![feature(const_slice_ptr_len)]
134 #![feature(const_str_from_utf8_unchecked_mut)]
135 #![feature(const_swap)]
136 #![feature(const_trait_impl)]
137 #![feature(const_type_id)]
138 #![feature(const_type_name)]
139 #![feature(const_default_impls)]
140 #![feature(duration_consts_2)]
141 #![feature(ptr_metadata)]
142 #![feature(slice_ptr_get)]
143 #![feature(str_internals)]
144 #![feature(variant_count)]
145 #![feature(const_array_from_ref)]
146 #![feature(const_slice_from_ref)]
147 //
148 // Language features:
149 #![feature(abi_unadjusted)]
150 #![feature(allow_internal_unsafe)]
151 #![feature(allow_internal_unstable)]
152 #![feature(asm)]
153 #![feature(associated_type_bounds)]
154 #![feature(auto_traits)]
155 #![feature(cfg_target_has_atomic)]
156 #![feature(const_fn_floating_point_arithmetic)]
157 #![feature(const_fn_fn_ptr_basics)]
158 #![feature(const_fn_trait_bound)]
159 #![feature(const_impl_trait)]
160 #![feature(const_mut_refs)]
161 #![feature(const_precise_live_drops)]
162 #![cfg_attr(bootstrap, feature(const_raw_ptr_deref))]
163 #![feature(const_refs_to_cell)]
164 #![feature(decl_macro)]
165 #![feature(doc_cfg)]
166 #![feature(doc_notable_trait)]
167 #![feature(doc_primitive)]
168 #![feature(exhaustive_patterns)]
169 #![feature(doc_cfg_hide)]
170 #![feature(extern_types)]
171 #![feature(fundamental)]
172 #![feature(if_let_guard)]
173 #![feature(intra_doc_pointers)]
174 #![feature(intrinsics)]
175 #![feature(lang_items)]
176 #![feature(link_llvm_intrinsics)]
177 #![feature(llvm_asm)]
178 #![feature(min_specialization)]
179 #![feature(mixed_integer_ops)]
180 #![feature(must_not_suspend)]
181 #![feature(negative_impls)]
182 #![feature(never_type)]
183 #![feature(no_core)]
184 #![feature(no_coverage)] // rust-lang/rust#84605
185 #![feature(no_niche)] // rust-lang/rust#68303
186 #![feature(platform_intrinsics)]
187 #![feature(prelude_import)]
188 #![feature(repr_simd)]
189 #![feature(rustc_allow_const_fn_unstable)]
190 #![feature(rustc_attrs)]
191 #![feature(simd_ffi)]
192 #![feature(staged_api)]
193 #![feature(stmt_expr_attributes)]
194 #![feature(trait_alias)]
195 #![feature(transparent_unions)]
196 #![feature(try_blocks)]
197 #![feature(unboxed_closures)]
198 #![feature(unsized_fn_params)]
199 #![cfg_attr(not(bootstrap), feature(asm_const))]
200 //
201 // Target features:
202 #![feature(aarch64_target_feature)]
203 #![feature(adx_target_feature)]
204 #![feature(arm_target_feature)]
205 #![feature(avx512_target_feature)]
206 #![feature(cmpxchg16b_target_feature)]
207 #![feature(f16c_target_feature)]
208 #![feature(hexagon_target_feature)]
209 #![feature(mips_target_feature)]
210 #![feature(powerpc_target_feature)]
211 #![feature(rtm_target_feature)]
212 #![feature(sse4a_target_feature)]
213 #![feature(tbm_target_feature)]
214 #![feature(wasm_target_feature)]
215
216 // allow using `core::` in intra-doc links
217 #[allow(unused_extern_crates)]
218 extern crate self as core;
219
220 #[prelude_import]
221 #[allow(unused)]
222 use prelude::v1::*;
223
224 #[cfg(not(test))] // See #65860
225 #[macro_use]
226 mod macros;
227
228 // We don't export this through #[macro_export] for now, to avoid breakage.
229 // See https://github.com/rust-lang/rust/issues/82913
230 #[cfg(not(test))]
231 #[unstable(feature = "assert_matches", issue = "82775")]
232 /// Unstable module containing the unstable `assert_matches` macro.
233 pub mod assert_matches {
234     #[unstable(feature = "assert_matches", issue = "82775")]
235     pub use crate::macros::{assert_matches, debug_assert_matches};
236 }
237
238 #[macro_use]
239 mod internal_macros;
240
241 #[path = "num/shells/int_macros.rs"]
242 #[macro_use]
243 mod int_macros;
244
245 #[path = "num/shells/i128.rs"]
246 pub mod i128;
247 #[path = "num/shells/i16.rs"]
248 pub mod i16;
249 #[path = "num/shells/i32.rs"]
250 pub mod i32;
251 #[path = "num/shells/i64.rs"]
252 pub mod i64;
253 #[path = "num/shells/i8.rs"]
254 pub mod i8;
255 #[path = "num/shells/isize.rs"]
256 pub mod isize;
257
258 #[path = "num/shells/u128.rs"]
259 pub mod u128;
260 #[path = "num/shells/u16.rs"]
261 pub mod u16;
262 #[path = "num/shells/u32.rs"]
263 pub mod u32;
264 #[path = "num/shells/u64.rs"]
265 pub mod u64;
266 #[path = "num/shells/u8.rs"]
267 pub mod u8;
268 #[path = "num/shells/usize.rs"]
269 pub mod usize;
270
271 #[path = "num/f32.rs"]
272 pub mod f32;
273 #[path = "num/f64.rs"]
274 pub mod f64;
275
276 #[macro_use]
277 pub mod num;
278
279 /* The libcore prelude, not as all-encompassing as the libstd prelude */
280
281 pub mod prelude;
282
283 /* Core modules for ownership management */
284
285 pub mod hint;
286 pub mod intrinsics;
287 pub mod mem;
288 pub mod ptr;
289
290 /* Core language traits */
291
292 pub mod borrow;
293 pub mod clone;
294 pub mod cmp;
295 pub mod convert;
296 pub mod default;
297 pub mod marker;
298 pub mod ops;
299
300 /* Core types and methods on primitives */
301
302 pub mod any;
303 pub mod array;
304 pub mod ascii;
305 pub mod cell;
306 pub mod char;
307 pub mod ffi;
308 pub mod iter;
309 #[unstable(feature = "once_cell", issue = "74465")]
310 pub mod lazy;
311 pub mod option;
312 pub mod panic;
313 pub mod panicking;
314 pub mod pin;
315 pub mod result;
316 #[unstable(feature = "async_stream", issue = "79024")]
317 pub mod stream;
318 pub mod sync;
319
320 pub mod fmt;
321 pub mod hash;
322 pub mod slice;
323 pub mod str;
324 pub mod time;
325
326 pub mod unicode;
327
328 /* Async */
329 pub mod future;
330 pub mod task;
331
332 /* Heap memory allocator trait */
333 #[allow(missing_docs)]
334 pub mod alloc;
335
336 // note: does not need to be public
337 mod bool;
338 mod tuple;
339 mod unit;
340
341 #[stable(feature = "core_primitive", since = "1.43.0")]
342 pub mod primitive;
343
344 // Pull in the `core_arch` crate directly into libcore. The contents of
345 // `core_arch` are in a different repository: rust-lang/stdarch.
346 //
347 // `core_arch` depends on libcore, but the contents of this module are
348 // set up in such a way that directly pulling it here works such that the
349 // crate uses the this crate as its libcore.
350 #[path = "../../stdarch/crates/core_arch/src/mod.rs"]
351 #[allow(
352     missing_docs,
353     missing_debug_implementations,
354     dead_code,
355     unused_imports,
356     unsafe_op_in_unsafe_fn
357 )]
358 #[allow(rustdoc::bare_urls)]
359 // FIXME: This annotation should be moved into rust-lang/stdarch after clashing_extern_declarations is
360 // merged. It currently cannot because bootstrap fails as the lint hasn't been defined yet.
361 #[allow(clashing_extern_declarations)]
362 #[unstable(feature = "stdsimd", issue = "48556")]
363 mod core_arch;
364
365 #[doc = include_str!("../../stdarch/crates/core_arch/src/core_arch_docs.md")]
366 #[stable(feature = "simd_arch", since = "1.27.0")]
367 pub mod arch {
368     #[stable(feature = "simd_arch", since = "1.27.0")]
369     pub use crate::core_arch::arch::*;
370
371     /// Inline assembly.
372     ///
373     /// Read the [unstable book] for the usage.
374     ///
375     /// [unstable book]: ../../unstable-book/library-features/asm.html
376     #[unstable(
377         feature = "asm",
378         issue = "72016",
379         reason = "inline assembly is not stable enough for use and is subject to change"
380     )]
381     #[rustc_builtin_macro]
382     pub macro asm("assembly template", $(operands,)* $(options($(option),*))?) {
383         /* compiler built-in */
384     }
385
386     /// Module-level inline assembly.
387     #[unstable(
388         feature = "global_asm",
389         issue = "35119",
390         reason = "`global_asm!` is not stable enough for use and is subject to change"
391     )]
392     #[rustc_builtin_macro]
393     pub macro global_asm("assembly template", $(operands,)* $(options($(option),*))?) {
394         /* compiler built-in */
395     }
396 }
397
398 // Pull in the `core_simd` crate directly into libcore. The contents of
399 // `core_simd` are in a different repository: rust-lang/portable-simd.
400 //
401 // `core_simd` depends on libcore, but the contents of this module are
402 // set up in such a way that directly pulling it here works such that the
403 // crate uses this crate as its libcore.
404 #[path = "../../portable-simd/crates/core_simd/src/mod.rs"]
405 #[allow(missing_debug_implementations, dead_code, unsafe_op_in_unsafe_fn, unused_unsafe)]
406 #[allow(rustdoc::bare_urls)]
407 #[unstable(feature = "portable_simd", issue = "86656")]
408 #[cfg(not(all(miri, doctest)))] // Miri does not support all SIMD intrinsics
409 #[cfg(not(bootstrap))]
410 mod core_simd;
411
412 #[doc = include_str!("../../portable-simd/crates/core_simd/src/core_simd_docs.md")]
413 #[unstable(feature = "portable_simd", issue = "86656")]
414 #[cfg(not(all(miri, doctest)))] // Miri does not support all SIMD intrinsics
415 #[cfg(not(bootstrap))]
416 pub mod simd {
417     #[unstable(feature = "portable_simd", issue = "86656")]
418     pub use crate::core_simd::simd::*;
419 }
420
421 include!("primitive_docs.rs");