lib.rs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. //! # The Rust core allocation and collections library
  3. //!
  4. //! This library provides smart pointers and collections for managing
  5. //! heap-allocated values.
  6. //!
  7. //! This library, like libcore, normally doesn’t need to be used directly
  8. //! since its contents are re-exported in the [`std` crate](../std/index.html).
  9. //! Crates that use the `#![no_std]` attribute however will typically
  10. //! not depend on `std`, so they’d use this crate instead.
  11. //!
  12. //! ## Boxed values
  13. //!
  14. //! The [`Box`] type is a smart pointer type. There can only be one owner of a
  15. //! [`Box`], and the owner can decide to mutate the contents, which live on the
  16. //! heap.
  17. //!
  18. //! This type can be sent among threads efficiently as the size of a `Box` value
  19. //! is the same as that of a pointer. Tree-like data structures are often built
  20. //! with boxes because each node often has only one owner, the parent.
  21. //!
  22. //! ## Reference counted pointers
  23. //!
  24. //! The [`Rc`] type is a non-threadsafe reference-counted pointer type intended
  25. //! for sharing memory within a thread. An [`Rc`] pointer wraps a type, `T`, and
  26. //! only allows access to `&T`, a shared reference.
  27. //!
  28. //! This type is useful when inherited mutability (such as using [`Box`]) is too
  29. //! constraining for an application, and is often paired with the [`Cell`] or
  30. //! [`RefCell`] types in order to allow mutation.
  31. //!
  32. //! ## Atomically reference counted pointers
  33. //!
  34. //! The [`Arc`] type is the threadsafe equivalent of the [`Rc`] type. It
  35. //! provides all the same functionality of [`Rc`], except it requires that the
  36. //! contained type `T` is shareable. Additionally, [`Arc<T>`][`Arc`] is itself
  37. //! sendable while [`Rc<T>`][`Rc`] is not.
  38. //!
  39. //! This type allows for shared access to the contained data, and is often
  40. //! paired with synchronization primitives such as mutexes to allow mutation of
  41. //! shared resources.
  42. //!
  43. //! ## Collections
  44. //!
  45. //! Implementations of the most common general purpose data structures are
  46. //! defined in this library. They are re-exported through the
  47. //! [standard collections library](../std/collections/index.html).
  48. //!
  49. //! ## Heap interfaces
  50. //!
  51. //! The [`alloc`](alloc/index.html) module defines the low-level interface to the
  52. //! default global allocator. It is not compatible with the libc allocator API.
  53. //!
  54. //! [`Arc`]: sync
  55. //! [`Box`]: boxed
  56. //! [`Cell`]: core::cell
  57. //! [`Rc`]: rc
  58. //! [`RefCell`]: core::cell
  59. // To run liballoc tests without x.py without ending up with two copies of liballoc, Miri needs to be
  60. // able to "empty" this crate. See <https://github.com/rust-lang/miri-test-libstd/issues/4>.
  61. // rustc itself never sets the feature, so this line has no affect there.
  62. #![cfg(any(not(feature = "miri-test-libstd"), test, doctest))]
  63. #![allow(unused_attributes)]
  64. #![stable(feature = "alloc", since = "1.36.0")]
  65. #![doc(
  66. html_playground_url = "https://play.rust-lang.org/",
  67. issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
  68. test(no_crate_inject, attr(allow(unused_variables), deny(warnings)))
  69. )]
  70. #![doc(cfg_hide(
  71. not(test),
  72. not(any(test, bootstrap)),
  73. any(not(feature = "miri-test-libstd"), test, doctest),
  74. no_global_oom_handling,
  75. not(no_global_oom_handling),
  76. target_has_atomic = "ptr"
  77. ))]
  78. #![no_std]
  79. #![needs_allocator]
  80. //
  81. // Lints:
  82. #![deny(unsafe_op_in_unsafe_fn)]
  83. #![warn(deprecated_in_future)]
  84. #![warn(missing_debug_implementations)]
  85. #![warn(missing_docs)]
  86. #![allow(explicit_outlives_requirements)]
  87. //
  88. // Library features:
  89. #![cfg_attr(not(no_global_oom_handling), feature(alloc_c_string))]
  90. #![feature(alloc_layout_extra)]
  91. #![feature(allocator_api)]
  92. #![feature(array_chunks)]
  93. #![feature(array_methods)]
  94. #![feature(array_windows)]
  95. #![feature(assert_matches)]
  96. #![feature(async_iterator)]
  97. #![feature(coerce_unsized)]
  98. #![cfg_attr(not(no_global_oom_handling), feature(const_alloc_error))]
  99. #![feature(const_box)]
  100. #![cfg_attr(not(no_global_oom_handling), feature(const_btree_new))]
  101. #![feature(const_cow_is_borrowed)]
  102. #![feature(const_convert)]
  103. #![feature(const_size_of_val)]
  104. #![feature(const_align_of_val)]
  105. #![feature(const_ptr_read)]
  106. #![feature(const_maybe_uninit_write)]
  107. #![feature(const_maybe_uninit_as_mut_ptr)]
  108. #![feature(const_refs_to_cell)]
  109. #![feature(core_c_str)]
  110. #![feature(core_intrinsics)]
  111. #![feature(core_ffi_c)]
  112. #![feature(const_eval_select)]
  113. #![feature(const_pin)]
  114. #![feature(cstr_from_bytes_until_nul)]
  115. #![feature(dispatch_from_dyn)]
  116. #![feature(exact_size_is_empty)]
  117. #![feature(extend_one)]
  118. #![feature(fmt_internals)]
  119. #![feature(fn_traits)]
  120. #![feature(hasher_prefixfree_extras)]
  121. #![feature(inplace_iteration)]
  122. #![feature(iter_advance_by)]
  123. #![feature(layout_for_ptr)]
  124. #![feature(maybe_uninit_slice)]
  125. #![cfg_attr(test, feature(new_uninit))]
  126. #![feature(nonnull_slice_from_raw_parts)]
  127. #![feature(pattern)]
  128. #![feature(ptr_internals)]
  129. #![feature(ptr_metadata)]
  130. #![feature(ptr_sub_ptr)]
  131. #![feature(receiver_trait)]
  132. #![feature(set_ptr_value)]
  133. #![feature(slice_group_by)]
  134. #![feature(slice_ptr_get)]
  135. #![feature(slice_ptr_len)]
  136. #![feature(slice_range)]
  137. #![feature(str_internals)]
  138. #![feature(strict_provenance)]
  139. #![feature(trusted_len)]
  140. #![feature(trusted_random_access)]
  141. #![feature(try_trait_v2)]
  142. #![feature(unchecked_math)]
  143. #![feature(unicode_internals)]
  144. #![feature(unsize)]
  145. //
  146. // Language features:
  147. #![feature(allocator_internals)]
  148. #![feature(allow_internal_unstable)]
  149. #![feature(associated_type_bounds)]
  150. #![feature(box_syntax)]
  151. #![feature(cfg_sanitize)]
  152. #![feature(const_deref)]
  153. #![feature(const_mut_refs)]
  154. #![feature(const_ptr_write)]
  155. #![feature(const_precise_live_drops)]
  156. #![feature(const_trait_impl)]
  157. #![feature(const_try)]
  158. #![feature(dropck_eyepatch)]
  159. #![feature(exclusive_range_pattern)]
  160. #![feature(fundamental)]
  161. #![cfg_attr(not(test), feature(generator_trait))]
  162. #![feature(hashmap_internals)]
  163. #![feature(lang_items)]
  164. #![feature(let_else)]
  165. #![feature(min_specialization)]
  166. #![feature(negative_impls)]
  167. #![feature(never_type)]
  168. #![feature(nll)] // Not necessary, but here to test the `nll` feature.
  169. #![feature(rustc_allow_const_fn_unstable)]
  170. #![feature(rustc_attrs)]
  171. #![feature(slice_internals)]
  172. #![feature(staged_api)]
  173. #![cfg_attr(test, feature(test))]
  174. #![feature(unboxed_closures)]
  175. #![feature(unsized_fn_params)]
  176. #![feature(c_unwind)]
  177. //
  178. // Rustdoc features:
  179. #![feature(doc_cfg)]
  180. #![feature(doc_cfg_hide)]
  181. // Technically, this is a bug in rustdoc: rustdoc sees the documentation on `#[lang = slice_alloc]`
  182. // blocks is for `&[T]`, which also has documentation using this feature in `core`, and gets mad
  183. // that the feature-gate isn't enabled. Ideally, it wouldn't check for the feature gate for docs
  184. // from other crates, but since this can only appear for lang items, it doesn't seem worth fixing.
  185. #![feature(intra_doc_pointers)]
  186. // Allow testing this library
  187. #[cfg(test)]
  188. #[macro_use]
  189. extern crate std;
  190. #[cfg(test)]
  191. extern crate test;
  192. // Module with internal macros used by other modules (needs to be included before other modules).
  193. #[cfg(not(no_macros))]
  194. #[macro_use]
  195. mod macros;
  196. mod raw_vec;
  197. // Heaps provided for low-level allocation strategies
  198. pub mod alloc;
  199. // Primitive types using the heaps above
  200. // Need to conditionally define the mod from `boxed.rs` to avoid
  201. // duplicating the lang-items when building in test cfg; but also need
  202. // to allow code to have `use boxed::Box;` declarations.
  203. #[cfg(not(test))]
  204. pub mod boxed;
  205. #[cfg(test)]
  206. mod boxed {
  207. pub use std::boxed::Box;
  208. }
  209. pub mod borrow;
  210. pub mod collections;
  211. #[cfg(not(no_global_oom_handling))]
  212. pub mod ffi;
  213. #[cfg(not(no_fmt))]
  214. pub mod fmt;
  215. #[cfg(not(no_rc))]
  216. pub mod rc;
  217. pub mod slice;
  218. #[cfg(not(no_str))]
  219. pub mod str;
  220. #[cfg(not(no_string))]
  221. pub mod string;
  222. #[cfg(not(no_sync))]
  223. #[cfg(target_has_atomic = "ptr")]
  224. pub mod sync;
  225. #[cfg(all(not(no_global_oom_handling), target_has_atomic = "ptr"))]
  226. pub mod task;
  227. #[cfg(test)]
  228. mod tests;
  229. pub mod vec;
  230. #[doc(hidden)]
  231. #[unstable(feature = "liballoc_internals", issue = "none", reason = "implementation detail")]
  232. pub mod __export {
  233. pub use core::format_args;
  234. }