fallible_alloc/vec/mod.rs
1//! Vector fallible allocations
2
3use super::alloc_error;
4use crate::util::alloc as util_alloc;
5
6/// Allocates a [`Vec<T>`] with given size
7///
8/// Usage example
9/// ```rust
10/// use fallible_alloc::vec::alloc_with_size;
11/// let size = 123;
12/// match alloc_with_size::<i32>(size) {
13/// Ok(vec) => println!("Created a vec with size 10"),
14/// Err(error) => println!("Failed to create a vec, reason: {}", error)
15/// };
16/// ```
17///
18/// # Errors
19///
20/// If allocation is not possible due to issues with memory layouts or not enough memory,
21/// it will return an [AllocError](crate::alloc_error::AllocError)
22pub fn alloc_with_size<T: Sized>(size: usize) -> Result<Vec<T>, alloc_error::AllocError> {
23 let buffer = util_alloc::alloc_array(size)?;
24 Ok(unsafe { Vec::from_raw_parts(buffer, size, size) })
25}