pub struct FreeIntervals { /* private fields */ }
Expand description

Collection of free intervals. You can take parts of the free intervals and add new free intervals.

Implementations§

source§

impl FreeIntervals

source

pub fn new(free_interval: Interval) -> Self

Initialize collection with free interval.

Examples found in repository?
examples/take_insert.rs (line 6)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // new collection with free interval
    let free = (0..100).into();
    let mut collection = FreeIntervals::new(free);

    // fill subrange of free interval
    let first = collection.take_exact(32).unwrap();
    println!("first: {first}"); // output: first: [0, 32)

    // fill other subrange of free interval with alignment
    let aligned = collection.take_exact_aligned(32, 10).unwrap();
    println!("aligned: {aligned}"); // output: aligned: [40, 72)

    // no free subrange with `length == 40`.
    let none = collection.take_exact(40);
    assert!(none.is_none());

    // free `first` intervals with `length == 32`,
    // it connects with padding with `length == 8` from aligned interval.
    collection.insert(first);

    // now we have subrange with `length == 40`.
    let some = collection.take_exact(40).unwrap();
    println!("some: {some}"); // output: some: [0, 40)
}
source

pub fn take_enough(&mut self, length: u64) -> Option<Interval>

Take the minimal interval larger then length. If collection doesn’t contain such free interval, None will be returned.

source

pub fn take_enough_aligned( &mut self, length: u64, align: u64 ) -> Option<Interval>

Take the minimal interval larger then length. Add paddintg to the interval start, to align it. The align padding will be added to collection as a new free interval. If collection doesn’t contain such free interval, None will be returned.

source

pub fn take_exact(&mut self, length: u64) -> Option<Interval>

Take the minimal interval larger then length and split it into [length, extra] parts. Add extra part as new free interval. If collection doesn’t contain such free interval, None will be returned.

Examples found in repository?
examples/take_insert.rs (line 9)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // new collection with free interval
    let free = (0..100).into();
    let mut collection = FreeIntervals::new(free);

    // fill subrange of free interval
    let first = collection.take_exact(32).unwrap();
    println!("first: {first}"); // output: first: [0, 32)

    // fill other subrange of free interval with alignment
    let aligned = collection.take_exact_aligned(32, 10).unwrap();
    println!("aligned: {aligned}"); // output: aligned: [40, 72)

    // no free subrange with `length == 40`.
    let none = collection.take_exact(40);
    assert!(none.is_none());

    // free `first` intervals with `length == 32`,
    // it connects with padding with `length == 8` from aligned interval.
    collection.insert(first);

    // now we have subrange with `length == 40`.
    let some = collection.take_exact(40).unwrap();
    println!("some: {some}"); // output: some: [0, 40)
}
source

pub fn take_exact_aligned( &mut self, length: u64, align: u64 ) -> Option<Interval>

Take the minimal interval larger then length and split it into [length, extra] parts. Add extra part as new free interval. Add paddintg to the interval start, to align it. The align padding will be added to collection as a new free interval. If collection doesn’t contain such free interval, None will be returned.

Examples found in repository?
examples/take_insert.rs (line 13)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // new collection with free interval
    let free = (0..100).into();
    let mut collection = FreeIntervals::new(free);

    // fill subrange of free interval
    let first = collection.take_exact(32).unwrap();
    println!("first: {first}"); // output: first: [0, 32)

    // fill other subrange of free interval with alignment
    let aligned = collection.take_exact_aligned(32, 10).unwrap();
    println!("aligned: {aligned}"); // output: aligned: [40, 72)

    // no free subrange with `length == 40`.
    let none = collection.take_exact(40);
    assert!(none.is_none());

    // free `first` intervals with `length == 32`,
    // it connects with padding with `length == 8` from aligned interval.
    collection.insert(first);

    // now we have subrange with `length == 40`.
    let some = collection.take_exact(40).unwrap();
    println!("some: {some}"); // output: some: [0, 40)
}
source

pub fn insert(&mut self, interval: Interval)

Insert free interval to collection. Connects it with any near free interval in the collection.

Examples found in repository?
examples/take_insert.rs (line 22)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
fn main() {
    // new collection with free interval
    let free = (0..100).into();
    let mut collection = FreeIntervals::new(free);

    // fill subrange of free interval
    let first = collection.take_exact(32).unwrap();
    println!("first: {first}"); // output: first: [0, 32)

    // fill other subrange of free interval with alignment
    let aligned = collection.take_exact_aligned(32, 10).unwrap();
    println!("aligned: {aligned}"); // output: aligned: [40, 72)

    // no free subrange with `length == 40`.
    let none = collection.take_exact(40);
    assert!(none.is_none());

    // free `first` intervals with `length == 32`,
    // it connects with padding with `length == 8` from aligned interval.
    collection.insert(first);

    // now we have subrange with `length == 40`.
    let some = collection.take_exact(40).unwrap();
    println!("some: {some}"); // output: some: [0, 40)
}
source

pub fn clear(&mut self)

Restore interval storage to initial state.

source

pub fn iter(&self) -> impl Iterator<Item = &Interval>

Returns iterator over free intervals.

Trait Implementations§

source§

impl Debug for FreeIntervals

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for FreeIntervals

source§

fn default() -> FreeIntervals

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.