| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 1 | use std::mem::{self, ManuallyDrop, MaybeUninit}; |
| 2 | use std::ptr; |
| 3 | use std::slice; |
| 4 | use std::str; |
| 5 | |
| 6 | #[repr(C)] |
| 7 | pub struct RustString { |
| 8 | repr: String, |
| 9 | } |
| 10 | |
| 11 | impl RustString { |
| 12 | pub fn from(s: String) -> Self { |
| 13 | RustString { repr: s } |
| 14 | } |
| 15 | |
| 16 | pub fn from_ref(s: &String) -> &Self { |
| 17 | unsafe { std::mem::transmute::<&String, &RustString>(s) } |
| 18 | } |
| 19 | |
| 20 | pub fn into_string(self) -> String { |
| 21 | self.repr |
| 22 | } |
| 23 | |
| 24 | pub fn as_string(&self) -> &String { |
| 25 | &self.repr |
| 26 | } |
| David Tolnay | 40226ab | 2020-03-03 00:05:35 -0800 | [diff] [blame] | 27 | |
| 28 | pub fn as_mut_string(&mut self) -> &mut String { |
| 29 | &mut self.repr |
| 30 | } |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 31 | } |
| 32 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 33 | #[export_name = "cxxbridge02$string$new"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 34 | unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { |
| 35 | ptr::write(this.as_mut_ptr(), String::new()); |
| 36 | } |
| 37 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 38 | #[export_name = "cxxbridge02$string$clone"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 39 | unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { |
| 40 | ptr::write(this.as_mut_ptr(), other.clone()); |
| 41 | } |
| 42 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 43 | #[export_name = "cxxbridge02$string$from"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 44 | unsafe extern "C" fn string_from( |
| 45 | this: &mut MaybeUninit<String>, |
| 46 | ptr: *const u8, |
| 47 | len: usize, |
| 48 | ) -> bool { |
| 49 | let slice = slice::from_raw_parts(ptr, len); |
| 50 | match str::from_utf8(slice) { |
| 51 | Ok(s) => { |
| 52 | ptr::write(this.as_mut_ptr(), s.to_owned()); |
| 53 | true |
| 54 | } |
| 55 | Err(_) => false, |
| 56 | } |
| 57 | } |
| 58 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 59 | #[export_name = "cxxbridge02$string$drop"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 60 | unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { |
| 61 | ManuallyDrop::drop(this); |
| 62 | } |
| 63 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 64 | #[export_name = "cxxbridge02$string$ptr"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 65 | unsafe extern "C" fn string_ptr(this: &String) -> *const u8 { |
| 66 | this.as_ptr() |
| 67 | } |
| 68 | |
| David Tolnay | 8c73049 | 2020-03-13 01:29:06 -0700 | [diff] [blame] | 69 | #[export_name = "cxxbridge02$string$len"] |
| David Tolnay | 7db7369 | 2019-10-20 14:51:12 -0400 | [diff] [blame] | 70 | unsafe extern "C" fn string_len(this: &String) -> usize { |
| 71 | this.len() |
| 72 | } |
| 73 | |
| 74 | fn _assert() { |
| 75 | let _: [(); mem::size_of::<[usize; 3]>()] = [(); mem::size_of::<String>()]; |
| 76 | let _: [(); mem::align_of::<usize>()] = [(); mem::align_of::<String>()]; |
| 77 | } |