• 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     ptr::write(this.as_mut_ptr(), String::new());
11 }
12 
13 #[export_name = "cxxbridge1$string$clone"]
string_clone(this: &mut MaybeUninit<String>, other: &String)14 unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) {
15     ptr::write(this.as_mut_ptr(), other.clone());
16 }
17 
18 #[export_name = "cxxbridge1$string$from"]
string_from( this: &mut MaybeUninit<String>, ptr: *const u8, len: usize, ) -> bool19 unsafe extern "C" fn string_from(
20     this: &mut MaybeUninit<String>,
21     ptr: *const u8,
22     len: usize,
23 ) -> bool {
24     let slice = slice::from_raw_parts(ptr, len);
25     match str::from_utf8(slice) {
26         Ok(s) => {
27             ptr::write(this.as_mut_ptr(), s.to_owned());
28             true
29         }
30         Err(_) => false,
31     }
32 }
33 
34 #[export_name = "cxxbridge1$string$drop"]
string_drop(this: &mut ManuallyDrop<String>)35 unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) {
36     ManuallyDrop::drop(this);
37 }
38 
39 #[export_name = "cxxbridge1$string$ptr"]
string_ptr(this: &String) -> *const u840 unsafe extern "C" fn string_ptr(this: &String) -> *const u8 {
41     this.as_ptr()
42 }
43 
44 #[export_name = "cxxbridge1$string$len"]
string_len(this: &String) -> usize45 unsafe extern "C" fn string_len(this: &String) -> usize {
46     this.len()
47 }
48 
49 #[export_name = "cxxbridge1$string$reserve_total"]
string_reserve_total(this: &mut String, cap: usize)50 unsafe extern "C" fn string_reserve_total(this: &mut String, cap: usize) {
51     this.reserve(cap);
52 }
53