0

尝试通过 Solana 程序构建时,出现此错误。谁能告诉我如何序列化字符串,因为我在我的结构中使用字符串。或者,如果在 Solana 程序中无法序列化字符串,我应该使用什么来代替 String?

结构:盒子

#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
pub struct Box {
    pub token_uris: [String; 3], 
    pub count: i32,
    pub title: String,
    pub description: String,
    pub image: String,
    pub price: i32,
    pub usd_price: i32,
    pub count_nft: i32,
    pub is_opened: bool,
}

错误日志:

15 | #[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
   |                                        ^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
   |
   = note: required because of the requirements on the impl of `AnchorDeserialize` for `[std::string::String; 3]`
   = help: see issue #48214
   = note: this error originates in the derive macro `BorshDeserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
4

1 回答 1

0

谁能告诉我如何序列化字符串,因为我在我的结构中使用字符串。

问题不在于您使用的是字符串或结构中的字符串,而是您使用的是固定大小数组中的字符串,因此编译器专门指向它:

for `[std::string::String; 3]`

不是

for `String`

如果你去阅读文档,BorshDeserialize你会发现:

impl<T> BorshDeserialize for [T; 3] where
    T: BorshDeserialize + Default + Copy, 

所以BorshDeserialize仅针对数组Default + Copy类型实现。由于aString不是Copy自动推导不能工作。

修复是:

  • 不使用固定大小的数组
  • 或在您的结构上手动实施而不是BorshDeserialize尝试derive()
于 2021-09-22T10:25:08.273 回答