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