[英]Deserialize array with Borsh in Rust?
I'm trying to deserialize an array:我正在尝试反序列化一个数组:
pixels: [(Pubkey, u8); 1_000 * 1_000],
I've added the following crate attribute:我添加了以下 crate 属性:
#![feature(trivial_bounds)]
Here's the compile error:这是编译错误:
|
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`
Is it possible to deserialize an array with Borsh?是否可以使用 Borsh 反序列化数组?
On older versions of Rust BorshSerialize
/ BorshDeserialize
are implemented for a variety of array sizes, but not all (this would require const generics).在旧版本的 Rust 上,
BorshSerialize
/ BorshDeserialize
实现了各种数组大小,但不是全部(这需要 const 泛型)。 This has since been stabilized, but I guess they didn't want to bump the minimum requires Rust version.这已经稳定了,但我猜他们不想碰到最低要求 Rust 版本。
If you specify the const-generics
feature in your Cargo.toml this should work though:如果您在 Cargo.toml 中指定
const-generics
功能,这应该可以工作:
borsh = { version = "0.9", features = ["const-generics"] }
That said, having a very large array like that will generally blow up in your face through stack overflows, so I would recommend turning it into a Vec<(PubKey, u8)>
instead.也就是说,拥有像这样的非常大的数组通常会因堆栈溢出而炸毁你的脸,所以我建议将它改为
Vec<(PubKey, u8)>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.