rust/library/core/src/lib.rs

317 lines
9.5 KiB
Rust
Raw Normal View History

2014-05-22 16:44:54 +00:00
//! # The Rust Core Library
2014-05-13 04:22:35 +00:00
//!
//! The Rust Core Library is the dependency-free[^free] foundation of [The
2014-05-20 04:53:00 +00:00
//! Rust Standard Library](../std/index.html). It is the portable glue
//! between the language and its libraries, defining the intrinsic and
//! primitive building blocks of all Rust code. It links to no
2014-05-20 18:39:40 +00:00
//! upstream libraries, no system libraries, and no libc.
2014-05-20 04:53:00 +00:00
//!
//! [^free]: Strictly speaking, there are some symbols which are needed but
2015-11-18 10:35:29 +00:00
//! they aren't always necessary.
//!
2014-05-20 04:53:00 +00:00
//! The core library is *minimal*: it isn't even aware of heap allocation,
//! nor does it provide concurrency or I/O. These things require
2014-05-20 18:39:40 +00:00
//! platform integration, and this library is platform-agnostic.
2014-05-20 04:53:00 +00:00
//!
//! # How to use the core library
//!
//! Please note that all of these details are currently not considered stable.
//!
2014-05-20 17:40:14 +00:00
// FIXME: Fill me in with more detail when the interface settles
2014-05-20 04:53:00 +00:00
//! This library is built on the assumption of a few existing symbols:
2014-05-13 04:22:35 +00:00
//!
//! * `memcpy`, `memcmp`, `memset` - These are core memory routines which are
//! often generated by LLVM. Additionally, this library can make explicit
2014-05-19 15:51:16 +00:00
//! calls to these functions. Their signatures are the same as found in C.
//! These functions are often provided by the system libc, but can also be
//! provided by the [compiler-builtins crate](https://crates.io/crates/compiler_builtins).
2014-05-13 04:22:35 +00:00
//!
2017-06-28 00:41:24 +00:00
//! * `rust_begin_panic` - This function takes four arguments, a
//! `fmt::Arguments`, a `&'static str`, and two `u32`'s. These four arguments
//! dictate the panic message, the file at which panic was invoked, and the
2017-06-27 02:26:52 +00:00
//! line and column inside the file. It is up to consumers of this core
//! library to define this panic function; it is only required to never
2018-04-30 08:57:11 +00:00
//! return. This requires a `lang` attribute named `panic_impl`.
//!
//! * `rust_eh_personality` - is used by the failure mechanisms of the
//! compiler. This is often mapped to GCC's personality function, but crates
//! which do not trigger a panic can be assured that this function is never
//! called. The `lang` attribute is called `eh_personality`.
// Since libcore defines many fundamental lang items, all tests live in a
// separate crate, libcoretest, to avoid bizarre issues.
2018-05-05 19:29:19 +00:00
//
// Here we explicitly #[cfg]-out this whole crate when testing. If we don't do
// this, both the generated test artifact and the linked libtest (which
// transitively includes libcore) will both define the same set of lang items,
// and this will cause the E0152 "found duplicate lang item" error. See
2018-05-05 19:29:19 +00:00
// discussion in #50466 for details.
//
// This cfg won't affect doc tests.
#![cfg(not(test))]
std: Stabilize APIs for the 1.6 release This commit is the standard API stabilization commit for the 1.6 release cycle. The list of issues and APIs below have all been through their cycle-long FCP and the libs team decisions are listed below Stabilized APIs * `Read::read_exact` * `ErrorKind::UnexpectedEof` (renamed from `UnexpectedEOF`) * libcore -- this was a bit of a nuanced stabilization, the crate itself is now marked as `#[stable]` and the methods appearing via traits for primitives like `char` and `str` are now also marked as stable. Note that the extension traits themeselves are marked as unstable as they're imported via the prelude. The `try!` macro was also moved from the standard library into libcore to have the same interface. Otherwise the functions all have copied stability from the standard library now. * The `#![no_std]` attribute * `fs::DirBuilder` * `fs::DirBuilder::new` * `fs::DirBuilder::recursive` * `fs::DirBuilder::create` * `os::unix::fs::DirBuilderExt` * `os::unix::fs::DirBuilderExt::mode` * `vec::Drain` * `vec::Vec::drain` * `string::Drain` * `string::String::drain` * `vec_deque::Drain` * `vec_deque::VecDeque::drain` * `collections::hash_map::Drain` * `collections::hash_map::HashMap::drain` * `collections::hash_set::Drain` * `collections::hash_set::HashSet::drain` * `collections::binary_heap::Drain` * `collections::binary_heap::BinaryHeap::drain` * `Vec::extend_from_slice` (renamed from `push_all`) * `Mutex::get_mut` * `Mutex::into_inner` * `RwLock::get_mut` * `RwLock::into_inner` * `Iterator::min_by_key` (renamed from `min_by`) * `Iterator::max_by_key` (renamed from `max_by`) Deprecated APIs * `ErrorKind::UnexpectedEOF` (renamed to `UnexpectedEof`) * `OsString::from_bytes` * `OsStr::to_cstring` * `OsStr::to_bytes` * `fs::walk_dir` and `fs::WalkDir` * `path::Components::peek` * `slice::bytes::MutableByteVector` * `slice::bytes::copy_memory` * `Vec::push_all` (renamed to `extend_from_slice`) * `Duration::span` * `IpAddr` * `SocketAddr::ip` * `Read::tee` * `io::Tee` * `Write::broadcast` * `io::Broadcast` * `Iterator::min_by` (renamed to `min_by_key`) * `Iterator::max_by` (renamed to `max_by_key`) * `net::lookup_addr` New APIs (still unstable) * `<[T]>::sort_by_key` (added to mirror `min_by_key`) Closes #27585 Closes #27704 Closes #27707 Closes #27710 Closes #27711 Closes #27727 Closes #27740 Closes #27744 Closes #27799 Closes #27801 cc #27801 (doesn't close as `Chars` is still unstable) Closes #28968
2015-12-03 01:31:49 +00:00
#![stable(feature = "core", since = "1.6.0")]
2019-12-22 22:42:04 +00:00
#![doc(
html_root_url = "https://doc.rust-lang.org/nightly/",
html_playground_url = "https://play.rust-lang.org/",
issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
test(no_crate_inject, attr(deny(warnings))),
test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
)]
#![no_core]
#![warn(deprecated_in_future)]
#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
2019-04-15 02:23:21 +00:00
#![allow(explicit_outlives_requirements)]
2020-11-19 20:01:48 +00:00
#![feature(rustc_allow_const_fn_unstable)]
#![feature(allow_internal_unstable)]
2018-05-23 00:07:51 +00:00
#![feature(arbitrary_self_types)]
#![feature(asm)]
2017-03-17 14:05:44 +00:00
#![feature(cfg_target_has_atomic)]
2020-12-30 13:04:59 +00:00
#![feature(const_heap)]
#![feature(const_alloc_layout)]
#![feature(const_assert_type)]
2020-03-08 13:24:32 +00:00
#![feature(const_discriminant)]
#![feature(const_cell_into_inner)]
#![feature(const_intrinsic_copy)]
#![feature(const_intrinsic_forget)]
#![feature(const_float_classify)]
#![feature(const_float_bits_conv)]
#![feature(const_int_unchecked_arith)]
#![feature(const_inherent_unchecked_arith)]
#![feature(const_mut_refs)]
#![feature(const_refs_to_cell)]
2019-12-27 01:59:55 +00:00
#![feature(const_panic)]
#![feature(const_pin)]
#![cfg_attr(bootstrap, feature(const_fn))]
#![feature(const_fn_union)]
2020-11-19 20:01:48 +00:00
#![feature(const_impl_trait)]
2020-10-07 22:56:26 +00:00
#![feature(const_fn_floating_point_arithmetic)]
#![feature(const_fn_fn_ptr_basics)]
#![cfg_attr(not(bootstrap), feature(const_fn_trait_bound))]
2020-07-01 13:07:23 +00:00
#![feature(const_option)]
#![feature(const_precise_live_drops)]
#![feature(const_ptr_offset)]
2019-12-18 17:00:59 +00:00
#![feature(const_ptr_offset_from)]
2020-12-26 00:27:13 +00:00
#![feature(const_ptr_read)]
#![feature(const_ptr_write)]
2020-07-16 13:12:59 +00:00
#![feature(const_raw_ptr_comparison)]
2020-12-02 02:01:18 +00:00
#![feature(const_raw_ptr_deref)]
#![feature(const_slice_from_raw_parts)]
#![feature(const_slice_ptr_len)]
2020-07-29 21:56:58 +00:00
#![feature(const_size_of_val)]
#![feature(const_swap)]
2020-07-29 21:56:58 +00:00
#![feature(const_align_of_val)]
#![feature(const_type_id)]
2019-12-18 17:00:59 +00:00
#![feature(const_type_name)]
#![feature(const_likely)]
2020-07-17 19:57:13 +00:00
#![feature(const_unreachable_unchecked)]
#![feature(const_maybe_uninit_assume_init)]
2020-12-07 23:05:26 +00:00
#![feature(const_maybe_uninit_as_ptr)]
#![feature(custom_inner_attributes)]
#![feature(decl_macro)]
#![feature(doc_cfg)]
#![cfg_attr(bootstrap, feature(doc_spotlight))]
#![cfg_attr(not(bootstrap), feature(doc_notable_trait))]
#![feature(duration_consts_2)]
Stabilize extended_key_value_attributes # Stabilization report ## Summary This stabilizes using macro expansion in key-value attributes, like so: ```rust #[doc = include_str!("my_doc.md")] struct S; #[path = concat!(env!("OUT_DIR"), "/generated.rs")] mod m; ``` See the changes to the reference for details on what macros are allowed; see Petrochenkov's excellent blog post [on internals](https://internals.rust-lang.org/t/macro-expansion-points-in-attributes/11455) for alternatives that were considered and rejected ("why accept no more and no less?") This has been available on nightly since 1.50 with no major issues. ## Notes ### Accepted syntax The parser accepts arbitrary Rust expressions in this position, but any expression other than a macro invocation will ultimately lead to an error because it is not expected by the built-in expression forms (e.g., `#[doc]`). Note that decorators and the like may be able to observe other expression forms. ### Expansion ordering Expansion of macro expressions in "inert" attributes occurs after decorators have executed, analogously to macro expressions appearing in the function body or other parts of decorator input. There is currently no way for decorators to accept macros in key-value position if macro expansion must be performed before the decorator executes (if the macro can simply be copied into the output for later expansion, that can work). ## Test cases - https://github.com/rust-lang/rust/blob/master/src/test/ui/attributes/key-value-expansion-on-mac.rs - https://github.com/rust-lang/rust/blob/master/src/test/rustdoc/external-doc.rs The feature has also been dogfooded extensively in the compiler and standard library: - https://github.com/rust-lang/rust/pull/83329 - https://github.com/rust-lang/rust/pull/83230 - https://github.com/rust-lang/rust/pull/82641 - https://github.com/rust-lang/rust/pull/80534 ## Implementation history - Initial proposal: https://github.com/rust-lang/rust/issues/55414#issuecomment-554005412 - Experiment to see how much code it would break: https://github.com/rust-lang/rust/pull/67121 - Preliminary work to restrict expansion that would conflict with this feature: https://github.com/rust-lang/rust/pull/77271 - Initial implementation: https://github.com/rust-lang/rust/pull/78837 - Fix for an ICE: https://github.com/rust-lang/rust/pull/80563 ## Unresolved Questions ~~https://github.com/rust-lang/rust/pull/83366#issuecomment-805180738 listed some concerns, but they have been resolved as of this final report.~~ ## Additional Information There are two workarounds that have a similar effect for `#[doc]` attributes on nightly. One is to emulate this behavior by using a limited version of this feature that was stabilized for historical reasons: ```rust macro_rules! forward_inner_docs { ($e:expr => $i:item) => { #[doc = $e] $i }; } forward_inner_docs!(include_str!("lib.rs") => struct S {}); ``` This also works for other attributes (like `#[path = concat!(...)]`). The other is to use `doc(include)`: ```rust #![feature(external_doc)] #[doc(include = "lib.rs")] struct S {} ``` The first works, but is non-trivial for people to discover, and difficult to read and maintain. The second is a strange special-case for a particular use of the macro. This generalizes it to work for any use case, not just including files. I plan to remove `doc(include)` when this is stabilized. The `forward_inner_docs` workaround will still compile without warnings, but I expect it to be used less once it's no longer necessary.
2021-03-22 05:10:10 +00:00
#![cfg_attr(bootstrap, feature(extended_key_value_attributes))]
2018-04-02 09:26:16 +00:00
#![feature(extern_types)]
#![feature(fundamental)]
#![feature(intra_doc_pointers)]
#![feature(intrinsics)]
#![feature(lang_items)]
#![feature(link_llvm_intrinsics)]
#![feature(llvm_asm)]
2020-04-22 19:45:35 +00:00
#![feature(negative_impls)]
#![feature(never_type)]
#![feature(nll)]
#![feature(exhaustive_patterns)]
#![feature(no_core)]
2020-12-30 13:04:59 +00:00
#![feature(auto_traits)]
2020-11-21 21:18:04 +00:00
#![cfg_attr(bootstrap, feature(or_patterns))]
2017-03-17 14:05:44 +00:00
#![feature(prelude_import)]
2021-03-26 20:10:21 +00:00
#![feature(ptr_metadata)]
#![feature(repr_simd, platform_intrinsics)]
#![feature(rustc_attrs)]
#![feature(simd_ffi)]
2020-08-17 18:34:44 +00:00
#![feature(min_specialization)]
#![feature(staged_api)]
#![feature(std_internals)]
#![feature(stmt_expr_attributes)]
#![feature(str_split_as_str)]
#![feature(str_split_inclusive_as_str)]
2021-02-27 11:45:18 +00:00
#![feature(char_indices_offset)]
#![feature(trait_alias)]
2019-07-04 14:05:50 +00:00
#![feature(transparent_unions)]
#![feature(try_blocks)]
#![feature(unboxed_closures)]
2020-11-19 20:01:48 +00:00
#![feature(unsized_fn_params)]
2017-03-17 14:05:44 +00:00
#![feature(unwind_attributes)]
2020-07-16 13:12:59 +00:00
#![feature(variant_count)]
2018-05-10 18:02:19 +00:00
#![feature(tbm_target_feature)]
#![feature(sse4a_target_feature)]
#![feature(arm_target_feature)]
#![feature(powerpc_target_feature)]
#![feature(mips_target_feature)]
#![feature(aarch64_target_feature)]
#![feature(wasm_target_feature)]
#![feature(avx512_target_feature)]
2019-01-19 23:25:06 +00:00
#![feature(cmpxchg16b_target_feature)]
2019-07-15 11:57:34 +00:00
#![feature(rtm_target_feature)]
#![feature(f16c_target_feature)]
#![feature(hexagon_target_feature)]
2020-07-16 13:12:59 +00:00
#![feature(const_fn_transmute)]
2019-01-07 15:20:25 +00:00
#![feature(abi_unadjusted)]
2019-01-19 23:25:06 +00:00
#![feature(adx_target_feature)]
2019-02-11 14:29:25 +00:00
#![feature(external_doc)]
#![feature(associated_type_bounds)]
#![feature(const_caller_location)]
#![feature(slice_ptr_get)]
2020-03-15 18:43:25 +00:00
#![feature(no_niche)] // rust-lang/rust#68303
#![cfg_attr(not(bootstrap), feature(no_coverage))] // rust-lang/rust#84605
2020-10-26 18:14:12 +00:00
#![feature(int_error_matching)]
#![deny(unsafe_op_in_unsafe_fn)]
// allow using `core::` in intra-doc links
#[allow(unused_extern_crates)]
extern crate self as core;
2016-08-22 10:02:28 +00:00
#[prelude_import]
#[allow(unused)]
use prelude::v1::*;
#[cfg(not(test))] // See #65860
#[macro_use]
mod macros;
2016-10-23 13:27:49 +00:00
#[macro_use]
mod internal_macros;
#[path = "num/shells/int_macros.rs"]
#[macro_use]
mod int_macros;
#[path = "num/shells/i128.rs"]
2019-12-22 22:42:04 +00:00
pub mod i128;
#[path = "num/shells/i16.rs"]
2019-12-22 22:42:04 +00:00
pub mod i16;
#[path = "num/shells/i32.rs"]
2019-12-22 22:42:04 +00:00
pub mod i32;
#[path = "num/shells/i64.rs"]
2019-12-22 22:42:04 +00:00
pub mod i64;
#[path = "num/shells/i8.rs"]
2019-12-22 22:42:04 +00:00
pub mod i8;
#[path = "num/shells/isize.rs"]
2019-12-22 22:42:04 +00:00
pub mod isize;
#[path = "num/shells/u128.rs"]
2019-12-22 22:42:04 +00:00
pub mod u128;
#[path = "num/shells/u16.rs"]
2019-12-22 22:42:04 +00:00
pub mod u16;
#[path = "num/shells/u32.rs"]
2019-12-22 22:42:04 +00:00
pub mod u32;
#[path = "num/shells/u64.rs"]
2019-12-22 22:42:04 +00:00
pub mod u64;
#[path = "num/shells/u8.rs"]
2019-12-22 22:42:04 +00:00
pub mod u8;
#[path = "num/shells/usize.rs"]
2019-12-22 22:42:04 +00:00
pub mod usize;
2019-12-22 22:42:04 +00:00
#[path = "num/f32.rs"]
pub mod f32;
#[path = "num/f64.rs"]
pub mod f64;
#[macro_use]
pub mod num;
/* The libcore prelude, not as all-encompassing as the libstd prelude */
pub mod prelude;
2014-05-01 03:04:56 +00:00
/* Core modules for ownership management */
2019-12-22 22:42:04 +00:00
pub mod hint;
2014-05-01 03:04:56 +00:00
pub mod intrinsics;
2014-05-01 03:13:05 +00:00
pub mod mem;
2014-05-01 03:17:50 +00:00
pub mod ptr;
2014-05-01 03:22:55 +00:00
/* Core language traits */
2019-12-22 22:42:04 +00:00
pub mod borrow;
pub mod clone;
pub mod cmp;
2019-12-22 22:42:04 +00:00
pub mod convert;
2014-05-01 03:46:51 +00:00
pub mod default;
2019-12-22 22:42:04 +00:00
pub mod marker;
pub mod ops;
2014-05-01 03:33:08 +00:00
/* Core types and methods on primitives */
2014-05-01 03:36:58 +00:00
pub mod any;
pub mod array;
pub mod ascii;
2014-05-01 18:19:56 +00:00
pub mod cell;
pub mod char;
2019-12-22 22:42:04 +00:00
pub mod ffi;
pub mod iter;
2020-07-18 00:09:47 +00:00
#[unstable(feature = "once_cell", issue = "74465")]
pub mod lazy;
2019-12-22 22:42:04 +00:00
pub mod option;
pub mod panic;
pub mod panicking;
pub mod pin;
2014-05-01 03:38:31 +00:00
pub mod raw;
pub mod result;
#[unstable(feature = "async_stream", issue = "79024")]
pub mod stream;
2019-12-22 22:42:04 +00:00
pub mod sync;
2019-12-22 22:42:04 +00:00
pub mod fmt;
pub mod hash;
2019-12-22 22:42:04 +00:00
pub mod slice;
pub mod str;
2017-12-11 18:42:01 +00:00
pub mod time;
pub mod unicode;
/* Async */
pub mod future;
pub mod task;
/* Heap memory allocator trait */
#[allow(missing_docs)]
pub mod alloc;
// note: does not need to be public
mod bool;
mod tuple;
mod unit;
2020-02-24 07:59:39 +00:00
#[stable(feature = "core_primitive", since = "1.43.0")]
pub mod primitive;
2019-01-21 17:42:04 +00:00
// Pull in the `core_arch` crate directly into libcore. The contents of
2019-07-15 11:53:44 +00:00
// `core_arch` are in a different repository: rust-lang/stdarch.
2019-01-21 17:42:04 +00:00
//
// `core_arch` depends on libcore, but the contents of this module are
// set up in such a way that directly pulling it here works such that the
// crate uses the this crate as its libcore.
2020-06-12 02:31:49 +00:00
#[path = "../../stdarch/crates/core_arch/src/mod.rs"]
#[allow(
missing_docs,
missing_debug_implementations,
dead_code,
unused_imports,
unsafe_op_in_unsafe_fn
)]
2021-03-28 03:35:02 +00:00
#[cfg_attr(bootstrap, allow(rustdoc::non_autolinks))]
#[cfg_attr(not(bootstrap), allow(rustdoc::bare_urls))]
// FIXME: This annotation should be moved into rust-lang/stdarch after clashing_extern_declarations is
// merged. It currently cannot because bootstrap fails as the lint hasn't been defined yet.
2020-07-16 13:12:59 +00:00
#[allow(clashing_extern_declarations)]
#[unstable(feature = "stdsimd", issue = "48556")]
2019-01-21 17:42:04 +00:00
mod core_arch;
#[stable(feature = "simd_arch", since = "1.27.0")]
2019-01-21 17:42:04 +00:00
pub use core_arch::arch;