簡體   English   中英

在 Rust 中使用 Borsh 反序列化數組?

[英]Deserialize array with Borsh in Rust?

我正在嘗試反序列化一個數組:

pixels: [(Pubkey, u8); 1_000 * 1_000],

我添加了以下 crate 屬性:

#![feature(trivial_bounds)]

這是編譯錯誤:

   |
76 | #[account]
   | ^^^^^^^^^^ the trait `BorshSerialize` is not implemented for `[(anchor_lang::prelude::Pubkey, u8); 1000000]`
   | 
  ::: /home/vedantroy/.cargo/registry/src/github.com-1ecc6299db9ec823/borsh-0.9.1/src/ser/mod.rs:44:18
   |
44 |     fn serialize<W: Write>(&self, writer: &mut W) -> Result<()>;
   |                  - required by this bound in `anchor_lang::AnchorSerialize::serialize`
   |
   = help: the following implementations were found:
             <[T; 0] as BorshSerialize>
             <[T; 1024] as BorshSerialize>
             <[T; 10] as BorshSerialize>
             <[T; 11] as BorshSerialize>
           and 37 others
   = note: required because of the requirements on the impl of `BorshSerialize` for `GameState`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0277]: the trait bound `[(anchor_lang::prelude::Pubkey, u8); 1000000]: BorshDeserialize` is not satisfied
  --> programs/auction/src/lib.rs:76:1
   |
76 | #[account]
   | ^^^^^^^^^^ the trait `BorshDeserialize` is not implemented for `[(anchor_lang::prelude::Pubkey, u8); 1000000]`
   |
   = help: the following implementations were found:
             <[T; 0] as BorshDeserialize>
             <[T; 1024] as BorshDeserialize>
             <[T; 10] as BorshDeserialize>
             <[T; 11] as BorshDeserialize>
           and 36 others
   = note: required because of the requirements on the impl of `BorshDeserialize` for `GameState`
   = note: required by `anchor_lang::AnchorDeserialize::deserialize`
   = note: this error originates in an attribute macro (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: could not compile `auction`

是否可以使用 Borsh 反序列化數組?

在舊版本的 Rust 上, BorshSerialize / BorshDeserialize實現了各種數組大小,但不是全部(這需要 const 泛型)。 這已經穩定了,但我猜他們不想碰到最低要求 Rust 版本。

如果您在 Cargo.toml 中指定const-generics功能,這應該可以工作:

borsh = { version = "0.9", features = ["const-generics"] }

也就是說,擁有像這樣的非常大的數組通常會因堆棧溢出而炸毀你的臉,所以我建議將它改為Vec<(PubKey, u8)>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM