Skip to content

Commit 8ded7db

Browse files
waywardmonkeyscwfitzgerald
authored andcommitted
no_std: use no_std + alloc
1 parent 4aec732 commit 8ded7db

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ sub-ranges on request. It uses a best-fit strategy to reduce fragmentation and
77
automatically merges adjacent free ranges on deallocation. Allocations can
88
optionally be aligned to a given boundary without wasting the padding space.
99

10+
## `no_std` support
11+
12+
This crate is `#![no_std]` and depends on `alloc`.
13+
1014
## Example
1115

1216
```rust

src/lib.rs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
//! `Range<T>` values from a pool. It uses a best-fit strategy to reduce
55
//! fragmentation and automatically merges adjacent free ranges on deallocation.
66
//!
7+
//! The crate is `#![no_std]` and uses `alloc` for internal storage.
8+
//!
79
//! # Example
810
//!
911
//! ```
@@ -24,7 +26,12 @@
2426
//! The MSRV of this crate is at least 1.39. It will only be
2527
//! bumped in a breaking release.
2628
27-
use std::{
29+
#![no_std]
30+
extern crate alloc;
31+
32+
use alloc::vec;
33+
use alloc::vec::Vec;
34+
use core::{
2835
fmt::Debug,
2936
iter::Sum,
3037
ops::{Add, AddAssign, Range, Rem, Sub},
@@ -441,20 +448,20 @@ mod tests {
441448
let mut alloc = RangeAllocator::new(0..10);
442449
// Test if an allocation works
443450
assert_eq!(alloc.allocate_range(4), Ok(0..4));
444-
assert!(alloc.allocated_ranges().eq(std::iter::once(0..4)));
451+
assert!(alloc.allocated_ranges().eq(core::iter::once(0..4)));
445452
// Free the prior allocation
446453
alloc.free_range(0..4);
447454
// Make sure the free actually worked
448455
assert_eq!(alloc.free_ranges, vec![0..10]);
449-
assert!(alloc.allocated_ranges().eq(std::iter::empty()));
456+
assert!(alloc.allocated_ranges().eq(core::iter::empty()));
450457
}
451458

452459
#[test]
453460
fn test_out_of_space() {
454461
let mut alloc = RangeAllocator::new(0..10);
455462
// Test if the allocator runs out of space correctly
456463
assert_eq!(alloc.allocate_range(10), Ok(0..10));
457-
assert!(alloc.allocated_ranges().eq(std::iter::once(0..10)));
464+
assert!(alloc.allocated_ranges().eq(core::iter::once(0..10)));
458465
assert!(alloc.allocate_range(4).is_err());
459466
alloc.free_range(0..10);
460467
}
@@ -464,7 +471,7 @@ mod tests {
464471
let mut alloc = RangeAllocator::new(0..11);
465472
// Test if the allocator runs out of space correctly
466473
assert_eq!(alloc.allocate_range(10), Ok(0..10));
467-
assert!(alloc.allocated_ranges().eq(std::iter::once(0..10)));
474+
assert!(alloc.allocated_ranges().eq(core::iter::once(0..10)));
468475
assert!(alloc.allocate_range(4).is_err());
469476
alloc.grow_to(20);
470477
assert_eq!(alloc.allocate_range(4), Ok(10..14));
@@ -480,7 +487,7 @@ mod tests {
480487
alloc.free_range(0..3);
481488

482489
alloc.grow_to(9);
483-
assert_eq!(alloc.allocated_ranges().collect::<Vec<_>>(), [3..6]);
490+
assert!(alloc.allocated_ranges().eq(core::iter::once(3..6)));
484491
}
485492
#[test]
486493
fn test_grow_with_hole_in_middle() {
@@ -527,7 +534,7 @@ mod tests {
527534
assert_eq!(alloc.allocate_range(10), Ok(80..90));
528535
assert_eq!(alloc.allocate_range(10), Ok(90..100));
529536
assert_eq!(alloc.free_ranges, vec![]);
530-
assert!(alloc.allocated_ranges().eq(std::iter::once(0..100)));
537+
assert!(alloc.allocated_ranges().eq(core::iter::once(0..100)));
531538
alloc.free_range(10..20);
532539
alloc.free_range(30..40);
533540
alloc.free_range(50..60);
@@ -565,7 +572,7 @@ mod tests {
565572
assert_eq!(alloc.allocate_range(4), Ok(96..100));
566573
// Check that nothing is free.
567574
assert_eq!(alloc.free_ranges, vec![]);
568-
assert!(alloc.allocated_ranges().eq(std::iter::once(0..100)));
575+
assert!(alloc.allocated_ranges().eq(core::iter::once(0..100)));
569576
}
570577

571578
#[test]
@@ -597,7 +604,7 @@ mod tests {
597604
alloc.free_range(6..9);
598605
alloc.free_range(3..6);
599606
assert_eq!(alloc.free_ranges, vec![0..9]);
600-
assert!(alloc.allocated_ranges().eq(std::iter::empty()));
607+
assert!(alloc.allocated_ranges().eq(core::iter::empty()));
601608
}
602609

603610
#[test]

0 commit comments

Comments
 (0)