Skip to content

Commit 737ea65

Browse files
committed
Ref type
1 parent 37490ca commit 737ea65

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

@@ -378,17 +386,14 @@ impl Instance {
378386

379387
/// Get a read-only pointer to the module.
380388
unsafe fn get_module_ptr(&self) -> *const sys::FizzyModule {
381-
let ptr = sys::fizzy_get_instance_module(self.0.as_ptr());
389+
let ptr = sys::fizzy_get_instance_module(self.instance.as_ptr());
382390
debug_assert!(!ptr.is_null());
383391
ptr
384392
}
385393

386394
/// Get a non-owned module instance.
387-
pub fn get_module(&self) -> Module {
388-
Module {
389-
ptr: unsafe { self.get_module_ptr() },
390-
owned: false,
391-
}
395+
pub fn get_module(&self) -> &Module {
396+
&self.module
392397
}
393398

394399
/// Find index of exported function by name.
@@ -421,7 +426,7 @@ impl Instance {
421426
depth: i32,
422427
) -> ExecutionResult {
423428
ExecutionResult {
424-
0: sys::fizzy_execute(self.0.as_ptr(), func_idx, args.as_ptr(), depth),
429+
0: sys::fizzy_execute(self.instance.as_ptr(), func_idx, args.as_ptr(), depth),
425430
}
426431
}
427432

0 commit comments

Comments
 (0)