Class: Wasmtime::Memory::UnsafeSlice
- Inherits:
-
Object
- Object
- Wasmtime::Memory::UnsafeSlice
- Defined in:
- ext/src/ruby_api/memory/unsafe_slice.rs
Overview
Represents a slice of a WebAssembly memory. This is useful for creating Ruby strings from WASM memory without any extra memory allocations.
The returned UnsafeSlice lazily reads the underlying memory, meaning that the actual pointer to the string buffer is not materialzed until #to_str is called.
Instance Method Summary collapse
-
#to_memory_view ⇒ Fiddle::MemoryView
Get this slice as a Fiddle memory view, which can be cheaply read by other Ruby extensions.
-
#to_str ⇒ String
Gets the memory slice as a Ruby string without copying the underlying buffer.
Instance Method Details
#to_memory_view ⇒ Fiddle::MemoryView
Get this slice as a Fiddle memory view, which can be cheaply read by other Ruby extensions.
70 71 72 73 74 |
# File 'ext/src/ruby_api/memory/unsafe_slice.rs', line 70
pub fn to_memory_view(rb_self: Obj<Self>) -> Result<Value, Error> {
static CLASS: Lazy<RClass> = Lazy::new(|_| fiddle_memory_view_class().unwrap());
let ruby = Ruby::get().unwrap();
ruby.get_inner(&CLASS).new_instance((rb_self,))
}
|
#to_str ⇒ String
Gets the memory slice as a Ruby string without copying the underlying buffer.
81 82 83 84 85 86 87 88 89 90 91 |
# File 'ext/src/ruby_api/memory/unsafe_slice.rs', line 81
pub fn to_str(rb_self: Obj<Self>) -> Result<Value, Error> {
let raw_slice = rb_self.get_raw_slice()?;
let id = IVAR_NAME.into_id();
let rstring = unsafe {
let val = rb_str_new_static(raw_slice.as_ptr() as _, raw_slice.len() as _);
rb_ivar_set(val, id.as_raw(), rb_self.as_raw());
rb_obj_freeze(val)
};
Ok(unsafe { Value::from_raw(rstring) })
}
|