Struct wasmedge_sys::Memory

source ·
pub struct Memory { /* private fields */ }
Expand description

Defines a WebAssembly memory instance, which is a linear memory described by its type. Each memory instance consists of a vector of bytes and an optional maximum size, and its size is a multiple of the WebAssembly page size (64KiB of each page).

Implementations§

Create a new Memory to be associated with the given capacity limit.

Arguments
  • ty - The type of the new Memory instance.
Errors
Example
use wasmedge_sys::{MemType, Memory};

let ty = MemType::create(10, Some(20), false).expect("fail to create memory type");

let memory = Memory::create(&ty);

Returns the type of the Memory.

Errors

If fail to get the type from the Memory, then an error is returned.

Copies the data from the Memory to the output buffer.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length.

Errors

If the offset + len is larger than the data size in the Memory, then an error is returned.

Copies the data from the given input buffer into the Memory.

Arguments
  • data - The data buffer to copy.

  • offset - The data start offset in the Memory.

Errors

If the sum of the offset and the data length is larger than the size of the Memory, then an error is returned.

use wasmedge_sys::{Memory, MemType};
use wasmedge_types::error::{CoreError, CoreExecutionError, WasmEdgeError};

// create a Memory: the min size 1 and the max size 2
let ty = MemType::create(1, Some(2), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");

// set data and the data length is larger than the data size in the memory
let result = mem.set_data(vec![1; 10], u32::pow(2, 16) - 9);
assert!(result.is_err());
assert_eq!(result.unwrap_err(), Box::new(WasmEdgeError::Core(CoreError::Execution(CoreExecutionError::MemoryOutOfBounds))));
Example
use wasmedge_sys::{MemType, Memory};

// create a Memory: the min size 1 and the max size 2
let ty = MemType::create(1, Some(2), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");
// page count
let count = mem.size();
assert_eq!(count, 1);

// set data
mem.set_data(vec![1; 10], 10).expect("fail to set data");

// get data
let data = mem.get_data(10, 10).expect("fail to get data");
assert_eq!(data, vec![1; 10]);

Returns the const data pointer to the Memory.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length. If the size of offset + len is larger than the data size in the Memory

Errors

If fail to get the data pointer, then an error is returned.

Returns the data pointer to the Memory.

Arguments
  • offset - The data start offset in the Memory.

  • len - The requested data length. If the size of offset + len is larger than the data size in the Memory

Errors

If fail to get the data pointer, then an error is returned.

Returns the size, in WebAssembly pages (64 KiB of each page), of this wasm memory.

Grows this WebAssembly memory by count pages.

Arguments
  • count - The page counts to be extended to the Memory.
Errors

If fail to grow the page count, then an error is returned.

Example
use wasmedge_sys::{MemType, Memory};

// create a Memory with a limit range [10, 20]
let ty = MemType::create(10, Some(20), false).expect("fail to create a memory type");
let mut mem = Memory::create(&ty).expect("fail to create a Memory");
// check page count
let count = mem.size();
assert_eq!(count, 10);

// grow 5 pages
mem.grow(10).expect("fail to grow the page count");
assert_eq!(mem.size(), 20);

Provides a raw pointer to the inner memory context.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Executes the destructor for this type. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.