use crate::validation::Validate;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum AllocationSlot {
MemoryManagerId(u8),
NamedPartition(String),
Custom {
kind: String,
version: u32,
value: Vec<u8>,
},
}
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub struct AllocationSlotDescriptor {
pub(crate) slot: AllocationSlot,
pub(crate) substrate: String,
pub(crate) descriptor_version: u32,
}
impl AllocationSlotDescriptor {
#[must_use]
pub const fn slot(&self) -> &AllocationSlot {
&self.slot
}
#[must_use]
pub fn substrate(&self) -> &str {
&self.substrate
}
#[must_use]
pub const fn descriptor_version(&self) -> u32 {
self.descriptor_version
}
}
impl Validate for AllocationSlotDescriptor {
type Error = AllocationSlotDescriptorError;
fn validate(&self) -> Result<(), Self::Error> {
if matches!(self.slot, AllocationSlot::MemoryManagerId(_))
|| self.substrate == super::memory_manager::MEMORY_MANAGER_SUBSTRATE
{
self.memory_manager_id()
.map_err(AllocationSlotDescriptorError::MemoryManager)?;
}
Ok(())
}
}
#[derive(Clone, Debug, Eq, thiserror::Error, PartialEq)]
pub enum AllocationSlotDescriptorError {
#[error(transparent)]
MemoryManager(super::memory_manager::MemoryManagerSlotError),
}