Skip to content

Commit baf8217

Browse files
committed
Ref type
1 parent 3e14ec8 commit baf8217

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

bindings/rust/src/lib.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,14 @@ pub fn parse<T: AsRef<[u8]>>(input: &T) -> Result<Module, ()> {
8888
}
8989

9090
/// An instance of a module.
91-
pub struct Instance(NonNull<sys::FizzyInstance>);
91+
pub struct Instance {
92+
instance: NonNull<sys::FizzyInstance>,
93+
module: Module,
94+
}
9295

9396
impl Drop for Instance {
9497
fn drop(&mut self) {
95-
unsafe { sys::fizzy_free_instance(self.0.as_ptr()) }
98+
unsafe { sys::fizzy_free_instance(self.instance.as_ptr()) }
9699
}
97100
}
98101

@@ -120,8 +123,13 @@ impl Module {
120123
if ptr.is_null() {
121124
return Err(());
122125
}
126+
let instance = unsafe { NonNull::new_unchecked(ptr) };
123127
Ok(Instance {
124-
0: unsafe { NonNull::new_unchecked(ptr) },
128+
instance: instance,
129+
module: Module {
130+
ptr: unsafe { sys::fizzy_get_instance_module(instance.as_ptr()) },
131+
owned: false,
132+
},
125133
})
126134
}
127135

@@ -311,17 +319,14 @@ impl TypedExecutionResult {
311319
impl Instance {
312320
/// Get a read-only pointer to the module.
313321
unsafe fn get_module_ptr(&self) -> *const sys::FizzyModule {
314-
let ptr = sys::fizzy_get_instance_module(self.0.as_ptr());
322+
let ptr = sys::fizzy_get_instance_module(self.instance.as_ptr());
315323
debug_assert!(!ptr.is_null());
316324
ptr
317325
}
318326

319327
/// Get a non-owned module instance.
320-
pub fn get_module(&self) -> Module {
321-
Module {
322-
ptr: unsafe { self.get_module_ptr() },
323-
owned: false,
324-
}
328+
pub fn get_module(&self) -> &Module {
329+
&self.module
325330
}
326331

327332
/// Find index of exported function by name.
@@ -354,7 +359,7 @@ impl Instance {
354359
depth: i32,
355360
) -> ExecutionResult {
356361
ExecutionResult {
357-
0: sys::fizzy_execute(self.0.as_ptr(), func_idx, args.as_ptr(), depth),
362+
0: sys::fizzy_execute(self.instance.as_ptr(), func_idx, args.as_ptr(), depth),
358363
}
359364
}
360365

0 commit comments

Comments
 (0)