#reserve #traits #no-std #stabilization #no-alloc

no-std try_reserve

Stable implementation of the TryReserveError from std for custom collections

3 releases

new 0.1.3 Mar 6, 2025
0.1.2 Feb 25, 2025
0.1.1 Feb 25, 2025
0.1.0 Feb 25, 2025

#864 in Data structures

Download history 289/week @ 2025-02-22 129/week @ 2025-03-01

418 downloads per month

MIT/Apache

15KB
163 lines

try_reserve

Crates.io Documentation Rust MIT/Apache-2 licensed

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_std support via feature flag
  • Full compatibility with std's TryReserveError when available using tansmutation
  • Complete integration with already existing types inside the std that use try_reserve()

Usage

Add this to your Cargo.toml:

[dependencies]
try_reserve = "0.1"

Basic usage

use try_reserve::{TryReserve, error::{TryReserveError, TryReserveErrorKind}};

// Implement the TryReserve trait for your custom collection
struct MyCollection<T> {
    data: Vec<T>,
}

impl<T> TryReserve for MyCollection<T> {
fn try_reserve(&mut self, additional: usize) -> Result<(), TryReserveError> {
    // Attempt to reserve space
    self.data.try_reserve(additional)
    .map_err(TryReserveError::from)
    }
}

// Or create your own error with specific kinds
fn example() -> Result<(), TryReserveError> {
    // Create a capacity overflow error
    let overflow_error = TryReserveErrorKind::CapacityOverflow;
    Err(overflow_error.into())
}

With no_std

To use this crate in a no_std environment, enable the no_std feature:

[dependencies]
try_reserve = { version = "0.1", features = ["no_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 (usually isize::MAX bytes).
  • AllocError { layout }: Error returned when the memory allocator fails. Contains the layout of the allocation request that failed.

Conversions

When not in a no_std environment, the crate provides conversions between its error types and the standard library's error types:

use try_reserve::error::TryReserveError;

// Convert from std to this crate's error type
let std_error = Vec::<i32>::with_capacity(100)
    .try_reserve(usize::MAX)
    .unwrap_err();
let our_error = TryReserveError::from(std_error);
// or
// let our_error: TryReserveError = std_error.into();

// And back again
let std_error_again = std::collections::TryReserveError::from(our_error);

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

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.

No runtime deps

Features