Commit Graph

558 Commits

Author SHA1 Message Date
ltdk
d6945f6d8c Add cast_init and cast_uninit methods for pointers 2025-08-12 16:57:56 -04:00
SabrinaJewson
1ce4b370e4 Handle the capacity == 0 case 2025-08-12 05:12:34 +01:00
SabrinaJewson
45e2449b2a Respond to review comments 2025-08-11 19:02:00 +01:00
SabrinaJewson
19df24b3b8 Make explicit guarantees about Vec’s allocator
This commit amends the documentation of `Vec::as_mut_ptr` and
`Vec::into_raw_parts` to make it explicit that such calls may be paired
with calls to `dealloc` with a suitable layout. This guarantee was
effectively already provided by the docs of `Vec::from_raw_parts`
mentioning `alloc`.

Additionally, we copy-paste and adjust the “Memory layout” section from
the documentation of `std::boxed` to `std::vec`. This explains the allocator
guarantees in more detail.
2025-08-11 17:15:53 +01:00
Samuel Tardieu
d082ff4c04 Rollup merge of #144478 - joshtriplett:doc-code-formatting-prep, r=Amanieu
Improve formatting of doc code blocks

We don't currently apply automatic formatting to doc comment code blocks. As a
result, it has built up various idiosyncracies, which make such automatic
formatting difficult. Some of those idiosyncracies also make things harder for
human readers or other tools.

This PR makes a few improvements to doc code formatting, in the hopes of making
future automatic formatting easier, as well as in many cases providing net
readability improvements.

I would suggest reading each commit separately, as each commit contains one
class of changes.
2025-08-02 11:24:24 +02:00
Balt
3ccfa14e48 Implement push_mut 2025-07-30 12:26:16 -05:00
Josh Triplett
b6c54a97b3 Avoid placing // FIXME comments inside doc code blocks
This leads tools like rustfmt to get confused, because the doc code
block effectively spans two doc comments. As a result, the tools think
the first code block is unclosed, and the subsequent terminator opens a
new block.

Move the FIXME comments outside the doc code blocks, instead.
2025-07-25 22:02:09 -07:00
Josh Triplett
715088094c Improve and regularize comment placement in doc code
Because doc code does not get automatically formatted, some doc code has
creative placements of comments that automatic formatting can't handle.
Reformat those comments to make the resulting code support standard Rust
formatting without breaking; this is generally an improvement to
readability as well.

Some comments are not indented to the prevailing indent, and are instead
aligned under some bit of code. Indent them to the prevailing indent,
and put spaces *inside* the comments to align them with code.

Some comments span several lines of code (which aren't the line the
comment is about) and expect alignment. Reformat them into one comment
not broken up by unrelated intervening code.

Some comments are placed on the same line as an opening brace, placing
them effectively inside the subsequent block, such that formatting would
typically format them like a line of that block. Move those comments to
attach them to what they apply to.

Some comments are placed on the same line as a one-line braced block,
effectively attaching them to the closing brace, even though they're
about the code inside the block. Reformat to make sure the comment will
stay on the same line as the code it's commenting.
2025-07-25 22:02:09 -07:00
bors
3014e79f9c Auto merge of #143877 - xizheyin:143813, r=scottmcm,saethlin
`std::vec`: Add UB check for `set_len`, `from_raw_parts_in`, and etc.

Closes rust-lang/rust#143813

I noticed that `from_parts_in` do the similar things like `from_raw_parts_in`, so I add the UB check in the last commit. If it is not appropriate, I will remove it.

And I fix a typo in the first commit.

r? `@scottmcm`
2025-07-15 14:47:10 +00:00
Deadbeef
6b02597ed3 update issue number for const_trait_impl 2025-07-13 23:55:06 +08:00
xizheyin
48caa5f889 std::vec: Add UB check in from_parts_in
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-13 16:18:56 +08:00
xizheyin
090c177003 std::vec: Add UB check in from_raw_parts_in
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-13 16:17:43 +08:00
xizheyin
1cab09ed9c std::vec: Upgrade debug_assert to UB check in set_len
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-13 16:06:53 +08:00
xizheyin
10866f4617 Fix typo in std::vec
Signed-off-by: xizheyin <xizheyin@smail.nju.edu.cn>
2025-07-13 15:51:32 +08:00
Esteban Küber
c3301503b9 Make Default const and add some const Default impls
Full list of `impl const Default` types:

- ()
- bool
- char
- Cell
- std::ascii::Char
- usize
- u8
- u16
- u32
- u64
- u128
- i8
- i16
- i32
- i64
- i128
- f16
- f32
- f64
- f128
- std::marker::PhantomData<T>
- Option<T>
- std::iter::Empty<T>
- std::ptr::Alignment
- &[T]
- &mut [T]
- &str
- &mut str
- String
- Vec<T>
2025-07-07 22:09:37 +00:00
bors
25face9808 Auto merge of #143338 - matthiaskrgr:rollup-ykaxh04, r=matthiaskrgr
Rollup of 11 pull requests

Successful merges:

 - rust-lang/rust#131923 (Derive `Copy` and `Hash` for `IntErrorKind`)
 - rust-lang/rust#138340 (Remove some unsized tuple impls now that we don't support unsizing tuples anymore)
 - rust-lang/rust#141219 (Change `{Box,Arc,Rc,Weak}::into_raw` to only work with `A = Global`)
 - rust-lang/rust#142212 (bootstrap: validate `rust.codegen-backends` & `target.<triple>.codegen-backends`)
 - rust-lang/rust#142237 (Detect more cases of unused_parens around types)
 - rust-lang/rust#142964 (Attribute rework: a parser for single attributes without arguments)
 - rust-lang/rust#143070 (Rewrite `macro_rules!` parser to not use the MBE engine itself)
 - rust-lang/rust#143235 (Assemble const bounds via normal item bounds in old solver too)
 - rust-lang/rust#143261 (Feed `explicit_predicates_of` instead of `predicates_of`)
 - rust-lang/rust#143276 (loop match: handle opaque patterns)
 - rust-lang/rust#143306 (Add `track_caller` attributes to trace origin of Clippy lints)

r? `@ghost`
`@rustbot` modify labels: rollup

try-job: aarch64-apple
try-job: x86_64-msvc-1
try-job: x86_64-gnu
try-job: dist-i586-gnu-i586-i686-musl
try-job: test-various
2025-07-02 23:29:03 +00:00
Matthias Krüger
6a31e693eb Rollup merge of #142138 - ashivaram23:vec_into_chunks, r=scottmcm
Add `Vec::into_chunks`

Tracking issue rust-lang/rust#142137
2025-07-02 19:28:04 +02:00
Amanieu d'Antras
15bd619d5f Change {Box,Arc,Rc,Weak}::into_raw to only work with A = Global
Also applies to `Vec::into_raw_parts`.

The expectation is that you can round-trip these methods with
`from_raw`, but this is only true when using the global allocator. With
custom allocators you should instead be using
`into_raw_with_allocator` and `from_raw_in`.

The implementation of `Box::leak` is changed to use
`Box::into_raw_with_allocator` and explicitly leak the allocator (which
was already the existing behavior). This is because, for `leak` to be
safe, the allocator must not free its underlying backing store. The
`Allocator` trait only guarantees that allocated memory remains valid
until the allocator is dropped.
2025-07-01 18:25:30 +02:00
Jeremy Smart
9d19cbe29b update docs, test 2025-06-11 22:57:57 -04:00
Amogh Shivaram
c1d32d8533 fix wording in assert 2025-06-07 08:18:44 -05:00
Amogh Shivaram
343db27aa6 cfg for no no_global_oom_handling 2025-06-07 01:14:24 -05:00
Amogh Shivaram
e4e77737f9 put feature attribute in example 2025-06-06 23:06:23 -05:00
Amogh Shivaram
230b55dcf8 Add into_chunks 2025-06-06 22:14:30 -05:00
Jeremy Smart
cc80429021 fix tests 2025-06-06 19:22:15 -04:00
Jeremy Smart
188c40126d add Vec::peek_mut 2025-06-04 18:15:12 -04:00
Paul Mabileau
b1d7480acb Docs(lib/extract_if): Unify example description
Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:54:21 +02:00
Paul Mabileau
014434e96f Docs(lib/extract_if): Unify paragraph about elements mutation
Take the one from `BTreeMap` that seems the best-worded and most
precise among the available variations.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:45:56 +02:00
Paul Mabileau
9205ee27a8 Docs(lib/extract_if): Unify paragraph about closure actions
Also fixes `HashSet`'s that incorrectly designated itself as a `list`.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:36:08 +02:00
Paul Mabileau
d29d1a332b Docs(lib/alloc/vec): Add the missing an to extract_if's first sentence
As inspired by the equivalent methods from other collection types.

Signed-off-by: Paul Mabileau <paul.mabileau@harfanglab.fr>
2025-05-17 02:09:06 +02:00
Matthias Krüger
7a8fd9985d Rollup merge of #140685 - viliml:patch-1, r=Mark-Simulacrum
Simplify `Vec::as_non_null` implementation and make it `const`

Tracking issue: #130364.
2025-05-15 22:28:50 +02:00
Stuart Cook
d5c09b4aa6 Rollup merge of #139773 - thaliaarchi:vec-into-iter-last, r=workingjubilee
Implement `Iterator::last` for `vec::IntoIter`

Avoid iterating everything when we have random access to the last element.
2025-05-06 16:28:39 +10:00
Stuart Cook
5ca0053c79 Rollup merge of #139764 - dtolnay:extractif, r=Amanieu
Consistent trait bounds for ExtractIf Debug impls

Closes #137654. Refer to that issue for a table of the **4** different impl signatures we previously had in the standard library for Debug impls of various ExtractIf iterator types.

The one we are standardizing on is the one so far only used by `alloc::collections::linked_list::ExtractIf`, which is _no_ `F: Debug` bound, _no_ `F: FnMut` bound, only `T: Debug` bound.

This PR applies the following signature changes:

```diff
/* alloc::collections::btree_map */

    pub struct ExtractIf<'a, K, V, F, A = Global>
    where
-       F: 'a + FnMut(&K, &mut V) -> bool,
        Allocator + Clone,

    impl Debug for ExtractIf<'a, K, V, F,
+       A,
    >
    where
        K: Debug,
        V: Debug,
-       F: FnMut(&K, &mut V) -> bool,
+       A: Allocator + Clone,
```

```diff
/* alloc::collections::btree_set */

    pub struct ExtractIf<'a, T, F, A = Global>
    where
-       T: 'a,
-       F: 'a + FnMut(&T) -> bool,
        Allocator + Clone,

    impl Debug for ExtractIf<'a, T, F, A>
    where
        T: Debug,
-       F: FnMut(&T) -> bool,
        A: Allocator + Clone,
```

```diff
/* alloc::collections::linked_list */

    impl Debug for ExtractIf<'a, T, F,
+       A,
    >
    where
        T: Debug,
+       A: Allocator,
```

```diff
/* alloc::vec */

    impl Debug for ExtractIf<'a, T, F, A>
    where
        T: Debug,
-       F: Debug,
        A: Allocator,
-       A: Debug,
```

```diff
/* std::collections::hash_map */

    pub struct ExtractIf<'a, K, V, F>
    where
-       F: FnMut(&K, &mut V) -> bool,

    impl Debug for ExtractIf<'a, K, V, F>
    where
+       K: Debug,
+       V: Debug,
-       F: FnMut(&K, &mut V) -> bool,
```

```diff
/* std::collections::hash_set */

    pub struct ExtractIf<'a, T, F>
    where
-       F: FnMut(&T) -> bool,

    impl Debug for ExtractIf<'a, T, F>
    where
+       T: Debug,
-       F: FnMut(&T) -> bool,
```

I have made the following changes to bring these types into better alignment with one another.

- Delete `F: Debug` bounds. These are especially problematic because Rust closures do not come with a Debug impl, rendering the impl useless.

- Delete `A: Debug` bounds. Allocator parameters are unstable for now, but in the future this would become an API commitment that we do not debug-print a representation of the allocator when printing an iterator.

- Delete `F: FnMut` bounds. Requires `hashbrown` PR: https://github.com/rust-lang/hashbrown/pull/616. **API commitment:** we commit to not doing RefCell voodoo inside ExtractIf to have some way for its Debug impl (which takes &amp;self) to call a FnMut closure, if this is even possible.

- Add `T: Debug` bounds (or `K`/`V`), even on Debug impls that do not currently make use of them, but might in the future. **Breaking change.** Must backport into Rust 1.87 (current beta) or do a de-stabilization PR in beta to delay those types by one release.

- Render using `debug_struct` + `finish_non_exhaustive`, instead of `debug_tuple`.

- Do not render the _entire_ underlying collection.

- Show a "peek" field indicating the current position of the iterator.
2025-05-06 16:28:38 +10:00
David Tolnay
c35914383a Consistent trait bounds for ExtractIf Debug impls 2025-05-05 19:46:46 -07:00
Vilim Lendvaj
53459ffa8c Simplify Vec::as_non_null implementation and make it const 2025-05-05 21:56:33 +02:00
Trevor Gross
c44e1d65f3 Rollup merge of #135734 - nk9:extract_if-doc-equivalent, r=tgross35
Correct `extract_if` sample equivalent.

Tracking issue: https://github.com/rust-lang/rust/issues/43244

Original PR: #133265

The sample code marked as equivalent in the doc comment isn't currently equivalent. Given the same predicate and range, if your vector were `[1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 6]`, then all of the 3s would be removed. `i` is only incremented when an element is dropped, but `range.end` is unchanged, so the items shift down. I got very confused when reading the docs and trying to square this sample code with the explanation of how the function works.

Fortunately, the real `extract_if()` does not have this problem. I've added an `end` variable to align the behavior. I've also taken the opportunity to simplify the predicate, which now just matches odd numbers, and to pad out the vec of numbers to line up the zero-indexed range with the integers in the vec.

r? the8472
2025-05-05 00:20:57 -04:00
Nick Kocharhook
52d806a7c0 extract_if's sample equivalent now really equivalent.
Simpler predicate.
Compare sample code output to that of the library function.
2025-05-04 23:38:54 +01:00
Paolo Barbolini
5cbb27ff90 Suggest retain_mut over retain as Vec::extract_if alternative 2025-05-03 16:39:47 +00:00
Thalia Archibald
cbdd7134ff Implement Iterator::last for vec::IntoIter 2025-05-02 20:08:28 -07:00
DaniPopes
f07cc409d3 Rename sub_ptr to offset_from_unsigned in docs 2025-04-28 13:58:27 +02:00
Urgau
40ba47d3b0 Implement lint against dangerous implicit autorefs 2025-04-20 11:36:28 +02:00
Boxy
a6c2ec04b4 replace version placeholder 2025-04-09 12:29:59 +01:00
Jonathan Gruner
4aab8e88e4 document panic behavior of Vec::resize and Vec::resize_with 2025-04-08 00:00:38 +02:00
mejrs
b1bc7255bb Delete unreacheable #[rustc_on_unimplemented] 2025-03-30 15:25:27 +02:00
Jacob Pratt
c7700406f4 Rollup merge of #136293 - hkBst:patch-32, r=Amanieu
document capacity for ZST as example

The main text already covers this, although it provides weaker guarantees, but I think an example in the right spot does not hurt. Fixes #80747
2025-03-16 21:47:42 -04:00
Marijn Schouten
db7e61cfa5 document capacity for ZST as example and prose 2025-03-09 13:11:51 +01:00
Martin Habovstiak
50ea503d9d Stabilize const_vec_string_slice
This feature was approved for stabilization in
https://github.com/rust-lang/rust/issues/129041#issuecomment-2508940661
so this change stabilizes it.
2025-03-08 17:03:52 +01:00
bjorn3
ae5687e4b0 Fully test the alloc crate through alloctests
For the tests that make use of internal implementation details, we
include the module to test using #[path] in alloctests now.
2025-03-07 19:11:13 +00:00
Thalia Archibald
988eb19970 library: Use size_of from the prelude instead of imported
Use `std::mem::{size_of, size_of_val, align_of, align_of_val}` from the
prelude instead of importing or qualifying them.

These functions were added to all preludes in Rust 1.80.
2025-03-06 20:20:38 -08:00
Trevor Gross
57ce16ca27 Rollup merge of #137109 - bend-n:knife, r=oli-obk
stabilize extract_if

Tracking issue: #43244
Closes: #43244
FCP completed: https://github.com/rust-lang/rust/issues/43244#issuecomment-2523595704
2025-02-24 18:46:35 -05:00
Trevor Gross
dc2b86feb8 Rollup merge of #135933 - hkBst:patch-19, r=workingjubilee
Explain how Vec::with_capacity is faithful

This is a revival of https://github.com/rust-lang/rust/pull/99790 building on the prose of `@workingjubilee` and edits of `@jmaargh.` Closes https://github.com/rust-lang/rust/issues/99385.
2025-02-24 18:46:34 -05:00