-
Notifications
You must be signed in to change notification settings - Fork 13k
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
add try_reserve and try_reserve_exact to Vec #43890
Conversation
TODO:
|
r? @sfackler (rust_highfive has picked a reviewer for you, use r? to override) |
@bors: try |
add try_reserve and try_reserve_exact to Vec Full rationale and proposal document coming Soon(TM), but I believe this is the API/impl that the gecko devs should use to solve their issues.
☀️ Test successful - status-travis |
It seems like this only enables the oom handler from https://doc.rust-lang.org/alloc/oom/fn.set_oom_handler.html to be called, and it doesn't seem to allow to recover from the allocation failure. So, say, for example, you have a browser trying to load a resource that would require to allocate a very large buffer of hundreds of megabytes, and there's not enough consecutive address space free to allocate those hundreds of megabytes. The allocator returns a failure. But then what? |
@glandium I don't understand what you're trying to say. What more could we possibly expose? The collection is left unmodified on failure, so you can do whatever you want. There are a few patterns users of these APIs follow:
|
So there are two things. The first is that I missed that try_reserve is to be called before each fallible operation on the vec, which is cumbersome, but enough to get started. The second is that this doesn't cover all the other types that enclose a vec internally (like HashMap). |
HashMap doesn't contain a Vec, but yes it should also have this API. As should VecDeque. Both of these types already have This PR is the first step towards developing our story on fallible allocation. It establishes the CollectionAllocErr type, and a basic idiom. This basic idiom is a decent 80/20 solution, as it covers almost every case with minimal API impact. Other collections, such as the fork of libcollections the Gecko devs will likely implement for FF57, can implement this idiom without diverging from the standard library. |
(still finishing up the longform rationale) |
/// (usually `isize::MAX` bytes). | ||
CapacityOverflow, | ||
/// Error due to the allocator (see the `AllocErr` type's docs). | ||
AllocErr(AllocErr), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can make an AllocErr
from a custom string, so is it worth having the distinction between these two errors at the API level? (e.g. do you think that these'll get matched on and dispatch to different behaviors?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And the second after I read this I see panics on capacity overflow and oom on allocation errors...
Rationale: rust-lang/rfcs#2116 |
I'm going to close this pending RFC approval. THANK YOU @gankro for pushing so hard on this topic! We'll try to move quickly on the RFC. |
Full rationale and proposal document coming Soon(TM), but I believe this is the API/impl that the gecko devs should use to solve their issues.