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

util: add DelayQueue::try_remove #5052

Merged
merged 4 commits into from
Sep 28, 2022
Merged

Conversation

erwanor
Copy link
Sponsor Contributor

@erwanor erwanor commented Sep 26, 2022

Adds a safe method to remove an item from the queue.

Fixes: #3558

Motivation

DelayQueue does not expose any way to check that an item is present in its slab. On the other hand, remove will panic if a client attempts to remove a key that is not in the queue. As a result, client libraries that have to avoid panics must maintain an extra HashSet around the queue.

Solution

Add a try_remove method which performs an internal membership check before calling remove. If a client attempts to remove a key that isn't there, try_remove will return None instead of panicking.

Since try_remove is the only method of its kind, I have elected to make it return an Option<Expired<T>> instead Result<Expired<T>, SomeNewErrorEnum>, at least for now.

@Darksonn Darksonn added A-tokio-util Area: The tokio-util crate M-time Module: tokio/time labels Sep 26, 2022
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

This looks reasonable.

tokio-util/src/time/delay_queue.rs Outdated Show resolved Hide resolved
tokio-util/src/time/delay_queue.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@Darksonn Darksonn left a comment

Choose a reason for hiding this comment

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

Thanks!

@Darksonn Darksonn enabled auto-merge (squash) September 27, 2022 21:26
@Darksonn Darksonn merged commit e835427 into tokio-rs:master Sep 28, 2022
dbischof90 pushed a commit to dbischof90/tokio that referenced this pull request Oct 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio-util Area: The tokio-util crate M-time Module: tokio/time
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants