From 29712282912f6b797e93e338905d259f340f35d9 Mon Sep 17 00:00:00 2001 From: Katherine Kiefer Date: Fri, 20 Oct 2023 11:57:51 +1100 Subject: [PATCH] no uninit --- crates/byondapi-rs-test/dm_project/dm_project.dme | 10 ++++++---- crates/byondapi-rs-test/src/lib.rs | 4 ++-- crates/byondapi-rs-test/tests/test.rs | 1 - crates/byondapi-rs/src/error.rs | 5 +---- crates/byondapi-rs/src/value/functions.rs | 10 ++-------- crates/byondapi-rs/src/value/trait_impls.rs | 13 +++++-------- 6 files changed, 16 insertions(+), 27 deletions(-) diff --git a/crates/byondapi-rs-test/dm_project/dm_project.dme b/crates/byondapi-rs-test/dm_project/dm_project.dme index 3177d19..8d36135 100644 --- a/crates/byondapi-rs-test/dm_project/dm_project.dme +++ b/crates/byondapi-rs-test/dm_project/dm_project.dme @@ -56,13 +56,15 @@ if(O.name != ret) throw EXCEPTION("Call proc failed, expected rust to return 'test name' but got '[ret]'") +/datum/data + var/name = "test name" + /test/proc/test_readwrite_var() - var/obj/O = new() - O.name = "test name" + var/datum/data/stub = new() - var/ret = call_ext("byondapi_test.dll", "byond:test_readwrite_var")(O) + var/ret = call_ext("byondapi_test.dll", "byond:test_readwrite_var")(stub) - if(O.name != ret) + if(stub.name != ret) throw EXCEPTION("Call proc failed, expected rust to return 'test name' but got '[ret]'") /test/proc/test_list_push() diff --git a/crates/byondapi-rs-test/src/lib.rs b/crates/byondapi-rs-test/src/lib.rs index 57c0d92..ecb51e9 100644 --- a/crates/byondapi-rs-test/src/lib.rs +++ b/crates/byondapi-rs-test/src/lib.rs @@ -102,8 +102,8 @@ pub unsafe extern "C" fn test_readwrite_var( let args = parse_args(argc, argv); let object = &args[0]; - match object.read_var("name") { - Ok(res) => res, + match object.read_string("name") { + Ok(res) => res.try_into().unwrap(), Err(e) => format!("{:#?}", e).try_into().unwrap(), } } diff --git a/crates/byondapi-rs-test/tests/test.rs b/crates/byondapi-rs-test/tests/test.rs index 23dd391..6176193 100644 --- a/crates/byondapi-rs-test/tests/test.rs +++ b/crates/byondapi-rs-test/tests/test.rs @@ -6,7 +6,6 @@ use std::{ use tempfile::TempDir; #[test] -#[ignore] #[cfg(windows)] fn test_byondapi_with_dreamdaemon() { let dll = build_dylib(); diff --git a/crates/byondapi-rs/src/error.rs b/crates/byondapi-rs/src/error.rs index 3f20d4b..66bb6ab 100644 --- a/crates/byondapi-rs/src/error.rs +++ b/crates/byondapi-rs/src/error.rs @@ -17,7 +17,7 @@ pub enum Error { UnknownByondError, /// Thrown by us when we know this call will panic internally because of the version NotAvailableForThisByondVersion, - /// Thrown by us when we know this type does not have a ref + /// Thrown by us when we know this type does not have a refnumber NotReferencable, /// Thrown by us when we know this type is not a list, and we're expecting one NotAList, @@ -27,8 +27,6 @@ pub enum Error { NotANum, /// Thrown by us when we know this type is not a pointer, and we're expecting one NotAPtr, - /// Thrown by us when we know this type is not a ref, and we're expecting one - NotARef, /// Thrown by [`crate::byond_string::str_id_of_cstr`] when the string doesn't exist in /// byondland NonExistentString, @@ -61,7 +59,6 @@ impl std::fmt::Display for Error { Self::NotAString => write!(f, "Value is not a string"), Self::NotANum => write!(f, "Value is not a number"), Self::NotAPtr => write!(f, "Value is not a pointer"), - Self::NotARef => write!(f, "Value is not a ref"), Self::NonExistentString => write!(f, "String id not found"), } } diff --git a/crates/byondapi-rs/src/value/functions.rs b/crates/byondapi-rs/src/value/functions.rs index 429b7b8..829b14e 100644 --- a/crates/byondapi-rs/src/value/functions.rs +++ b/crates/byondapi-rs/src/value/functions.rs @@ -35,13 +35,7 @@ impl ByondValue { /// Try to get a [`CString`] or fail if this isn't a string type pub fn get_cstring(&self) -> Result { - if self.is_str() { - let ptr = unsafe { byond().ByondValue_GetStr(&self.0) }; - let cstr = unsafe { CStr::from_ptr(ptr) }; - Ok(cstr.to_owned()) - } else { - Err(Error::NotAString) - } + self.get_cstr().map(|cstr| cstr.to_owned()) } /// Try to get a [`CStr`] or fail if this isn't a string type @@ -116,7 +110,7 @@ impl ByondValue { /// Read a variable through the ref. Fails if this isn't a ref type. pub fn read_var>>(&self, name: T) -> Result { if self.is_num() || self.is_str() || self.is_ptr() || self.is_null() || self.is_list() { - return Err(Error::NotARef); + return Err(Error::NotReferencable); } let c_string = CString::new(name).unwrap(); let c_str = c_string.as_c_str(); diff --git a/crates/byondapi-rs/src/value/trait_impls.rs b/crates/byondapi-rs/src/value/trait_impls.rs index d09d7d0..7483fb9 100644 --- a/crates/byondapi-rs/src/value/trait_impls.rs +++ b/crates/byondapi-rs/src/value/trait_impls.rs @@ -1,20 +1,17 @@ use super::ByondValue; use crate::static_global::byond; -use std::{fmt::Debug, mem::MaybeUninit}; +use std::fmt::Debug; // Memory handling impl Clone for ByondValue { fn clone(&self) -> Self { - let mut new_inner = MaybeUninit::uninit(); + let mut cloned_value = ByondValue::null(); - let new_inner = unsafe { - // Safety: new_inner is going to an initialization function, it will only write to the pointer. - byond().ByondValue_CopyFrom(new_inner.as_mut_ptr(), &self.0); - // Safety: ByondValue_Init will have initialized the new_inner. - new_inner.assume_init() + unsafe { + byond().ByondValue_CopyFrom(&mut cloned_value.0, &self.0); }; - Self(new_inner) + cloned_value } }