Skip to content

Implement push_mut #135975

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Implement push_mut #135975

wants to merge 3 commits into from

Conversation

balt-dev
Copy link

@balt-dev balt-dev commented Jan 24, 2025

Implementation of #135974.

@rustbot
Copy link
Collaborator

rustbot commented Jan 24, 2025

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @ibraheemdev (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jan 24, 2025
@balt-dev
Copy link
Author

Sorry if I did this wrong, it's my first time!

@rust-log-analyzer

This comment has been minimized.

@balt-dev
Copy link
Author

A single trailing whitespace. Oops.

@rust-log-analyzer

This comment has been minimized.

@ibraheemdev
Copy link
Member

I'm not sure we should be touching Vec::push here.. maybe just duplicate the implementation?

@balt-dev
Copy link
Author

Alright, I'll do that soon.

@ibraheemdev ibraheemdev added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 31, 2025
@balt-dev
Copy link
Author

I finally got around to duplicating the implementation. College has been eating my time like a child with Halloween candy, sorry.

@balt-dev
Copy link
Author

@rustbot review

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Feb 22, 2025
/// Takes *O*(1) time.
#[inline]
#[unstable(feature = "push_mut", issue = "135974")]
// #[unstable(feature = "vec_push_within_capacity", issue = "100486")]
Copy link
Member

Choose a reason for hiding this comment

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

Should this comment be removed?

Copy link
Author

Choose a reason for hiding this comment

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

Well, both features apply, so I wasn't quite sure what to do. If push_mut is stabilized before vec_push_within_capacity, this should still be unstable, and same for the reverse.

///
/// let mut vec = vec![];
/// // Due to current borrow checker limitations (see -Zpolonius), this is the only way to spell this right now.
/// let last = if let Some(v) = vec.last_mut() { v } else { vec.push_mut(0) };
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure I understand the point of this.

Copy link
Author

Choose a reason for hiding this comment

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

There should probably be a better example, yeah. Will update soon!

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 23, 2025
@alex-semenyuk
Copy link
Member

@balt-dev
Thanks for your contribution
From wg-triage. Any updates on this PR?

@balt-dev
Copy link
Author

balt-dev commented Apr 7, 2025

I'm sorry, college has been quite a time sink for me and I haven't had the time to follow up as of recent - I will when I can, promise!

@balt-dev
Copy link
Author

It's been a rough few months. ._.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label May 31, 2025
@balt-dev
Copy link
Author

hm.

@balt-dev
Copy link
Author

balt-dev commented May 31, 2025

I think I messed up my fork so bad I'll have to reset it and put my code in from scratch in order to get rid of the merge commit. At least it's copy/pasteable?

@balt-dev
Copy link
Author

balt-dev commented May 31, 2025

What the hell is going on!?

@balt-dev
Copy link
Author

balt-dev commented May 31, 2025

For the record:
I ended up discarding my commits entirely, instead force-pushing the latest commit on the main repo to my fork, and then putting my changes back. I didn't touch src/tools/cargo at all (although it seems the latest commit, 7a7bcbb, did).

src/tools/cargo Outdated
Copy link
Author

Choose a reason for hiding this comment

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

I have no idea why this happened and no idea how to revert it. I don't even know if I should revert it. Could someone advise?

@rust-log-analyzer

This comment has been minimized.

@balt-dev
Copy link
Author

@rustbot review

I need help fixing this mess.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label May 31, 2025
@traviscross
Copy link
Contributor

I've fixed your branch so as to not touch any submodules.

@traviscross traviscross changed the title Implementation of push_mut Implementation of push_mut May 31, 2025
@traviscross traviscross changed the title Implementation of push_mut Implement push_mut May 31, 2025
@balt-dev
Copy link
Author

balt-dev commented Jun 1, 2025

Thank you!!

@balt-dev
Copy link
Author

balt-dev commented Jun 3, 2025

Should rust-lang/libs-team#579 be lumped in with this implementation?
I don't see why not.

@traviscross
Copy link
Contributor

traviscross commented Jun 3, 2025

Yes, I'd expect that should be OK. (It can always be broken out later if the reviewer prefers.)

Comment on lines +2515 to +2538
/// #![feature(push_mut)]
///
/// # #[allow(unused)]
/// #[derive(PartialEq, Eq, Debug)]
/// struct Item { identifier: &'static str, count: usize }
///
/// impl Default for Item {
/// fn default() -> Self {
/// return Self { identifier: "stone", count: 64 }
/// }
/// }
///
/// let mut items = vec![];
///
/// // We can mutate the just-pushed value without having to fetch it again
/// for count in [15, 35, 61] {
/// let item = items.push_mut(Item::default());
/// item.count = count;
/// }
///
/// assert_eq!(
/// items,
/// [Item { identifier: "stone", count: 15 }, Item { identifier: "stone", count: 35 }, Item { identifier: "stone", count: 61 }]
/// );
Copy link
Member

@ibraheemdev ibraheemdev Jun 8, 2025

Choose a reason for hiding this comment

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

This example is quite long. Documentation examples should generally be short and to the point.

Suggested change
/// #![feature(push_mut)]
///
/// # #[allow(unused)]
/// #[derive(PartialEq, Eq, Debug)]
/// struct Item { identifier: &'static str, count: usize }
///
/// impl Default for Item {
/// fn default() -> Self {
/// return Self { identifier: "stone", count: 64 }
/// }
/// }
///
/// let mut items = vec![];
///
/// // We can mutate the just-pushed value without having to fetch it again
/// for count in [15, 35, 61] {
/// let item = items.push_mut(Item::default());
/// item.count = count;
/// }
///
/// assert_eq!(
/// items,
/// [Item { identifier: "stone", count: 15 }, Item { identifier: "stone", count: 35 }, Item { identifier: "stone", count: 61 }]
/// );
/// #![feature(push_mut)]
///
/// let mut vec = vec![1, 2];
/// let last = vec.push(3);
/// assert_eq!(vec, [1, 2, 3]);
/// *last = 4;
/// assert_eq!(vec, [1, 2, 4]);
/// ```

@ibraheemdev
Copy link
Member

I'm happy to review Vec::insert_mut along with this PR, but also fine to merge after the review comment is addressed.

@ibraheemdev ibraheemdev added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 8, 2025
@rustbot
Copy link
Collaborator

rustbot commented Jun 9, 2025

⚠️ Warning ⚠️

  • Some commits in this PR modify submodules.

@balt-dev
Copy link
Author

balt-dev commented Jun 9, 2025

...again?

@balt-dev
Copy link
Author

balt-dev commented Jun 9, 2025

Sigh....

@balt-dev
Copy link
Author

balt-dev commented Jun 9, 2025

@traviscross How'd you fix the submodules?

@balt-dev
Copy link
Author

balt-dev commented Jun 9, 2025

image
Hm. I can't access anything here.

@rust-log-analyzer
Copy link
Collaborator

The job mingw-check-tidy failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)
info: removing rustup binaries
info: rustup is uninstalled
##[group]Image checksum input
mingw-check-tidy
# We use the ghcr base image because ghcr doesn't have a rate limit
# and the mingw-check-tidy job doesn't cache docker images in CI.
FROM ghcr.io/rust-lang/ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get install -y --no-install-recommends \
  g++ \
---

COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/

# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) && \
 python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
#
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
#    pip-compile --allow-unsafe --generate-hashes reuse-requirements.in
---
#15 2.831 Building wheels for collected packages: reuse
#15 2.832   Building wheel for reuse (pyproject.toml): started
#15 3.048   Building wheel for reuse (pyproject.toml): finished with status 'done'
#15 3.049   Created wheel for reuse: filename=reuse-4.0.3-cp310-cp310-manylinux_2_35_x86_64.whl size=132719 sha256=d2a2565e7037ad3883fb9337653f2e25bbb588534fbef3697286cbc26d1bf634
#15 3.049   Stored in directory: /tmp/pip-ephem-wheel-cache-ev71wf9t/wheels/3d/8d/0a/e0fc6aba4494b28a967ab5eaf951c121d9c677958714e34532
#15 3.052 Successfully built reuse
#15 3.052 Installing collected packages: boolean-py, binaryornot, tomlkit, reuse, python-debian, markupsafe, license-expression, jinja2, chardet, attrs
#15 3.453 Successfully installed attrs-23.2.0 binaryornot-0.4.4 boolean-py-4.0 chardet-5.2.0 jinja2-3.1.4 license-expression-30.3.0 markupsafe-2.1.5 python-debian-0.1.49 reuse-4.0.3 tomlkit-0.13.0
#15 3.454 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 4.009 Collecting virtualenv
#15 4.055   Downloading virtualenv-20.31.2-py3-none-any.whl (6.1 MB)
#15 4.197      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.1/6.1 MB 43.4 MB/s eta 0:00:00
#15 4.260 Collecting platformdirs<5,>=3.9.1
#15 4.265   Downloading platformdirs-4.3.8-py3-none-any.whl (18 kB)
#15 4.288 Collecting distlib<1,>=0.3.7
#15 4.295   Downloading distlib-0.3.9-py2.py3-none-any.whl (468 kB)
#15 4.303      ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 469.0/469.0 KB 77.6 MB/s eta 0:00:00
#15 4.341 Collecting filelock<4,>=3.12.2
#15 4.347   Downloading filelock-3.18.0-py3-none-any.whl (16 kB)
#15 4.429 Installing collected packages: distlib, platformdirs, filelock, virtualenv
#15 4.621 Successfully installed distlib-0.3.9 filelock-3.18.0 platformdirs-4.3.8 virtualenv-20.31.2
#15 4.621 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
#15 DONE 4.7s

#16 [10/11] COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
#16 DONE 0.0s
---
DirectMap4k:      155584 kB
DirectMap2M:     7184384 kB
DirectMap1G:    11534336 kB
##[endgroup]
Executing TIDY_PRINT_DIFF=1 npm install eslint@$(head -n 1 /tmp/eslint.version) &&  python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
+ head -n 1 /tmp/eslint.version
+ TIDY_PRINT_DIFF=1 npm install [email protected]
npm WARN deprecated [email protected]: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm WARN deprecated [email protected]: Rimraf versions prior to v4 are no longer supported
npm WARN deprecated [email protected]: Glob versions prior to v9 are no longer supported
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/config-array instead
npm WARN deprecated @humanwhocodes/[email protected]: Use @eslint/object-schema instead
npm WARN deprecated [email protected]: This version is no longer supported. Please see https://eslint.org/version-support for other options.

added 89 packages in 3s

17 packages are looking for funding
  run `npm fund` for details
+ python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest --extra-checks=py,cpp
##[group]Building bootstrap
    Finished `dev` profile [unoptimized] target(s) in 0.05s
##[endgroup]
WARN: currently no CI rustc builds have rustc debug assertions enabled. Please either set `rust.debug-assertions` to `false` if you want to use download CI rustc or set `rust.download-rustc` to `false`.
WARN: `rust.debug-assertions = true` will prevent downloading CI rustc as alt CI rustc is not currently built with debug assertions.
---
     /// Inserts an element at position `index` within the vector, shifting all
     /// elements after it to the right.
-    /// 
+    ///
     /// Returns [`None`] if the specified index is out of bounds.
     ///
     /// # Examples
Diff in /checkout/library/alloc/src/vec/mod.rs:2051:
     /// let x = vec.insert_mut(3, 6).unwrap();
     /// *x += 1;
     /// assert_eq!(vec, [1, 3, 5, 7, 9]);
-    /// 
+    ///
     /// let y = vec.insert_mut(7, 6);
     /// assert!(y.is_none());
     /// ```
fmt: checked 6049 files
Build completed unsuccessfully in 0:00:44
  local time: Mon Jun  9 01:09:26 UTC 2025
  network time: Mon, 09 Jun 2025 01:09:26 GMT

@tgross35
Copy link
Contributor

tgross35 commented Jun 9, 2025

@traviscross How'd you fix the submodules?

The easiest way is something like:

# Make sure you have rust-lang's `master` available
git remote add upstream https://github.com/rust-lang/rust.git
git fetch upstream master

# Do a soft reset to the point where you forked off from master.
# Mild warning: this will keep the diff but get rid of the new
# commits (the files will show up as having unstaged changes)
git reset "$(git merge-base HEAD upstream/master)"

# Just reset submodules
git submodule deinit --all --force

# Add the changes you actually want and create a new commit
git add library
git commit

# Update this PR, which will replace the three commits here with
# the one new commit
git push --force-with-lease

git rebase -i "$(git merge-base HEAD upstream/master)" will work in the future then , if you need to squash at some point.

For future reference, it's usually better practice to always work on branches rather than your fork's master so you can do git branch --set-upstream-to=upstream/master on your local master, then it will always track rust-lang/rust without needing to continually update your fork. But since this PR is from your master, don't do this until it merges :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. T-libs Relevant to the library team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants