#casper #derive #boilerplate #contracts #type #byte #cl-typed

casper_types_derive

在Casper合约中无需样板代码使用结构体类型

1个不稳定版本

0.1.0 2021年4月13日

#20#casper

Download history 68/week @ 2024-03-15 79/week @ 2024-03-22 124/week @ 2024-03-29 74/week @ 2024-04-05 73/week @ 2024-04-12 74/week @ 2024-04-19 82/week @ 2024-04-26 67/week @ 2024-05-03 74/week @ 2024-05-10 72/week @ 2024-05-17 65/week @ 2024-05-24 51/week @ 2024-05-31 40/week @ 2024-06-07 64/week @ 2024-06-14 65/week @ 2024-06-21 34/week @ 2024-06-28

每月209次下载
用于 18 个crate(5个直接使用)

MIT/Apache

7KB
100

此crate包含三个用于derive宏,用于casper_types::CLTypedcasper_types::bytesrepr::FromBytescasper_types::bytesrepr::ToBytes

您可能需要为要在Casper存储中存储的结构体实现这三个特质。请参阅存储API文档

在类似此的结构体上声明宏

use casper_types_derive::{CLTyped, ToBytes, FromBytes};

#[derive(CLTyped, ToBytes, FromBytes)]
struct Dog {
  name: String,
  likes_treat: BTreeMap<String, bool>,
}

将展开为以下内容

impl CLTypes for Dog {
  fn cl_type(&self) -> CLType {
    CLType::Any
  }
}

impl FromBytes for Dog {
  fn from_bytes(bytes: &[u8]) -> Result<(Self, &[u8]), casper_types::bytesrepr::Error> {
    let (name, bytes) = FromBytes::from_bytes(bytes)?;
    let (likes_treat, bytes) = FromBytes::from_bytes(bytes)?;
    let value = Dog {
      name,
      likes_treat,
    };
    Ok((value, bytes))
  }
}

impl ToBytes for Dog {
  fn serialized_length(&self) -> usize {
    let mut size = 0;
    size += name.serialized_length();
    size += likes_treat.serialized_length();
    return size;
  }

  fn to_bytes(&self) -> Result<Vec<u8>, casper_types::bytesrepr::Error> {
    let mut vec = Vec::with_capacity(self.serialized_length());
    vec.append(self.name.to_bytes()?);
    vec.append(self.likes_treat.to_bytes()?);
    Ok(vec)
  }
}

依赖项

~1.5MB
~35K SLoC