diff options
Diffstat (limited to 'src/symbols/rust_string.rs')
-rw-r--r-- | src/symbols/rust_string.rs | 54 |
1 files changed, 45 insertions, 9 deletions
diff --git a/src/symbols/rust_string.rs b/src/symbols/rust_string.rs index 91fd78a3..49d40697 100644 --- a/src/symbols/rust_string.rs +++ b/src/symbols/rust_string.rs @@ -7,24 +7,47 @@ use core::str; #[export_name = "cxxbridge1$string$new"] unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { - ptr::write(this.as_mut_ptr(), String::new()); + let this = this.as_mut_ptr(); + let new = String::new(); + unsafe { ptr::write(this, new) } } #[export_name = "cxxbridge1$string$clone"] unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { - ptr::write(this.as_mut_ptr(), other.clone()); + let this = this.as_mut_ptr(); + let clone = other.clone(); + unsafe { ptr::write(this, clone) } } -#[export_name = "cxxbridge1$string$from"] -unsafe extern "C" fn string_from( +#[export_name = "cxxbridge1$string$from_utf8"] +unsafe extern "C" fn string_from_utf8( this: &mut MaybeUninit<String>, ptr: *const u8, len: usize, ) -> bool { - let slice = slice::from_raw_parts(ptr, len); + let slice = unsafe { slice::from_raw_parts(ptr, len) }; match str::from_utf8(slice) { Ok(s) => { - ptr::write(this.as_mut_ptr(), s.to_owned()); + let this = this.as_mut_ptr(); + let owned = s.to_owned(); + unsafe { ptr::write(this, owned) } + true + } + Err(_) => false, + } +} + +#[export_name = "cxxbridge1$string$from_utf16"] +unsafe extern "C" fn string_from_utf16( + this: &mut MaybeUninit<String>, + ptr: *const u16, + len: usize, +) -> bool { + let slice = unsafe { slice::from_raw_parts(ptr, len) }; + match String::from_utf16(slice) { + Ok(s) => { + let this = this.as_mut_ptr(); + unsafe { ptr::write(this, s) } true } Err(_) => false, @@ -33,7 +56,7 @@ unsafe extern "C" fn string_from( #[export_name = "cxxbridge1$string$drop"] unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { - ManuallyDrop::drop(this); + unsafe { ManuallyDrop::drop(this) } } #[export_name = "cxxbridge1$string$ptr"] @@ -46,7 +69,20 @@ unsafe extern "C" fn string_len(this: &String) -> usize { this.len() } +#[export_name = "cxxbridge1$string$capacity"] +unsafe extern "C" fn string_capacity(this: &String) -> usize { + this.capacity() +} + +#[export_name = "cxxbridge1$string$reserve_additional"] +unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) { + this.reserve(additional); +} + #[export_name = "cxxbridge1$string$reserve_total"] -unsafe extern "C" fn string_reserve_total(this: &mut String, cap: usize) { - this.reserve(cap); +unsafe extern "C" fn string_reserve_total(this: &mut String, new_cap: usize) { + if new_cap > this.capacity() { + let additional = new_cap - this.len(); + this.reserve(additional); + } } |