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

Tracking Issue for more_fallible_allocation_methods #86942

Closed
3 tasks
Ericson2314 opened this issue Jul 7, 2021 · 6 comments
Closed
3 tasks

Tracking Issue for more_fallible_allocation_methods #86942

Ericson2314 opened this issue Jul 7, 2021 · 6 comments
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@Ericson2314
Copy link
Contributor

Ericson2314 commented Jul 7, 2021

Feature gate: #![feature(more_fallible_allocation_methods)]

This is a tracking issue for adding more fallible allocation methods, i.e. ones where allocation failure will return Err(_) rather than calling some diverging global handler.

It overlaps a bit with #32838, since the most general methods will be fallible and allocator-agnostic.

Public API

Basically, all the methods in the alloc crate that begin with try_. Perhaps others in std someday.

Steps / History

Unresolved Questions

@Ericson2314 Ericson2314 added C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. labels Jul 7, 2021
@leo60228
Copy link
Contributor

leo60228 commented Dec 2, 2021

Not sure where this should go, but it seems strange to me that try_reserve was stabilized in 1.57 but Box::try_new is still unstable.

@TennyZhuang
Copy link
Contributor

Should #91529 be a part of the tracking issue?

@Xuanwo
Copy link
Contributor

Xuanwo commented Dec 5, 2021

Is this issue still going on? Is it OK to add try_ alike API in alloc? I'm interested to send PRs around this feature.

@Ericson2314
Copy link
Contributor Author

I have had little time to debug the tiny few failures or #86938 --- it's not that it would take very long, but that switching tasks from what I am usually doing has a mental overhead.

Landing that PR is still good, and will help Rust for LInux slightly which I think is very important, so @Xuanwo if you are interested I would be very thankful and appreciative if you might take over that PR, and then add more try_ building upon it.

@Xuanwo
Copy link
Contributor

Xuanwo commented Mar 13, 2022

I'm sorry I don't have enough time to finish this issue now. Interested contributors, please take over themselves. Existing work could be found at #91559. Thanks for all the help!

dpaoliello added a commit to dpaoliello/rust that referenced this issue Oct 10, 2022
…hout infallible allocation methods

As a part of the work for rust-lang#86942 the `no_global_oom_handling` cfg was added in rust-lang#84266, however enabling that cfg makes it difficult to use `Vec`: the `vec!` macro is missing and there is no way to insert or push new elements, nor to create a `Vec` from an iterator (without resorting to `unsafe` APIs to manually expand and write to the underlying buffer).

This change adds `try_` equivalent methods for all methods in `Vec` that are disabled by `no_global_oom_handling` as well as a `try_vec!` as the equivalent of `vec!`.

Performance and implementation notes: A goal of this change was to make sure to NOT regress the performance of the existing infallible methods - a naive approach would be to move the actual implementation into the fallible method, then call it from the infallible method and `unwrap()`, but this would add extra compare+branch instructions into the infallible methods. It would also be possible to simply duplicate the code for the fallible version - I did opt for this in a few cases, but where the code was larger and more complex this would lead to a maintenance nightmare. Instead, I moved the implementation into an `*_impl` method that was then specialized based on a new `VecError` trait and returned `Result<_, VecError>`, this trait also provided a pair of methods for raising errors. Never (`!`) was used as the infallible version of this trait (and always panics) and `TryReserveError` as the fallible version (and returns the correct error object). All these `VecError` method were marked with `#[inline]`, so after inlining the compiler could see for the infallible version always returns `Ok()` on success (and panics on error) thus the `?` operator or `unwrap()` call was a no-op, and so the same non-branching instructions as before are generated.

I also added `try_from_iter` and `try_expand` methods for completeness, even though their infallible equivalents are trait implementations.
@dtolnay
Copy link
Member

dtolnay commented Jun 25, 2024

The feature tracked by this issue never got merged. I'll close the tracking issue since this is not tracking any work at the moment, but we can reopen (or create a new one) if someone picks this back up in the future.

@dtolnay dtolnay closed this as completed Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-tracking-issue Category: A tracking issue for an RFC or an unstable feature. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants