When trying to build by Solana Program, I'm getting this error. Can anybody please tell me how can I serialize String, As I'm using String in my struct. OR instead of String what I should I use if serializing string isn't possible in Solana program?
Struct: Box
#[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
pub struct Box {
pub token_uris: [String; 3],
pub count: i32,
pub title: String,
pub description: String,
pub image: String,
pub price: i32,
pub usd_price: i32,
pub count_nft: i32,
pub is_opened: bool,
}
Error Log:
15 | #[derive(Clone, Debug, BorshSerialize, BorshDeserialize, PartialEq)]
| ^^^^^^^^^^^^^^^^ the trait `std::marker::Copy` is not implemented for `std::string::String`
|
= note: required because of the requirements on the impl of `AnchorDeserialize` for `[std::string::String; 3]`
= help: see issue #48214
= note: this error originates in the derive macro `BorshDeserialize` (in Nightly builds, run with -Z macro-backtrace for more info)
CodePudding user response:
Can anybody please tell me how can I serialize String, As I'm using String in my struct.
The problem is not that you're using a string, or a string in a struct, it's that you're using a string in a fixed-size array, hence the compiler pointing to that specifically:
for `[std::string::String; 3]`
not
for `String`
And if you go read the documentation for BorshDeserialize
you can find:
impl<T> BorshDeserialize for [T; 3] where
T: BorshDeserialize Default Copy,
So BorshDeserialize
is only implemented for arrays Default Copy
types. Since a String
is not Copy
the automatic derivation can not work.
The fixes are to either:
- not use a fixed-size array
- or implement
BorshDeserialize
by hand on your structure instead of trying toderive()
it