use crate::Word;
use crate::account::storage::slot::StorageSlotId;
use crate::account::{StorageMap, StorageSlotContent, StorageSlotName, StorageSlotType};
use crate::utils::serde::{
ByteReader,
ByteWriter,
Deserializable,
DeserializationError,
Serializable,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct StorageSlot {
name: StorageSlotName,
content: StorageSlotContent,
}
impl StorageSlot {
pub const NUM_ELEMENTS: usize = 8;
pub fn new(name: StorageSlotName, content: StorageSlotContent) -> Self {
Self { name, content }
}
pub fn with_value(name: StorageSlotName, value: Word) -> Self {
Self::new(name, StorageSlotContent::Value(value))
}
pub fn with_empty_value(name: StorageSlotName) -> Self {
Self::new(name, StorageSlotContent::empty_value())
}
pub fn with_map(name: StorageSlotName, map: StorageMap) -> Self {
Self::new(name, StorageSlotContent::Map(map))
}
pub fn with_empty_map(name: StorageSlotName) -> Self {
Self::new(name, StorageSlotContent::empty_map())
}
pub fn name(&self) -> &StorageSlotName {
&self.name
}
pub fn id(&self) -> StorageSlotId {
self.name.id()
}
pub fn value(&self) -> Word {
self.content().value()
}
pub fn content(&self) -> &StorageSlotContent {
&self.content
}
pub fn slot_type(&self) -> StorageSlotType {
self.content.slot_type()
}
pub fn content_mut(&mut self) -> &mut StorageSlotContent {
&mut self.content
}
pub fn into_parts(self) -> (StorageSlotName, StorageSlotContent) {
(self.name, self.content)
}
}
impl Serializable for StorageSlot {
fn write_into<W: ByteWriter>(&self, target: &mut W) {
target.write(&self.name);
target.write(&self.content);
}
fn get_size_hint(&self) -> usize {
self.name.get_size_hint() + self.content().get_size_hint()
}
}
impl Deserializable for StorageSlot {
fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
let name: StorageSlotName = source.read()?;
let content: StorageSlotContent = source.read()?;
Ok(Self::new(name, content))
}
}