pabigot/buffer-layout

Strange results deserializing an array of structs

Opened this issue · 1 comments

Hello I have an array of structs coming from a Rust app that I am trying to deserialize using buffer-layout. It sort of works but the values are coming in a strange sort of offset. Basically my struct has two fields: archive_id and created_on but the created_on is being filled into the next item in the array instead of together with the matching archive_id.

// this is the rust struct that is coming into my client js code
#[derive(BorshSerialize, BorshDeserialize, Debug)] pub struct ChatMessage { pub archive_id: String, pub created_on: String }

// this is the deserialization code in javascript
const archive_id = lo.cstr("archive_id"); const created_on = lo.cstr("created_on"); const dataStruct = lo.struct([archive_id, created_on], "ChatMessage"); const ds = lo.seq(dataStruct, CHAT_MESSAGE_ELEMENTS_COUNT); const messages = ds.decode(sentAccount.data);

// this is the how the results come in. note on rust side the logs look normal
image

Ok guys I'm almost there. I added some padding to the end after each field and now things align. But for some reason array elements get pushed up one.
Also what's up with the single space and plus sign on the last character of the strings?

// new deserialization code. note I'm using u8 because that is the format used by the rust program
const archive_id = lo.cstr("archive_id"); const created_on = lo.cstr("created_on"); const dataStruct = lo.struct( [archive_id, lo.seq(lo.u8(), 3), created_on, lo.seq(lo.u8(), 3)], "ChatMessage" ); const ds = lo.seq(dataStruct, CHAT_MESSAGE_ELEMENTS_COUNT); const messages = ds.decode(sentAccount.data);

image