1 #![cfg(feature = "alloc")]
2
3 use alloc::borrow::ToOwned;
4 use alloc::string::String;
5 use core::mem::{ManuallyDrop, MaybeUninit};
6 use core::ptr;
7 use core::slice;
8 use core::str;
9
10 #[export_name = "cxxbridge1$string$new"]
string_new(this: &mut MaybeUninit<String>)11 unsafe 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"]
string_clone(this: &mut MaybeUninit<String>, other: &String)18 unsafe 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"]
string_from_utf8( this: &mut MaybeUninit<String>, ptr: *const u8, len: usize, ) -> bool25 unsafe 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"]
string_from_utf8_lossy( this: &mut MaybeUninit<String>, ptr: *const u8, len: usize, )43 unsafe 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"]
string_from_utf16( this: &mut MaybeUninit<String>, ptr: *const u16, len: usize, ) -> bool55 unsafe 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"]
string_from_utf16_lossy( this: &mut MaybeUninit<String>, ptr: *const u16, len: usize, )72 unsafe 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"]
string_drop(this: &mut ManuallyDrop<String>)84 unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) {
85 unsafe { ManuallyDrop::drop(this) }
86 }
87
88 #[export_name = "cxxbridge1$string$ptr"]
string_ptr(this: &String) -> *const u889 unsafe extern "C" fn string_ptr(this: &String) -> *const u8 {
90 this.as_ptr()
91 }
92
93 #[export_name = "cxxbridge1$string$len"]
string_len(this: &String) -> usize94 unsafe extern "C" fn string_len(this: &String) -> usize {
95 this.len()
96 }
97
98 #[export_name = "cxxbridge1$string$capacity"]
string_capacity(this: &String) -> usize99 unsafe extern "C" fn string_capacity(this: &String) -> usize {
100 this.capacity()
101 }
102
103 #[export_name = "cxxbridge1$string$reserve_additional"]
string_reserve_additional(this: &mut String, additional: usize)104 unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) {
105 this.reserve(additional);
106 }
107
108 #[export_name = "cxxbridge1$string$reserve_total"]
string_reserve_total(this: &mut String, new_cap: usize)109 unsafe 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