#bump #into #allocator #slice #space

no-std bump-into

a no_std bump allocator sourcing space from a user-provided slice

19 releases (6 breaking)

new 0.7.2 Feb 22, 2021
0.7.1 Oct 5, 2020
0.6.0 Sep 30, 2020
0.4.3 Jul 28, 2020
0.2.0 Feb 20, 2020

#59 in No standard library

Download history 13/week @ 2020-11-05 22/week @ 2020-11-26 21/week @ 2020-12-03 20/week @ 2020-12-10 1/week @ 2020-12-24 54/week @ 2021-02-11 67/week @ 2021-02-18

58 downloads per month

MIT license

43KB
736 lines

bump-into

crates.io docs.rs CI status

A no_std bump allocator sourcing space from a user-provided mutable slice rather than from a global allocator, making it suitable for use in embedded applications and tight loops.

Drop behavior

Values held in BumpInto allocations are never dropped. If they must be dropped, you can use core::mem::ManuallyDrop::drop or core::ptr::drop_in_place to drop them explicitly (and unsafely). In safe code, you can allocate an Option and drop the value inside by overwriting it with None.

Example

use bump_into::{self, BumpInto};

// allocate 64 bytes of uninitialized space on the stack
let mut bump_into_space = bump_into::space_uninit!(64);
let bump_into = BumpInto::from_slice(&mut bump_into_space[..]);

// allocating an object produces a mutable reference with
// a lifetime borrowed from `bump_into_space`, or gives
// back its argument in `Err` if there isn't enough space
let number: &mut u64 = bump_into
    .alloc_with(|| 123)
    .ok()
    .expect("not enough space");
assert_eq!(*number, 123);
*number = 50000;
assert_eq!(*number, 50000);

// slices can be allocated as well
let slice: &mut [u16] = bump_into
    .alloc_n_with(5, core::iter::repeat(10))
    .expect("not enough space");
assert_eq!(slice, &[10; 5]);
slice[2] = 100;
assert_eq!(slice, &[10, 10, 100, 10, 10]);

Copying

Copyright (c) 2020 autumnontape

This project may be reproduced under the terms of the MIT license, a copy of which should be available to you in a file called COPYING.

No runtime deps