1#![cfg(feature = "alloc")] 2 3use alloc::borrow::ToOwned; 4use alloc::string::String; 5use core::mem::{ManuallyDrop, MaybeUninit}; 6use core::ptr; 7use core::slice; 8use core::str; 9 10#[export_name = "cxxbridge1$string$new"] 11unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { 12 let this = this.as_mut_ptr(); 13 let new = String::new(); 14 unsafe { ptr::write(this, new) } 15} 16 17#[export_name = "cxxbridge1$string$clone"] 18unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { 19 let this = this.as_mut_ptr(); 20 let clone = other.clone(); 21 unsafe { ptr::write(this, clone) } 22} 23 24#[export_name = "cxxbridge1$string$from_utf8"] 25unsafe extern "C" fn string_from_utf8( 26 this: &mut MaybeUninit<String>, 27 ptr: *const u8, 28 len: usize, 29) -> bool { 30 let slice = unsafe { slice::from_raw_parts(ptr, len) }; 31 match str::from_utf8(slice) { 32 Ok(s) => { 33 let this = this.as_mut_ptr(); 34 let owned = s.to_owned(); 35 unsafe { ptr::write(this, owned) } 36 true 37 } 38 Err(_) => false, 39 } 40} 41 42#[export_name = "cxxbridge1$string$from_utf8_lossy"] 43unsafe extern "C" fn string_from_utf8_lossy( 44 this: &mut MaybeUninit<String>, 45 ptr: *const u8, 46 len: usize, 47) { 48 let slice = unsafe { slice::from_raw_parts(ptr, len) }; 49 let owned = String::from_utf8_lossy(slice).into_owned(); 50 let this = this.as_mut_ptr(); 51 unsafe { ptr::write(this, owned) } 52} 53 54#[export_name = "cxxbridge1$string$from_utf16"] 55unsafe extern "C" fn string_from_utf16( 56 this: &mut MaybeUninit<String>, 57 ptr: *const u16, 58 len: usize, 59) -> bool { 60 let slice = unsafe { slice::from_raw_parts(ptr, len) }; 61 match String::from_utf16(slice) { 62 Ok(s) => { 63 let this = this.as_mut_ptr(); 64 unsafe { ptr::write(this, s) } 65 true 66 } 67 Err(_) => false, 68 } 69} 70 71#[export_name = "cxxbridge1$string$from_utf16_lossy"] 72unsafe extern "C" fn string_from_utf16_lossy( 73 this: &mut MaybeUninit<String>, 74 ptr: *const u16, 75 len: usize, 76) { 77 let slice = unsafe { slice::from_raw_parts(ptr, len) }; 78 let owned = String::from_utf16_lossy(slice); 79 let this = this.as_mut_ptr(); 80 unsafe { ptr::write(this, owned) } 81} 82 83#[export_name = "cxxbridge1$string$drop"] 84unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { 85 unsafe { ManuallyDrop::drop(this) } 86} 87 88#[export_name = "cxxbridge1$string$ptr"] 89unsafe extern "C" fn string_ptr(this: &String) -> *const u8 { 90 this.as_ptr() 91} 92 93#[export_name = "cxxbridge1$string$len"] 94unsafe extern "C" fn string_len(this: &String) -> usize { 95 this.len() 96} 97 98#[export_name = "cxxbridge1$string$capacity"] 99unsafe extern "C" fn string_capacity(this: &String) -> usize { 100 this.capacity() 101} 102 103#[export_name = "cxxbridge1$string$reserve_additional"] 104unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) { 105 this.reserve(additional); 106} 107 108#[export_name = "cxxbridge1$string$reserve_total"] 109unsafe extern "C" fn string_reserve_total(this: &mut String, new_cap: usize) { 110 if new_cap > this.capacity() { 111 let additional = new_cap - this.len(); 112 this.reserve(additional); 113 } 114} 115