try_reserve
A stable implementation of TryReserveError that exposes TryReserveErrorKind.
Overview
This crate provides a stable implementation of the TryReserveError and TryReserveErrorKind types, which are currently not fully exposed in the Rust standard library.
This is a workaround for rust-lang/rust#48043, an RFC that has been pending for stabilization for 7 years.
Purpose
The sole purpose of this crate is to expose the normally private TryReserveErrorKind enum.
This allows for easier and unified creation of custom collections that need to return appropriate allocation error types.
Features
no_stdsupport by default- Full compatibility with std's
TryReserveErrorwhen available usingtansmutation - Complete integration with already existing types inside the std that use
try_reserve()if thestdflag is turned on
Usage
Add this to your Cargo.toml:
[]
= "0.2" # No std
Or with the std integrations:
[]
= { = "0.2", = ["std"] }
Basic usage
use ;
// Implement the TryReserve trait for your custom collection
// Or create your own error with specific kinds
With std
To use this crate in a std environment, enable the no_std feature:
[]
= { = "0.2", = ["std"] }
Error Types
TryReserveError
This is the main error type returned by try_reserve methods. It wraps a TryReserveErrorKind and provides a clean API.
TryReserveErrorKind
An enum with the following variants:
CapacityOverflow: Error due to the computed capacity exceeding the collection's maximum (usuallyisize::MAXbytes).AllocError { layout }: Error returned when the memory allocator fails. Contains the layout of the allocation request that failed.
Conversions
When in a std environment, the crate provides conversions between its error types and the standard library's error types:
use TryReserveError;
// Convert from std to this crate's error type
let std_error = Vec::with_capacity
.try_reserve
.unwrap_err;
let our_error = from;
// or
// let our_error: TryReserveError = std_error.into();
// And back again
let std_error_again = from;
Why this crate exists
The Rust standard library has had a pending RFC for the stabilization of the TryReserveErrorKind enum for 7 years (see rust-lang/rust#48043).
Without access to this enum, it's difficult to create custom collections that properly handle and report allocation errors.
This crate provides a stable workaround until the standard library stabilizes this API.
License
Licensed under
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.