Skip to content

Add retain_back (aka truncate_front) method to Deque#663

Open
nullstalgia wants to merge 7 commits into
rust-embedded:mainfrom
nullstalgia:deque_retain_back
Open

Add retain_back (aka truncate_front) method to Deque#663
nullstalgia wants to merge 7 commits into
rust-embedded:mainfrom
nullstalgia:deque_retain_back

Conversation

@nullstalgia

Copy link
Copy Markdown
Contributor

Howdy again!

I originally intended to do this after championing for the stabilization of alloc's truncate_front method, but it appears someone got to it before me!

Feeling inspired, I jumped right to the heapless implementation like I had done earlier last year for Deque::truncate, using the new name decided for the method in the stabilization PR.

Miri seems happy, and running through the logic on pen and paper it seems sound, but please do let me know if I missed anything or if some more exhaustive tests would be needed!

@zeenix zeenix left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks! As usual, I only have nitpicks to offer. :)

Comment thread src/deque.rs Outdated
Comment thread src/deque.rs Outdated
Comment thread src/deque.rs Outdated
Comment thread src/deque.rs Outdated
Comment thread src/deque.rs Outdated
@nullstalgia

nullstalgia commented Jun 7, 2026

Copy link
Copy Markdown
Contributor Author

@zeenix Since this PR is essentially a continuation of the implementation I did for truncate, am I to apply these same suggested changes to that method as well?

The Safety comments (and overall implementation) I had there and here are essentially copies from the ones from the stdlib's implementation of VecDeque::truncate and truncate_front VecDeque::retain_back. Otherwise there would be unexplained differences in the implementations despite how logically similar they are both between each other here and the stdlib's. Would that fall into the scope of a different PR?

@zeenix

zeenix commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

@zeenix Since this PR is essentially a continuation of the implementation I did for truncate, am I to apply these same suggested changes to that method as well?

Yes please. It's good to be always consistent but please in a separate PR than this.

The Safety comments (and overall implementation) I had there and here are essentially copies from the ones from the stdlib's implementation of VecDeque::truncate and truncate_front VecDeque::retain_back. Otherwise there would be unexplained differences in the implementations despite how logically similar they are both between each other here and the stdlib's.

The important bit is the logic and actual code. As long as they remain as similar as possible, I don't think deviating for improvement, is a problem.

Would that fall into the scope of a different PR?

This would be fixup of this umerged work, so I don't think so.

EDIT: I might have misunderstood you here. I mean, the fixups/changes to the unmerged changes in this PR, should go in this PR. Changes to existing code (already merged) should go into a separate one. I hope it's clear now. :)

@nullstalgia

Copy link
Copy Markdown
Contributor Author

Thanks for clarifying, I'll push the requested changes + submit an accompanying PR soon.

@zeenix zeenix left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm otherwise.

Comment thread src/deque.rs
Comment on lines +2542 to +2545
assert!(
tester.push_front(Droppable::new()).is_ok(),
"deque must have room for all {LEN} entries"
);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not simply:

tester
    .push_front(Droppable::new())
    .expect("deque must have room for all {LEN} entries");

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

.expect() does not support formatting strings as it is not a macro.

Would you prefer that I use .expect(format!()), or simply not mention the LEN at all?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@nullstalgia nullstalgia Jun 10, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I might be missing something, but your example isn't very clear/is conflicting.

It's expect()-ing the Ok(()) variant (so no error message gets printed due to a failed unwrap), and the #![allow(unused)] hides the warning that LEN is never read.

https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=06510eeb3a4c30ff16264f72f5f914f2

Comment thread src/deque.rs Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants