casper_types/addressable_entity/
weight.rs

1use alloc::vec::Vec;
2
3#[cfg(feature = "datasize")]
4use datasize::DataSize;
5#[cfg(feature = "json-schema")]
6use schemars::JsonSchema;
7
8use serde::{Deserialize, Serialize};
9
10use crate::{
11    bytesrepr::{self, Error, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
12    CLType, CLTyped,
13};
14
15/// The number of bytes in a serialized [`Weight`].
16pub const WEIGHT_SERIALIZED_LENGTH: usize = U8_SERIALIZED_LENGTH;
17
18/// The weight associated with public keys in an account's associated keys.
19#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
20#[cfg_attr(feature = "datasize", derive(DataSize))]
21#[cfg_attr(feature = "json-schema", derive(JsonSchema))]
22#[cfg_attr(
23    feature = "json-schema",
24    schemars(rename = "EntityAssociatedKeyWeight")
25)]
26pub struct Weight(u8);
27
28impl Weight {
29    /// Constructs a new `Weight`.
30    pub const fn new(weight: u8) -> Weight {
31        Weight(weight)
32    }
33
34    /// Returns the value of `self` as a `u8`.
35    pub fn value(self) -> u8 {
36        self.0
37    }
38}
39
40impl ToBytes for Weight {
41    fn to_bytes(&self) -> Result<Vec<u8>, Error> {
42        self.0.to_bytes()
43    }
44
45    fn serialized_length(&self) -> usize {
46        WEIGHT_SERIALIZED_LENGTH
47    }
48
49    fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
50        writer.push(self.0);
51        Ok(())
52    }
53}
54
55impl FromBytes for Weight {
56    fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
57        let (byte, rem) = u8::from_bytes(bytes)?;
58        Ok((Weight::new(byte), rem))
59    }
60}
61
62impl CLTyped for Weight {
63    fn cl_type() -> CLType {
64        CLType::U8
65    }
66}