pub enum ExpansionMode {
Panic,
Ignore,
Overwrite,
Expand(f64),
}
Expand description
Different modes of action when the FixedSizeCollection
is full. Depending on the mode, the
collection will behave differently through the check_expansion
method.
There are four modes of expansion:
Panic
- The collection will panic when the capacity is reached.Ignore
- The collection will ignore the addition of the new item.Overwrite
- The collection will overwrite the an item when the capacity is reached.Expand
- The collection will expand the capacity by a specific factor. The factor must be greater than1.0
. If the factor is1.0
, the function willpanic!
. This is the default mode with a factor of2.0
.
Examples
Example on how the expansion is checked in FixedSizeCollection
through the
check_expansion
method:
use trait_based_collection::prelude::*;
fn check_expansion<'a, T, C: FixedSizeCollection<'a, T>>(mut collection: C) -> bool {
if collection.is_full() {
match collection.mode() {
ExpansionMode::Panic => {
panic!("The collection is full");
}
ExpansionMode::Ignore => {
return true;
}
ExpansionMode::Overwrite => {
collection.remove();
}
ExpansionMode::Expand(factor) => {
if *factor < 1.0 {
panic!("Expand factor must be greater than 1");
}
let size = ((*factor - 1.0) * collection.capacity() as f64).floor() as usize;
collection.expand(size);
}
}
}
false
}
Variants
Panic
The collection will panic when the capacity is reached.
Ignore
The collection will ignore the addition of the new item.
Overwrite
The collection will overwrite the an item when the capacity is reached. This means that
before the new item is added, an item is removed by calling the remove
method.
Expand(f64)
The collection will expand the capacity by a specific factor. The factor must be greater
than 1.0
. If the factor is 1.0
, the function will panic!
. This is the default mode
with a factor of 2.0
.