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

Rollup of 7 pull requests #102632

Merged
merged 23 commits into from
Oct 3, 2022
Merged
Changes from 6 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
5dcc418
Document the conditional existence of `alloc::sync` and `alloc::task`.
kpreid Jun 18, 2022
9cd66be
docs: be less harsh in wording for Vec::from_raw_parts
duarten Jul 13, 2022
c9ec7aa
changes to wording
duarten Jul 13, 2022
050115c
typo
duarten Jul 14, 2022
8d35ab3
rustdoc
duarten Jul 14, 2022
a85ee3e
add code examples
duarten Jul 14, 2022
551d921
docs: Improve AsRef / AsMut docs on blanket impls
JanBeh Jul 19, 2022
9f68e3e
fixup! docs: Improve AsRef / AsMut docs on blanket impls
JanBeh Jul 19, 2022
e4a259b
fixup! docs: Improve AsRef / AsMut docs on blanket impls
JanBeh Jul 19, 2022
e6b761b
fixup! docs: Improve AsRef / AsMut docs on blanket impls
JanBeh Jul 21, 2022
698a3c6
Tweak `FpCategory` example order.
reitermarkus Aug 13, 2022
cb86c38
Fix `#[derive(Default)]` on a generic `#[default]` enum adding unnece…
danielhenrymantilla Aug 26, 2022
975e72f
Add corresponding regression test
danielhenrymantilla Aug 26, 2022
3d4980b
Future-proof against loose bounds if default variant is non-exhaustive.
danielhenrymantilla Sep 15, 2022
591c1f2
introduce `{char, u8}::is_ascii_octdigit`
oppiliappan Sep 27, 2022
b9c0467
Add diagnostic struct for const eval error in `rustc_middle`
pierwill Oct 2, 2022
098e8b7
Rollup merge of #98218 - kpreid:nostdarc, r=joshtriplett
matthiaskrgr Oct 3, 2022
2110d2d
Rollup merge of #99216 - duarten:master, r=joshtriplett
matthiaskrgr Oct 3, 2022
eedb512
Rollup merge of #99460 - JanBeh:PR_asref_asmut_docs, r=joshtriplett
matthiaskrgr Oct 3, 2022
c7f1b8e
Rollup merge of #100470 - reitermarkus:patch-1, r=joshtriplett
matthiaskrgr Oct 3, 2022
df11395
Rollup merge of #101040 - danielhenrymantilla:no-bounds-for-default-a…
matthiaskrgr Oct 3, 2022
a548882
Rollup merge of #101308 - nerdypepper:feature/is-ascii-octdigit, r=jo…
matthiaskrgr Oct 3, 2022
1b9014f
Rollup merge of #102486 - pierwill:middle-const-eval-err, r=compiler-…
matthiaskrgr Oct 3, 2022
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
81 changes: 73 additions & 8 deletions library/alloc/src/vec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,15 +483,13 @@ impl<T> Vec<T> {
Self::with_capacity_in(capacity, Global)
}

/// Creates a `Vec<T>` directly from the raw components of another vector.
/// Creates a `Vec<T>` directly from a pointer, a capacity, and a length.
///
/// # Safety
///
/// This is highly unsafe, due to the number of invariants that aren't
/// checked:
///
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
/// (at least, it's highly likely to be incorrect if it wasn't).
/// * `T` needs to have the same alignment as what `ptr` was allocated with.
/// (`T` having a less strict alignment is not sufficient, the alignment really
/// needs to be equal to satisfy the [`dealloc`] requirement that memory must be
Expand All @@ -500,6 +498,14 @@ impl<T> Vec<T> {
/// to be the same size as the pointer was allocated with. (Because similar to
/// alignment, [`dealloc`] must be called with the same layout `size`.)
/// * `length` needs to be less than or equal to `capacity`.
/// * The first `length` values must be properly initialized values of type `T`.
/// * `capacity` needs to be the capacity that the pointer was allocated with.
/// * The allocated size in bytes must be no larger than `isize::MAX`.
/// See the safety documentation of [`pointer::offset`].
///
/// These requirements are always upheld by any `ptr` that has been allocated
/// via `Vec<T>`. Other allocation sources are allowed if the invariants are
/// upheld.
///
/// Violating these may cause problems like corrupting the allocator's
/// internal data structures. For example it is normally **not** safe
Expand Down Expand Up @@ -551,6 +557,32 @@ impl<T> Vec<T> {
/// assert_eq!(rebuilt, [4, 5, 6]);
/// }
/// ```
///
/// Using memory that was allocated elsewhere:
///
/// ```rust
/// #![feature(allocator_api)]
///
/// use std::alloc::{AllocError, Allocator, Global, Layout};
///
/// fn main() {
/// let layout = Layout::array::<u32>(16).expect("overflow cannot happen");
///
/// let vec = unsafe {
/// let mem = match Global.allocate(layout) {
/// Ok(mem) => mem.cast::<u32>().as_ptr(),
/// Err(AllocError) => return,
/// };
///
/// mem.write(1_000_000);
///
/// Vec::from_raw_parts_in(mem, 1, 16, Global)
/// };
///
/// assert_eq!(vec, &[1_000_000]);
/// assert_eq!(vec.capacity(), 16);
/// }
/// ```
#[inline]
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_raw_parts(ptr: *mut T, length: usize, capacity: usize) -> Self {
Expand Down Expand Up @@ -641,21 +673,30 @@ impl<T, A: Allocator> Vec<T, A> {
Vec { buf: RawVec::with_capacity_in(capacity, alloc), len: 0 }
}

/// Creates a `Vec<T, A>` directly from the raw components of another vector.
/// Creates a `Vec<T, A>` directly from a pointer, a capacity, a length,
/// and an allocator.
///
/// # Safety
///
/// This is highly unsafe, due to the number of invariants that aren't
/// checked:
///
/// * `ptr` needs to have been previously allocated via [`String`]/`Vec<T>`
/// (at least, it's highly likely to be incorrect if it wasn't).
/// * `T` needs to have the same size and alignment as what `ptr` was allocated with.
/// * `T` needs to have the same alignment as what `ptr` was allocated with.
/// (`T` having a less strict alignment is not sufficient, the alignment really
/// needs to be equal to satisfy the [`dealloc`] requirement that memory must be
/// allocated and deallocated with the same layout.)
/// * The size of `T` times the `capacity` (ie. the allocated size in bytes) needs
/// to be the same size as the pointer was allocated with. (Because similar to
/// alignment, [`dealloc`] must be called with the same layout `size`.)
/// * `length` needs to be less than or equal to `capacity`.
/// * `capacity` needs to be the capacity that the pointer was allocated with.
/// * The first `length` values must be properly initialized values of type `T`.
/// * `capacity` needs to [*fit*] the layout size that the pointer was allocated with.
/// * The allocated size in bytes must be no larger than `isize::MAX`.
/// See the safety documentation of [`pointer::offset`].
///
/// These requirements are always upheld by any `ptr` that has been allocated
/// via `Vec<T, A>`. Other allocation sources are allowed if the invariants are
/// upheld.
///
/// Violating these may cause problems like corrupting the allocator's
/// internal data structures. For example it is **not** safe
Expand All @@ -673,6 +714,7 @@ impl<T, A: Allocator> Vec<T, A> {
///
/// [`String`]: crate::string::String
/// [`dealloc`]: crate::alloc::GlobalAlloc::dealloc
/// [*fit*]: crate::alloc::Allocator#memory-fitting
///
/// # Examples
///
Expand Down Expand Up @@ -711,6 +753,29 @@ impl<T, A: Allocator> Vec<T, A> {
/// assert_eq!(rebuilt, [4, 5, 6]);
/// }
/// ```
///
/// Using memory that was allocated elsewhere:
///
/// ```rust
/// use std::alloc::{alloc, Layout};
///
/// fn main() {
/// let layout = Layout::array::<u32>(16).expect("overflow cannot happen");
/// let vec = unsafe {
/// let mem = alloc(layout).cast::<u32>();
/// if mem.is_null() {
/// return;
/// }
///
/// mem.write(1_000_000);
///
/// Vec::from_raw_parts(mem, 1, 16)
/// };
///
/// assert_eq!(vec, &[1_000_000]);
/// assert_eq!(vec.capacity(), 16);
/// }
/// ```
#[inline]
#[unstable(feature = "allocator_api", issue = "32838")]
pub unsafe fn from_raw_parts_in(ptr: *mut T, length: usize, capacity: usize, alloc: A) -> Self {
Expand Down