casper_types/account/
weight.rs1use alloc::vec::Vec;
2
3#[cfg(feature = "datasize")]
4use datasize::DataSize;
5use serde::{Deserialize, Serialize};
6
7use crate::{
8 bytesrepr::{self, Error, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
9 CLType, CLTyped,
10};
11
12pub const WEIGHT_SERIALIZED_LENGTH: usize = U8_SERIALIZED_LENGTH;
14
15#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Debug, Serialize, Deserialize)]
18#[cfg_attr(feature = "datasize", derive(DataSize))]
19pub struct Weight(u8);
20
21impl Weight {
22 pub const MAX: Weight = Weight(u8::MAX);
24
25 pub const fn new(weight: u8) -> Weight {
27 Weight(weight)
28 }
29
30 pub fn value(self) -> u8 {
32 self.0
33 }
34}
35
36impl ToBytes for Weight {
37 fn to_bytes(&self) -> Result<Vec<u8>, Error> {
38 self.0.to_bytes()
39 }
40
41 fn serialized_length(&self) -> usize {
42 WEIGHT_SERIALIZED_LENGTH
43 }
44
45 fn write_bytes(&self, writer: &mut Vec<u8>) -> Result<(), bytesrepr::Error> {
46 writer.push(self.0);
47 Ok(())
48 }
49}
50
51impl FromBytes for Weight {
52 fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), Error> {
53 let (byte, rem) = u8::from_bytes(bytes)?;
54 Ok((Weight::new(byte), rem))
55 }
56}
57
58impl CLTyped for Weight {
59 fn cl_type() -> CLType {
60 CLType::U8
61 }
62}