Skip to main content

miden_protocol/asset/
asset_value.rs

1use core::fmt::Display;
2
3use miden_crypto_derive::WordWrapper;
4
5use crate::Word;
6use crate::utils::serde::{
7    ByteReader,
8    ByteWriter,
9    Deserializable,
10    DeserializationError,
11    Serializable,
12};
13
14// ASSET VALUE
15// ================================================================================================
16
17/// The value of an [`Asset`](crate::asset::Asset).
18///
19/// See its docs for details.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, WordWrapper)]
21pub struct AssetValue(Word);
22
23impl AssetValue {
24    /// The serialized size of an asset value in bytes.
25    pub const SERIALIZED_SIZE: usize = Word::SERIALIZED_SIZE;
26}
27
28impl From<AssetValue> for Word {
29    fn from(value: AssetValue) -> Self {
30        value.0
31    }
32}
33
34impl Display for AssetValue {
35    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36        write!(f, "{}", self.as_word())
37    }
38}
39
40// SERIALIZATION
41// ================================================================================================
42
43impl Serializable for AssetValue {
44    fn write_into<W: ByteWriter>(&self, target: &mut W) {
45        target.write_many(self.as_word());
46    }
47
48    fn get_size_hint(&self) -> usize {
49        Self::SERIALIZED_SIZE
50    }
51}
52
53impl Deserializable for AssetValue {
54    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
55        Ok(AssetValue::from_raw(source.read()?))
56    }
57}