Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ jobs:
- run: cargo test

msrv:
name: MSRV (1.31)
name: MSRV (1.39)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.31"
- run: cargo +1.31 check
toolchain: "1.39"
- run: cargo +1.39 check
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ sub-ranges on request. It uses a best-fit strategy to reduce fragmentation and
automatically merges adjacent free ranges on deallocation. Allocations can
optionally be aligned to a given boundary without wasting the padding space.

## `no_std` support

This crate is `#![no_std]` and depends on `alloc`.

## Example

```rust
Expand All @@ -31,7 +35,7 @@ alloc.grow_to(2048);

## Minimum Supported Rust Version

The MSRV of this crate is at least 1.31, possibly earlier. It will only be
The MSRV of this crate is at least 1.39. It will only be
bumped in a breaking release.

## License
Expand Down
27 changes: 17 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
//! `Range<T>` values from a pool. It uses a best-fit strategy to reduce
//! fragmentation and automatically merges adjacent free ranges on deallocation.
//!
//! The crate is `#![no_std]` and uses `alloc` for internal storage.
//!
//! # Example
//!
//! ```
Expand All @@ -21,10 +23,15 @@
//!
//! # Minimum Supported Rust Version
//!
//! The MSRV of this crate is at least 1.31, possibly earlier. It will only be
//! The MSRV of this crate is at least 1.39. It will only be
//! bumped in a breaking release.

use std::{
#![no_std]
extern crate alloc;

use alloc::vec;
use alloc::vec::Vec;
use core::{
fmt::Debug,
iter::Sum,
ops::{Add, AddAssign, Range, Rem, Sub},
Expand Down Expand Up @@ -441,20 +448,20 @@ mod tests {
let mut alloc = RangeAllocator::new(0..10);
// Test if an allocation works
assert_eq!(alloc.allocate_range(4), Ok(0..4));
assert!(alloc.allocated_ranges().eq(std::iter::once(0..4)));
assert!(alloc.allocated_ranges().eq(core::iter::once(0..4)));
// Free the prior allocation
alloc.free_range(0..4);
// Make sure the free actually worked
assert_eq!(alloc.free_ranges, vec![0..10]);
assert!(alloc.allocated_ranges().eq(std::iter::empty()));
assert!(alloc.allocated_ranges().eq(core::iter::empty()));
}

#[test]
fn test_out_of_space() {
let mut alloc = RangeAllocator::new(0..10);
// Test if the allocator runs out of space correctly
assert_eq!(alloc.allocate_range(10), Ok(0..10));
assert!(alloc.allocated_ranges().eq(std::iter::once(0..10)));
assert!(alloc.allocated_ranges().eq(core::iter::once(0..10)));
assert!(alloc.allocate_range(4).is_err());
alloc.free_range(0..10);
}
Expand All @@ -464,7 +471,7 @@ mod tests {
let mut alloc = RangeAllocator::new(0..11);
// Test if the allocator runs out of space correctly
assert_eq!(alloc.allocate_range(10), Ok(0..10));
assert!(alloc.allocated_ranges().eq(std::iter::once(0..10)));
assert!(alloc.allocated_ranges().eq(core::iter::once(0..10)));
assert!(alloc.allocate_range(4).is_err());
alloc.grow_to(20);
assert_eq!(alloc.allocate_range(4), Ok(10..14));
Expand All @@ -480,7 +487,7 @@ mod tests {
alloc.free_range(0..3);

alloc.grow_to(9);
assert_eq!(alloc.allocated_ranges().collect::<Vec<_>>(), [3..6]);
assert!(alloc.allocated_ranges().eq(core::iter::once(3..6)));
}
#[test]
fn test_grow_with_hole_in_middle() {
Expand Down Expand Up @@ -527,7 +534,7 @@ mod tests {
assert_eq!(alloc.allocate_range(10), Ok(80..90));
assert_eq!(alloc.allocate_range(10), Ok(90..100));
assert_eq!(alloc.free_ranges, vec![]);
assert!(alloc.allocated_ranges().eq(std::iter::once(0..100)));
assert!(alloc.allocated_ranges().eq(core::iter::once(0..100)));
alloc.free_range(10..20);
alloc.free_range(30..40);
alloc.free_range(50..60);
Expand Down Expand Up @@ -565,7 +572,7 @@ mod tests {
assert_eq!(alloc.allocate_range(4), Ok(96..100));
// Check that nothing is free.
assert_eq!(alloc.free_ranges, vec![]);
assert!(alloc.allocated_ranges().eq(std::iter::once(0..100)));
assert!(alloc.allocated_ranges().eq(core::iter::once(0..100)));
}

#[test]
Expand Down Expand Up @@ -597,7 +604,7 @@ mod tests {
alloc.free_range(6..9);
alloc.free_range(3..6);
assert_eq!(alloc.free_ranges, vec![0..9]);
assert!(alloc.allocated_ranges().eq(std::iter::empty()));
assert!(alloc.allocated_ranges().eq(core::iter::empty()));
}

#[test]
Expand Down