Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make RawVec private to alloc #90542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ extern crate test;
#[macro_use]
mod macros;

mod raw_vec;

// Heaps provided for low-level allocation strategies

pub mod alloc;
Expand All @@ -191,7 +193,6 @@ mod boxed {
pub mod borrow;
pub mod collections;
pub mod fmt;
pub mod raw_vec;
pub mod rc;
pub mod slice;
pub mod str;
Expand Down
61 changes: 4 additions & 57 deletions library/alloc/src/raw_vec.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#![unstable(feature = "raw_vec_internals", reason = "implementation detail", issue = "none")]
#![doc(hidden)]
#![unstable(feature = "raw_vec_internals", reason = "unstable const warnings", issue = "none")]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this be removed since it is no longer exported?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but got

error: const-stable function cannot use `#[feature(const_fn_trait_bound)]`
   --> library/alloc/src/raw_vec.rs:104:9
    |
104 | impl<T, A: Allocator> RawVec<T, A> {
    |         ^
    |
help: if it is not part of the public API, make this function unstably const
    |
121 |     #[rustc_const_unstable(feature = "...", issue = "...")]
    |
help: otherwise `#[rustc_allow_const_fn_unstable]` can be used to bypass stability checks
    |
121 |     #[rustc_allow_const_fn_unstable(const_fn_trait_bound)]
    |

error: `Unique::<T>::dangling` is not yet stable as a const fn
   --> library/alloc/src/raw_vec.rs:123:21
    |
123 |         Self { ptr: Unique::dangling(), cap: 0, alloc }
    |                     ^^^^^^^^^^^^^^^^^^
    |
    = help: const-stable functions can only call other const-stable functions

Adding rustc_allow_const_fn_unstable solves the former but not the latter.


use core::alloc::LayoutError;
use core::cmp;
Expand Down Expand Up @@ -50,7 +49,7 @@ enum AllocInit {
/// `usize::MAX`. This means that you need to be careful when round-tripping this type with a
/// `Box<[T]>`, since `capacity()` won't yield the length.
#[allow(missing_debug_implementations)]
pub struct RawVec<T, A: Allocator = Global> {
pub(crate) struct RawVec<T, A: Allocator = Global> {
ptr: Unique<T>,
cap: usize,
alloc: A,
Expand Down Expand Up @@ -87,33 +86,20 @@ impl<T> RawVec<T, Global> {
/// # Aborts
///
/// Aborts on OOM.
#[cfg(not(no_global_oom_handling))]
#[cfg(not(any(no_global_oom_handling, test)))]
#[must_use]
#[inline]
pub fn with_capacity(capacity: usize) -> Self {
Self::with_capacity_in(capacity, Global)
}

/// Like `with_capacity`, but guarantees the buffer is zeroed.
#[cfg(not(no_global_oom_handling))]
#[cfg(not(any(no_global_oom_handling, test)))]
#[must_use]
#[inline]
pub fn with_capacity_zeroed(capacity: usize) -> Self {
Self::with_capacity_zeroed_in(capacity, Global)
}

/// Reconstitutes a `RawVec` from a pointer and capacity.
///
/// # Safety
///
/// The `ptr` must be allocated (on the system heap), and with the given `capacity`.
/// The `capacity` cannot exceed `isize::MAX` for sized types. (only a concern on 32-bit
/// systems). ZST vectors may have a capacity up to `usize::MAX`.
/// If the `ptr` and `capacity` come from a `RawVec`, then this is guaranteed.
#[inline]
pub unsafe fn from_raw_parts(ptr: *mut T, capacity: usize) -> Self {
unsafe { Self::from_raw_parts_in(ptr, capacity, Global) }
}
}

impl<T, A: Allocator> RawVec<T, A> {
Expand Down Expand Up @@ -154,14 +140,6 @@ impl<T, A: Allocator> RawVec<T, A> {
Self::allocate_in(capacity, AllocInit::Zeroed, alloc)
}

/// Converts a `Box<[T]>` into a `RawVec<T>`.
pub fn from_box(slice: Box<[T], A>) -> Self {
unsafe {
let (slice, alloc) = Box::into_raw_with_allocator(slice);
RawVec::from_raw_parts_in(slice.as_mut_ptr(), slice.len(), alloc)
}
}

/// Converts the entire buffer into `Box<[MaybeUninit<T>]>` with the specified `len`.
///
/// Note that this will correctly reconstitute any `cap` changes
Expand Down Expand Up @@ -290,37 +268,6 @@ impl<T, A: Allocator> RawVec<T, A> {
/// # Aborts
///
/// Aborts on OOM.
///
/// # Examples
///
/// ```
/// # #![feature(raw_vec_internals)]
/// # extern crate alloc;
/// # use std::ptr;
/// # use alloc::raw_vec::RawVec;
/// struct MyVec<T> {
/// buf: RawVec<T>,
/// len: usize,
/// }
///
/// impl<T: Clone> MyVec<T> {
/// pub fn push_all(&mut self, elems: &[T]) {
/// self.buf.reserve(self.len, elems.len());
/// // reserve would have aborted or panicked if the len exceeded
/// // `isize::MAX` so this is safe to do unchecked now.
/// for x in elems {
/// unsafe {
/// ptr::write(self.buf.ptr().add(self.len), x.clone());
/// }
/// self.len += 1;
/// }
/// }
/// }
/// # fn main() {
/// # let mut vector = MyVec { buf: RawVec::new(), len: 0 };
/// # vector.push_all(&[1, 3, 5, 7, 9]);
/// # }
/// ```
#[cfg(not(no_global_oom_handling))]
#[inline]
pub fn reserve(&mut self, len: usize, additional: usize) {
Expand Down