#vec #try-reserve #with-capacity #into-raw-parts

with_capacity_safe

A safer alternative to Vec::with_capacity with into_raw_parts which can be used from stable

6 releases (3 breaking)

0.4.2 Dec 3, 2021
0.4.1 Dec 3, 2021
0.4.0 Nov 12, 2021
0.3.0 Feb 24, 2021
0.1.0 Feb 17, 2021

#1781 in Rust patterns

Download history 14/week @ 2024-01-04 25/week @ 2024-01-11 12/week @ 2024-01-18 4/week @ 2024-01-25 13/week @ 2024-02-01 33/week @ 2024-02-08 33/week @ 2024-02-15 31/week @ 2024-02-22 42/week @ 2024-02-29 45/week @ 2024-03-07 37/week @ 2024-03-14 55/week @ 2024-03-21 83/week @ 2024-03-28 36/week @ 2024-04-04 42/week @ 2024-04-11 24/week @ 2024-04-18

189 downloads per month
Used in 15 crates (9 directly)

MIT license

6KB

Vec::with_capacity(capacity) panics / aborts when the capacity is too large.

This is a safer alternative which reports Error using try_reserve_exact

//let's pretend this is an arbitrary number read from a broken file
let number_from_file : usize = 100_000_000_000_000;

//try to create a 100TB Vec
let result : Result<Vec<u8>, _> = vec_with_capacity_safe(number_from_file);

//An error is reported
assert!(result.is_err());

vec_into_raw_parts decomposes Vec into ts raw components.

Minimum Supported Version of Rust

1.57.0(stable)

changelog

0.4.2 TryReserve is stabilized at Rust 1.57.0

0.3.0

  • removed WcsErrorType **** Breaking Change ****
  • changed WcsError to enum again. **** Breaking Change ****
  • added try_reserve_error() method to WcsError

0.2.0

  • Added some documentations.
  • Changed implementation to try_reserve_exact(), just in case.
  • Removed WcsError::new **** breaking change ***

No runtime deps