Repository navigation
Open
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-slice-vecArea: `[T]` and `Vec<T>`Area: `[T]` and `Vec<T>`A-strArea: `str` and `String`Area: `str` and `String`A-vecdequeArea: `std::collections::VecDeque`Area: `std::collections::VecDeque`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
}
}Reacted by Kostas Tsakas, Stanislav Tkach, Diana, Chuigda Whitegive, Michael Partheil, Vin Singh, Lynne, Isak Sunde Singh, Jesus Hernandez, Sayan and 15 more
Reacted by Kornel, Jesus Hernandez, José Julián Espina, Felipe S. S. Schneider, MarkTanashchuk, Diego Reis and Abrahem.79
Reacted by Felipe S. S. Schneider
Metadata
Metadata
Assignees
Labels
A-allocatorsArea: Custom and system allocatorsArea: Custom and system allocatorsA-slice-vecArea: `[T]` and `Vec<T>`Area: `[T]` and `Vec<T>`A-strArea: `str` and `String`Area: `str` and `String`A-vecdequeArea: `std::collections::VecDeque`Area: `std::collections::VecDeque`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?
130 remaining items
SimonSapin 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 (…