我正在探索"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);
}
错误消息
错误[E0599]:函数或关联项
try_from_slice
存在对于结构体SampleData<'_>
,但其特征边界不满足
-->src/main.rs:27:34|6| struct SampleData<'a>{|------------------|||找不到此|的函数或关联项try_from_slice
不满足SampleData<'_>: BorshDeserialize
。。。27|let _decoded_a=示例数据::try_from_slice(&_encoded_a(.unwrap((|
不能调用函数或关联项由于特征边界不满足而导致的SampleData<'_>
|注意:由于需求的原因,以下特征界限未得到满足关于CCD_ 6对CCD_ 7的实现:(&Pubkey, &Pubkey): BorshDeserialize
&Pubkey: BorshDeserialize
->src/main.rs:5:26|5|#[drive(BorshSerialize,BorshDeserialize,PartialEq,Debug(]|结构体SampleData<'a>{|
帮助:来自特征的项目只能在trait已实现并且在scope中=注意:以下trait定义了一个项try_from_slice
,也许您需要实现它:候选者#1:BorshDeserialize
=注意:该错误源于派生宏CCD_ 12(在Nightly构建中,使用-Z宏回溯运行以获取更多信息(有关此错误的详细信息,请尝试
rustc --explain E0599
。错误:由于以前的错误,无法编译seralize
问题是SampleData
包含引用,这在正常的Borsh反序列化中不受支持。Borsh基于切片创建您类型的新实例,但它将无法创建引用。
如果你想坚持Borsh,你应该把类型改为:
#[derive(BorshSerialize, BorshDeserialize, PartialEq, Debug)]
struct SampleData {
accounts: (Pubkey, Pubkey),
data: String,
timestamp: u64,
owner: Pubkey,
}