错误[E0277]:不满足特征绑定"NotesDs:Serialize"


#[derive(BorshSerialize, BorshDeserialize)]
pub struct NotesDs {
pub own: Vec<String>,
pub shared: UnorderedMap<AccountId,Vec<String>>,
}
impl NotesDs{
pub fn new() -> Self {
assert!(env::state_read::<Self>().is_none(), "Already initialized");
Self {
own: Vec:: new(),
shared: UnorderedMap::new(b"w".to_vec()),
}
}
}
#[near_bindgen]
#[derive(BorshDeserialize, BorshSerialize)]
pub struct Note {
pub note_list : UnorderedMap<AccountId,NotesDs>, 
}
impl Default for Note {
fn default() -> Self {
// Check incase the contract is not initialized
env::panic(b"The contract is not initialized.")
}
}
#[near_bindgen]
impl Note {
/// Init attribute used for instantiation.
#[init]
pub fn new() -> Self {
assert!(env::state_read::<Self>().is_none(), "Already initialized");
Self {
note_list: UnorderedMap::new(b"h".to_vec()),
}
}
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}
}

问题定义

这会导致错误
error[E0277]: the trait bound NotesDs: Serialize is not satisfied
#[near_bindgen] - the trait Serialize is not implemented for 'NotesDs'
note: required because of the requirements on the impl of Serialize for Vec<NotesDs> note: required by a bound in to_vec



Cargo.toml

[package]
name = "test101"
version = "0.1.0"
edition = "2021"
[lib]
crate-type = ["cdylib", "rlib"]
[dependencies]
near-sdk = "3.1.0"
serde_json = "1.0.2"
[profile.release]
codegen-units = 1
opt-level = "z"         
lto = true              
debug = false          
panic = "abort"         
overflow-checks = true  

我尝试将Serialize和Deserialize添加到我所有的结构和枚举中。

当返回值的类型为NotesDs时,主要问题就来了,而不是返回类型的函数运行良好。

我甚至尝试在一个单独的文件中初始化自定义定义的结构并导入它。但我仍然收到错误。

您使用#[derive(BorshSerialize, BorshDeserialize)]NotesDs结构上实现了Borsh序列化,并且您将从以下契约函数返回NotesDs

pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}

问题是near-sdk默认为JSON序列化。要对返回类型使用Borsh序列化,请使用#[result_serializer(borsh)]:对函数进行注释

#[result_serializer(borsh)]
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}

请参阅有关序列化协议的文档。

相关内容

  • 没有找到相关文章

最新更新