BorshDeserialize 给出 try_from_slice 错误
BorshDeserialize gives try_from_slice error
我正在研究 Rust 中的 "BorshDeserialize" 选项。并在使用特征边界时注意到以下问题。感谢任何帮助
示例代码
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;
use std::mem;
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct SampleData<'a> {
accounts: (&'a Pubkey, &'a Pubkey),
data: String,
timestamp: u64,
owner: &'a Pubkey,
}
fn main() {
println!(
"Size of SampleData is {} bytes",
mem::size_of::<SampleData>()
);
let _pub_key = Pubkey::default();
let _a = SampleData {
accounts: (&_pub_key, &_pub_key),
data: "ABCDE".to_string(),
timestamp: 1643116047,
owner: &_pub_key,
};
let _encoded_a = _a.try_to_vec().unwrap();
println!("{:?}", _encoded_a);
let _decoded_a = SampleData::try_from_slice(&_encoded_a).unwrap();
println!("decoded_a: {:?}", _decoded_a);
}
错误信息
error[E0599]: the function or associated item try_from_slice
exists
for struct SampleData<'_>
, but its trait bounds were not satisfied
--> src/main.rs:27:34 | 6 | struct SampleData<'a> { | --------------------- | | | function or associated item try_from_slice
not found for this | doesn't satisfy
SampleData<'_>: BorshDeserialize
... 27 | let _decoded_a =
SampleData::try_from_slice(&_encoded_a).unwrap(); |
^^^^^^^^^^^^^^ function or associated item cannot be called on
SampleData<'_>
due to unsatisfied trait bounds | note: the
following trait bounds were not satisfied because of the requirements
of the implementation of BorshDeserialize
for _
:
(&Pubkey, &Pubkey): BorshDeserialize
&Pubkey: BorshDeserialize
--> src/main.rs:5:26 | 5 | #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] | ^^^^^^^^^^^^^^^^ 6 | struct SampleData<'a> { |
^^^^^^^^^^^^^^ = help: items from traits can only be used if the
trait is implemented and in scope = note: the following trait
defines an item try_from_slice
, perhaps you need to implement it:
candidate #1: BorshDeserialize
= note: this error originates in the derive macro BorshDeserialize
(in Nightly builds,
run with -Z macro-backtrace for more info)
For more information about this error, try rustc --explain E0599
.
error: could not compile seralize
due to previous error
问题是 SampleData
包含引用,这在正常的 Borsh 反序列化中不受支持。 Borsh 基于切片创建您的类型的新实例,但它无法创建引用。
如果你想坚持使用 Borsh,你应该将类型更改为:
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct SampleData {
accounts: (Pubkey, Pubkey),
data: String,
timestamp: u64,
owner: Pubkey,
}
我正在研究 Rust 中的 "BorshDeserialize" 选项。并在使用特征边界时注意到以下问题。感谢任何帮助
示例代码
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::pubkey::Pubkey;
use std::mem;
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct SampleData<'a> {
accounts: (&'a Pubkey, &'a Pubkey),
data: String,
timestamp: u64,
owner: &'a Pubkey,
}
fn main() {
println!(
"Size of SampleData is {} bytes",
mem::size_of::<SampleData>()
);
let _pub_key = Pubkey::default();
let _a = SampleData {
accounts: (&_pub_key, &_pub_key),
data: "ABCDE".to_string(),
timestamp: 1643116047,
owner: &_pub_key,
};
let _encoded_a = _a.try_to_vec().unwrap();
println!("{:?}", _encoded_a);
let _decoded_a = SampleData::try_from_slice(&_encoded_a).unwrap();
println!("decoded_a: {:?}", _decoded_a);
}
错误信息
error[E0599]: the function or associated item
try_from_slice
exists for structSampleData<'_>
, but its trait bounds were not satisfied
--> src/main.rs:27:34 | 6 | struct SampleData<'a> { | --------------------- | | | function or associated itemtry_from_slice
not found for this | doesn't satisfySampleData<'_>: BorshDeserialize
... 27 | let _decoded_a = SampleData::try_from_slice(&_encoded_a).unwrap(); |
^^^^^^^^^^^^^^ function or associated item cannot be called onSampleData<'_>
due to unsatisfied trait bounds | note: the following trait bounds were not satisfied because of the requirements of the implementation ofBorshDeserialize
for_
:(&Pubkey, &Pubkey): BorshDeserialize
&Pubkey: BorshDeserialize
--> src/main.rs:5:26 | 5 | #[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)] | ^^^^^^^^^^^^^^^^ 6 | struct SampleData<'a> { |
^^^^^^^^^^^^^^ = help: items from traits can only be used if the trait is implemented and in scope = note: the following trait defines an itemtry_from_slice
, perhaps you need to implement it: candidate #1:BorshDeserialize
= note: this error originates in the derive macroBorshDeserialize
(in Nightly builds, run with -Z macro-backtrace for more info)For more information about this error, try
rustc --explain E0599
. error: could not compileseralize
due to previous error
问题是 SampleData
包含引用,这在正常的 Borsh 反序列化中不受支持。 Borsh 基于切片创建您的类型的新实例,但它无法创建引用。
如果你想坚持使用 Borsh,你应该将类型更改为:
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct SampleData {
accounts: (Pubkey, Pubkey),
data: String,
timestamp: u64,
owner: Pubkey,
}