mod.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. //! Collection types.
  3. #![stable(feature = "rust1", since = "1.0.0")]
  4. #[cfg(not(no_global_oom_handling))]
  5. pub mod binary_heap;
  6. #[cfg(not(no_global_oom_handling))]
  7. mod btree;
  8. #[cfg(not(no_global_oom_handling))]
  9. pub mod linked_list;
  10. #[cfg(not(no_global_oom_handling))]
  11. pub mod vec_deque;
  12. #[cfg(not(no_global_oom_handling))]
  13. #[stable(feature = "rust1", since = "1.0.0")]
  14. pub mod btree_map {
  15. //! An ordered map based on a B-Tree.
  16. #[stable(feature = "rust1", since = "1.0.0")]
  17. pub use super::btree::map::*;
  18. }
  19. #[cfg(not(no_global_oom_handling))]
  20. #[stable(feature = "rust1", since = "1.0.0")]
  21. pub mod btree_set {
  22. //! An ordered set based on a B-Tree.
  23. #[stable(feature = "rust1", since = "1.0.0")]
  24. pub use super::btree::set::*;
  25. }
  26. #[cfg(not(no_global_oom_handling))]
  27. #[stable(feature = "rust1", since = "1.0.0")]
  28. #[doc(no_inline)]
  29. pub use binary_heap::BinaryHeap;
  30. #[cfg(not(no_global_oom_handling))]
  31. #[stable(feature = "rust1", since = "1.0.0")]
  32. #[doc(no_inline)]
  33. pub use btree_map::BTreeMap;
  34. #[cfg(not(no_global_oom_handling))]
  35. #[stable(feature = "rust1", since = "1.0.0")]
  36. #[doc(no_inline)]
  37. pub use btree_set::BTreeSet;
  38. #[cfg(not(no_global_oom_handling))]
  39. #[stable(feature = "rust1", since = "1.0.0")]
  40. #[doc(no_inline)]
  41. pub use linked_list::LinkedList;
  42. #[cfg(not(no_global_oom_handling))]
  43. #[stable(feature = "rust1", since = "1.0.0")]
  44. #[doc(no_inline)]
  45. pub use vec_deque::VecDeque;
  46. use crate::alloc::{Layout, LayoutError};
  47. use core::fmt::Display;
  48. /// The error type for `try_reserve` methods.
  49. #[derive(Clone, PartialEq, Eq, Debug)]
  50. #[stable(feature = "try_reserve", since = "1.57.0")]
  51. pub struct TryReserveError {
  52. kind: TryReserveErrorKind,
  53. }
  54. impl TryReserveError {
  55. /// Details about the allocation that caused the error
  56. #[inline]
  57. #[must_use]
  58. #[unstable(
  59. feature = "try_reserve_kind",
  60. reason = "Uncertain how much info should be exposed",
  61. issue = "48043"
  62. )]
  63. pub fn kind(&self) -> TryReserveErrorKind {
  64. self.kind.clone()
  65. }
  66. }
  67. /// Details of the allocation that caused a `TryReserveError`
  68. #[derive(Clone, PartialEq, Eq, Debug)]
  69. #[unstable(
  70. feature = "try_reserve_kind",
  71. reason = "Uncertain how much info should be exposed",
  72. issue = "48043"
  73. )]
  74. pub enum TryReserveErrorKind {
  75. /// Error due to the computed capacity exceeding the collection's maximum
  76. /// (usually `isize::MAX` bytes).
  77. CapacityOverflow,
  78. /// The memory allocator returned an error
  79. AllocError {
  80. /// The layout of allocation request that failed
  81. layout: Layout,
  82. #[doc(hidden)]
  83. #[unstable(
  84. feature = "container_error_extra",
  85. issue = "none",
  86. reason = "\
  87. Enable exposing the allocator’s custom error value \
  88. if an associated type is added in the future: \
  89. https://github.com/rust-lang/wg-allocators/issues/23"
  90. )]
  91. non_exhaustive: (),
  92. },
  93. }
  94. #[unstable(
  95. feature = "try_reserve_kind",
  96. reason = "Uncertain how much info should be exposed",
  97. issue = "48043"
  98. )]
  99. impl From<TryReserveErrorKind> for TryReserveError {
  100. #[inline]
  101. fn from(kind: TryReserveErrorKind) -> Self {
  102. Self { kind }
  103. }
  104. }
  105. #[unstable(feature = "try_reserve_kind", reason = "new API", issue = "48043")]
  106. impl From<LayoutError> for TryReserveErrorKind {
  107. /// Always evaluates to [`TryReserveErrorKind::CapacityOverflow`].
  108. #[inline]
  109. fn from(_: LayoutError) -> Self {
  110. TryReserveErrorKind::CapacityOverflow
  111. }
  112. }
  113. #[stable(feature = "try_reserve", since = "1.57.0")]
  114. impl Display for TryReserveError {
  115. fn fmt(
  116. &self,
  117. fmt: &mut core::fmt::Formatter<'_>,
  118. ) -> core::result::Result<(), core::fmt::Error> {
  119. fmt.write_str("memory allocation failed")?;
  120. let reason = match self.kind {
  121. TryReserveErrorKind::CapacityOverflow => {
  122. " because the computed capacity exceeded the collection's maximum"
  123. }
  124. TryReserveErrorKind::AllocError { .. } => {
  125. " because the memory allocator returned a error"
  126. }
  127. };
  128. fmt.write_str(reason)
  129. }
  130. }
  131. /// An intermediate trait for specialization of `Extend`.
  132. #[doc(hidden)]
  133. trait SpecExtend<I: IntoIterator> {
  134. /// Extends `self` with the contents of the given iterator.
  135. fn spec_extend(&mut self, iter: I);
  136. }