• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use alloc::borrow::ToOwned;
2 use alloc::string::String;
3 use core::mem::{ManuallyDrop, MaybeUninit};
4 use core::ptr;
5 use core::slice;
6 use core::str;
7 
8 #[export_name = "cxxbridge1$string$new"]
string_new(this: &mut MaybeUninit<String>)9 unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) {
10     let this = this.as_mut_ptr();
11     let new = String::new();
12     unsafe { ptr::write(this, new) }
13 }
14 
15 #[export_name = "cxxbridge1$string$clone"]
string_clone(this: &mut MaybeUninit<String>, other: &String)16 unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) {
17     let this = this.as_mut_ptr();
18     let clone = other.clone();
19     unsafe { ptr::write(this, clone) }
20 }
21 
22 #[export_name = "cxxbridge1$string$from_utf8"]
string_from_utf8( this: &mut MaybeUninit<String>, ptr: *const u8, len: usize, ) -> bool23 unsafe extern "C" fn string_from_utf8(
24     this: &mut MaybeUninit<String>,
25     ptr: *const u8,
26     len: usize,
27 ) -> bool {
28     let slice = unsafe { slice::from_raw_parts(ptr, len) };
29     match str::from_utf8(slice) {
30         Ok(s) => {
31             let this = this.as_mut_ptr();
32             let owned = s.to_owned();
33             unsafe { ptr::write(this, owned) }
34             true
35         }
36         Err(_) => false,
37     }
38 }
39 
40 #[export_name = "cxxbridge1$string$from_utf16"]
string_from_utf16( this: &mut MaybeUninit<String>, ptr: *const u16, len: usize, ) -> bool41 unsafe extern "C" fn string_from_utf16(
42     this: &mut MaybeUninit<String>,
43     ptr: *const u16,
44     len: usize,
45 ) -> bool {
46     let slice = unsafe { slice::from_raw_parts(ptr, len) };
47     match String::from_utf16(slice) {
48         Ok(s) => {
49             let this = this.as_mut_ptr();
50             unsafe { ptr::write(this, s) }
51             true
52         }
53         Err(_) => false,
54     }
55 }
56 
57 #[export_name = "cxxbridge1$string$drop"]
string_drop(this: &mut ManuallyDrop<String>)58 unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) {
59     unsafe { ManuallyDrop::drop(this) }
60 }
61 
62 #[export_name = "cxxbridge1$string$ptr"]
string_ptr(this: &String) -> *const u863 unsafe extern "C" fn string_ptr(this: &String) -> *const u8 {
64     this.as_ptr()
65 }
66 
67 #[export_name = "cxxbridge1$string$len"]
string_len(this: &String) -> usize68 unsafe extern "C" fn string_len(this: &String) -> usize {
69     this.len()
70 }
71 
72 #[export_name = "cxxbridge1$string$capacity"]
string_capacity(this: &String) -> usize73 unsafe extern "C" fn string_capacity(this: &String) -> usize {
74     this.capacity()
75 }
76 
77 #[export_name = "cxxbridge1$string$reserve_additional"]
string_reserve_additional(this: &mut String, additional: usize)78 unsafe extern "C" fn string_reserve_additional(this: &mut String, additional: usize) {
79     this.reserve(additional);
80 }
81 
82 #[export_name = "cxxbridge1$string$reserve_total"]
string_reserve_total(this: &mut String, new_cap: usize)83 unsafe extern "C" fn string_reserve_total(this: &mut String, new_cap: usize) {
84     if new_cap > this.capacity() {
85         let additional = new_cap - this.len();
86         this.reserve(additional);
87     }
88 }
89