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 try_reserve: RFC 2116 fallible collection allocation #48043

Open
3 of 5 tasks
aturon opened this issue Feb 7, 2018 · 104 comments
Open
3 of 5 tasks

Tracking issue for try_reserve: RFC 2116 fallible collection allocation #48043

aturon opened this issue Feb 7, 2018 · 104 comments
Labels
A-allocators Area: Custom and system allocators A-collections Area: `std::collection` B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue.

Comments

@aturon
Copy link
Member

aturon commented Feb 7, 2018

This is a tracking issue for the try_reserve part of the RFC "fallible collection allocation" (rust-lang/rfcs#2116).

Steps:

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
    }
}
@aturon aturon added B-RFC-approved Blocker: Approved by a merged RFC but not yet implemented. T-libs-api Relevant to the library API team, which will review and decide on the PR/issue. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC labels Feb 7, 2018
@snf
Copy link
Contributor

snf commented Feb 22, 2018

Hi, this feature will require oom and try_reserve to be implemented. I'm needing try_reserve and 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
Copy link
Contributor

snf commented 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
Copy link
Member Author

aturon commented Mar 5, 2018

cc @gankro, can you help mentor?

bors added a commit that referenced this issue Mar 15, 2018
Fallible allocation

Implementing RFC#2116 [Fallible Allocation](#48043) .
Work in progress. Initially adding @gankro's try_reserve for Vec.
@SimonSapin
Copy link
Contributor

try_reserve methods 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 in Cargo.toml. It’s not clear to me how this can work.

@glandium
Copy link
Contributor

glandium commented May 4, 2018

Unfortunately the RFC does not discuss how this would be implemented, other than being controlled by a flag in Cargo.toml. It’s not clear to me how this can work.

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
Copy link
Contributor

@alexcrichton, how does #51041 fit with the goal of implementing some way to opt into oom=panic?

@alexcrichton
Copy link
Member

@SimonSapin we'd decide that the regression is acceptable, and we'd revert the change.

@SimonSapin
Copy link
Contributor

You use a conditional tense, but oom=panic is already in an accepted RFC (though the implementation strategy is not quite settled).

@alexcrichton
Copy link
Member

Ok sure, we can figure it out in the implementation details? AFAIK there's no need to revert the change as it has basically no benefit today. It's a trivial change to revert as well, it's one line in the standard library

@Stargateur
Copy link
Contributor

Stargateur commented 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-allocators Area: Custom and system allocators A-collections Area: `std::collection` B-RFC-implemented Blocker: Approved by a merged RFC and implemented. B-unstable Blocker: Implemented in the nightly compiler and unstable. C-tracking-issue Category: An issue tracking the progress of sth. like the implementation of an RFC Libs-Tracked Libs issues that are tracked on the team's project board. 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