Repository navigation
Tracking issue for try_reserve: RFC 2116 fallible collection allocation #48043
Copy link
Copy link
Open
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-collectionsArea: `std::collections`Area: `std::collections`B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Description
This is a tracking issue for the try_reserve part of the RFC "fallible collection allocation" (rust-lang/rfcs#2116).
Steps:
- Implement the RFC Fallible allocation #48648
- Add
HashSet::try_reserve: Replace HashMap implementation with SwissTable (as an external crate) #58623
- Add
- Finalize the error type
- Adjust documentation (see instructions on forge)
- Stabilization PR (see instructions on forge)
API:
impl /* each of String, Vec<T>, VecDeque<T> */ {
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {…}
pub fn try_reserve_exact(&mut self, additional: usize) -> Result<(), TryReserveError> {…}
}
impl /* each of HashMap<K, V> and HashSet<T> */ {
pub fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {…}
}
/// The error type for `try_reserve` methods.
#[derive(Clone, PartialEq, Eq, Debug)]
pub enum TryReserveError { // in std::collections
/// Error due to the computed capacity exceeding the collection's maximum
/// (usually `isize::MAX` bytes).
CapacityOverflow,
/// The memory allocator returned an error
AllocError {
/// The layout of allocation request that failed
layout: Layout,
#[doc(hidden)]
#[unstable(feature = "container_error_extra", issue = "0", reason = "\
Enable exposing the allocator’s custom error value \
if an associated type is added in the future: \
https://github.com/rust-lang/wg-allocators/issues/23")]
non_exhaustive: (),
},
}
impl From<LayoutErr> for TryReserveError {
fn from(_: LayoutErr) -> Self {
TryReserveError::CapacityOverflow
}
}Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-collectionsArea: `std::collections`Area: `std::collections`B-RFC-implementedBlocker: Approved by a merged RFC and implemented but not stabilized.Blocker: Approved by a merged RFC and implemented but not stabilized.B-unstableBlocker: Implemented in the nightly compiler and unstable.Blocker: Implemented in the nightly compiler and unstable.C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCI-libs-radarLibs issues that are tracked on the team's radar.Libs issues that are tracked on the team's radar.T-libsRelevant to the library team, which will review and decide on the PR/issue.Relevant to the library team, which will review and decide on the PR/issue.
Activity
snf commentedon Feb 22, 2018
Hi, this feature will require
oomandtry_reserveto be implemented. I'm needingtry_reserveand I'm willing to work (slowly) on it if noone else does.I see that the previous commit by @gankro is here and seems to match the RFC description.
Is there anything else I should consider before rebasing that PR?
snf commentedon Mar 3, 2018
I can use some mentorship on what's the best way to implement oom=panic/abort . Should I emit a global variable through rustc depending on this case?
I was taking inspiration from #32900 but there might be an easier way to emit a boolean global variable.
aturon commentedon Mar 5, 2018
cc @gankro, can you help mentor?
SimonSapin commentedon Apr 5, 2018
try_reservemethods are implemented in Nightly, but the accepted RFC also includes the ability to make allocation failure cause a panic rather than abort and that part is not implemented. Unfortunately the RFC does not discuss how this would be implemented, other than being controlled by a flag inCargo.toml. It’s not clear to me how this can work.glandium commentedon May 4, 2018
We've been talking about panic::set_hook-like hooking for the new oom lang item in #49668, this could be how this works.
SimonSapin commentedon May 29, 2018
@alexcrichton, how does #51041 fit with the goal of implementing some way to opt into oom=panic?
126 remaining items
orlp commentedon Dec 27, 2021
It's super minor but
TryReserveErrorKindis missing periods at the end of sentences in the documentation.8573 commentedon Dec 27, 2021
The only such sentence I see there is "The memory allocator returned an error"; the others are just sentence fragments, which may not deserve periods (for example, where sentence fragments may be used in Wikipedia, their style guide (probably the result of compromise between many strong opinions) says these "should in most cases not end with a period").
orlp commentedon Dec 27, 2021
https://doc.rust-lang.org/1.0.0/style/style/comments.html#sentence-structure
Sentence fragments should end with a period in Rust, even if full sentences are preferred.
safinaskar commentedon Jan 21, 2022
Important news!
HashMapgot methodtry_insert( #82766 ), wheretry_means something completely different toBox::try_new. Please, prevent somehow thistry_insertfrom becoming stableSimonSapin commentedon Jan 24, 2022
Please file an issue dedicated to the naming of
try_insertsince this thread is already covering other topics.Toru3 commentedon Aug 16, 2022
The comment of example code is seems wrong.
If size of
datais about 60% of system memory,try_reservemight success because OS doesn't allocate physical memory immediately.Writing to allocated memory causes page fault and OS allocates memory.
Sum of size of old data and new data is about 120% of system memory.
So, OOM might occur.
List of the comments
kornelski commentedon Aug 25, 2022
@Toru3 Overcommit is a known limitation, and it's been considered in the RFC. You can mitigate the problem on the OS level either by disabling overcommit, or running code in containers with limited amount of memory. Within Rust programs you can reduce chance of hitting the problem by using the
capallocator with a limit below the amount of physical memory available.Stargateur commentedon Mar 13, 2024
Would it be possible/wise to put TryReserveError into core ? I may need to have access to this struct when my crate compile without alloc.
Added try_with_capacity
studyingegret commentedon Jan 2, 2026
Just to check my understanding, are
TryReserveError::kindandTryReserveErrorKindthe only blockers now?abrahem79 commentedon Feb 9, 2026
tgross35 commentedon Mar 31, 2026
Some concerns about the contents of
TryReserveErrorKindrust-lang/wg-allocators#106 (comment)test(testing/oom): pin RawVec capacity-guard for synthetic OOM helper (…