diff --git a/src/f_string.rs b/src/f_string.rs index 7b94f82..96051a3 100644 --- a/src/f_string.rs +++ b/src/f_string.rs @@ -12,6 +12,7 @@ static FSTRING_PRINTF: OnceLock = OnceLock::new(); pub trait Printf { /// Since rust does not support varargs in functions, it is recommended to call format! fn printf>(handle: usize, fmt: A); + fn printf_unsafe>(&mut self, fmt: A); } pub struct FString(pub TArray); @@ -33,6 +34,27 @@ impl Printf for FString { ); } } + + #[inline(always)] + fn printf_unsafe>(&mut self, fmt: A) { + let tmp = U16CString::from_str(fmt).unwrap(); + let new_len = tmp.len(); + if new_len > self.0.cap as usize { + // TODO: use Result instead + panic!("New string is too long and no printf ffi binding has been provided"); + } + let mut tmp_bytes = tmp.into_vec_with_nul(); + tmp_bytes.resize(self.0.cap as usize, 0); + unsafe { + std::ptr::copy_nonoverlapping( + tmp_bytes.as_ptr(), + self.0.data_ptr as *mut u16, + self.0.cap as usize, + ); + } + self.0.len = new_len as u32; + self.0.write(); + } } impl<'a> Container<&'a U16CStr> for FString { @@ -70,4 +92,4 @@ impl Display for FString { #[inline(always)] fn fstring_printf() -> &'static usize { FSTRING_PRINTF.get().unwrap() -} +} \ No newline at end of file diff --git a/src/t_array.rs b/src/t_array.rs index 045ba6a..3e2da85 100644 --- a/src/t_array.rs +++ b/src/t_array.rs @@ -81,7 +81,7 @@ impl TArray { } #[inline(always)] - fn write(&self) { + pub(crate) fn write(&self) { let data = unsafe { std::slice::from_raw_parts_mut(self.ptr as *mut u8, T_ARRAY_HEADER_SIZE) };