Skip to content

Return the amount of bytes read from Memory::read #348

Closed
@ranile

Description

@ranile

Memory::read takes in an offset and buffer but does not return how much data has been written to the buffer.

Consider the following scenario:

let memory_manger = MemoryManager::init(DefaultMemoryImpl::default());
let mem = memory_manger.get(1);


let mut data = vec![];
let mut pos = 0;

loop {
    let mut buf = [0; 1028];
    mem.read(pos, &mut buf);
    pos = buf.len()
    vec.extend(buf);
    if ??? { break } // ??? here would be checking if buf.len() is more than amount of bytes returned, suggesting that the buffer has been memory has been all read and there's nothing more to read. If the number of bytes read is less than buf.len() than it should also tell that since bunch of 0s at the end of a buffer aren't helpful 
}

Use case

Serializing data to bytes to be stored in memory and deserializing those bytes later into a rust type. This is needed for data that can't be morphed into any of the Stable* structures.
While StableVec of u8 can get the job done, it requires all the data be read into memory and then deserialized, which is not ideal for large amount of data:

let memory_manger = MemoryManager::init(DefaultMemoryImpl::default());
let mem = memory_manger.get(1);
let stable_vec = StableVec::<u8, _>::init(mem).expect("memory was not for stable vec");
let bytes = stable_vec.iter().collect::<Vec<_>>();
// deserialize here

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions