Converting Rc<RefCell<&mut [u8]>> to simple &[u8]

I'm trying to get this working:

let raw_account_data = account.data.borrow_mut(); // account type is AccountInfo
let account_data  = AccountData::deserialize(raw_account_data); // <-- ERROR

////////////////////////////////////////////////////////////////

pub struct AccountInfo<'a> {
    pub data: Rc<RefCell<&'a mut [u8]>>,
////////////////////////////////////////////////////////////////

pub fn deserialize(buf: &mut &[u8]) -> Result<Self>
////////////////////////////////////////////////////////////////

I tried various combinations but none gave me expected &mut &[u8].
How to convert account.data?

mismatched types
expected mutable reference `&mut &[u8]`
              found struct `RefMut<'_, &mut [u8]>`

You want to get a shared borrow of the bytes within (&[u8]) and then pass a mutable reference to that into deserialize. It will use that like a cursor to parse the data (presumably). It won't be able to modify the bytes.

    let raw_account_data = account.data.borrow();
    let mut bytes = &**raw_account_data;
    let account_data  = AccountData::deserialize(&mut bytes);
1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.