use alloc::{collections::TryReserveError, vec::Vec};
use core::num::TryFromIntError;
use super::StableSlots;
#[derive(Debug)]
pub enum StableSlotsCreationError {
CapacityUnrepresentable {
maximum_rows: usize,
source: TryFromIntError,
},
RowStorageUnavailable {
source: TryReserveError,
},
SlotStorageUnavailable {
source: TryReserveError,
},
}
impl StableSlots {
pub fn try_new(maximum_rows: usize) -> Result<Self, StableSlotsCreationError> {
u32::try_from(maximum_rows).map_err(|source| {
StableSlotsCreationError::CapacityUnrepresentable {
maximum_rows,
source,
}
})?;
let mut row_to_slot = Vec::new();
row_to_slot
.try_reserve_exact(maximum_rows)
.map_err(|source| StableSlotsCreationError::RowStorageUnavailable { source })?;
let mut slot_locations = Vec::new();
slot_locations
.try_reserve_exact(maximum_rows)
.map_err(|source| StableSlotsCreationError::SlotStorageUnavailable { source })?;
Ok(Self {
maximum_rows,
row_to_slot,
slot_locations,
first_free: None,
})
}
}