fallible_alloc 0.2.0

Fallible rust stable std collections allocations
Documentation
  • Coverage
  • 100%
    13 out of 13 items documented7 out of 8 items with examples
  • Size
  • Source code size: 70.82 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 2.07 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • zkud/fallible-alloc
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • zkud

Fallible rust stable std collections allocations

failable_alloc codecov Hits-of-Code

At the moment we have an unstabilized allocations API in the std, so this is a temporary safe solution for a stable rust.

Usage example

To create a vector you could use this code example:

use fallible_alloc::vec::alloc_with_size;

...

let vector_size: usize = 10;
let maybe_vector = alloc_with_size::<f64>(vector_size);

match maybe_vector {
  Ok(vec) => println!("Created a vec with size 10"),
  Err(error) => println!("Failed to create a vec, reason: {}", error)
}

As you could see, the maybe_vector has a Result<Vec<T>, AllocError> type, so now it's possible to handle a part of allocation errors.

Also it's possible to change the allocator used by crate with this code example:

use std::alloc::{GlobalAlloc, System, Layout};

struct MyAllocator;

unsafe impl GlobalAlloc for MyAllocator {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        System.alloc(layout)
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout)
    }
}

#[global_allocator]
static GLOBAL: MyAllocator = MyAllocator;