error[E0277]: 特征绑定 `NotesDs: Serialize` 不满足
error[E0277]: the trait bound `NotesDs: Serialize` is not satisfied
#[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
我尝试将序列化和反序列化添加到我的所有结构和枚举中。
主要问题出现在 return 值是 NotesDs 类型时,不是 return 类型的函数工作正常。
我什至尝试在单独的文件中初始化自定义结构并将其导入。但我仍然得到错误。
您使用 #[derive(BorshSerialize, BorshDeserialize)]
在 NotesDs
结构上实现了 Borsh 序列化,并且您正在 returning NotesDs
来自这个合约函数:
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}
问题是 near-sdk
默认为 JSON 序列化。要对 return 类型使用 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()
}
#[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
我尝试将序列化和反序列化添加到我的所有结构和枚举中。
主要问题出现在 return 值是 NotesDs 类型时,不是 return 类型的函数工作正常。
我什至尝试在单独的文件中初始化自定义结构并将其导入。但我仍然得到错误。
您使用 #[derive(BorshSerialize, BorshDeserialize)]
在 NotesDs
结构上实现了 Borsh 序列化,并且您正在 returning NotesDs
来自这个合约函数:
pub fn get_note_list(&mut self, account_id: AccountId) -> NotesDs {
self.note_list.get(&account_id).unwrap()
}
问题是 near-sdk
默认为 JSON 序列化。要对 return 类型使用 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()
}