2

The goal is to use to use the char type.

I couldn't find a way or ressource on how to solve to borsh serialization errors that get thrown when trying to store a char type in an #[account] struct field.

Other types or enums won't make problems.

A reproduction is as simple as taking any anchor project and defining one of the fileds of an account to be type of char. E.g. when using one of the example projects from the anchor repo and adding a char field it will produce an error:

#[account]
pub struct Counter {
    pub authority: Pubkey,
    pub count: u64,
    pub something: char, // <- will produce error
}

To take an example from one of the projects I'm working on: char field enter image description here

Error msg:

rustc: the trait bound `char: BorshSerialize` is not satisfied
the following other types implement trait `BorshSerialize`:
  &T
  ()
  (T0, T1)
  (T0, T1, T2)
  (T0, T1, T2, T3)
  (T0, T1, T2, T3, T4)
  (T0, T1, T2, T3, T4, T5)
  (T0, T1, T2, T3, T4, T5, T6)
and 185 others
see issue #48214

Also trying to use a char as a function argument throws errors for the #[program] macro.

Project in screenshots: https://github.com/tobealive/anchor-solana-twitter/

5
  • provide the nonworking code and any error message(s) you're receiving
    – trent.sol
    Commented Aug 17, 2022 at 6:20
  • Added an example, screenshots and an error message.
    – tenxsoydev
    Commented Sep 8, 2022 at 14:57
  • Why not use an u8 instead of a char? its the same Commented Sep 8, 2022 at 14:59
  • How is u8 and char the same? Lets say the char should represent an utf8 character like an emoji. The char type is capabale of that. u8 on the other is only for 8bit integers or am I missunderstanding something?
    – tenxsoydev
    Commented Sep 8, 2022 at 15:04
  • 1
    I assumed you want to store an ASCII Commented Sep 8, 2022 at 15:15

1 Answer 1

1

A char in rust is not considered serializable by the Borsh spec which anchor uses. This is because a char is really an array of 4 bytes with many invalid values.

Depending on your use case you can use a string that you then take the first character of only or store a 4 byte array that you utf8 encode into and out of. It's very likely though that you really need a char stored. If you elaborate on the use case there may be a better solution.

A more extreme option would be to extend borsh yourself by storing a wrapped value and implementing BorshSerialize for that. You will need to manually deserialize it on the client side and anchor doesn't usually like custom types.

1
  • Thanks for shedding some light onto this. The goal of the question turned to understand why it is not possible to use the char type in general and if there is a way to solve it. For the specific char-use-case in the app: Just a predefined set of chars had to be stored. A good solution was to use an enum instead of a char type. The enum variants describe the allowed characters, and a validation function matches / evaluates them to the corresponding characters. It's the 'reaction' part of the app.
    – tenxsoydev
    Commented Sep 10, 2022 at 15:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.