| David Tolnay | c5a52f9 | 2020-09-14 00:43:29 -0400 | [diff] [blame] | 1 | use alloc::borrow::ToOwned; |
| 2 | use alloc::string::String; |
| David Tolnay | 3384c14 | 2020-09-14 00:26:47 -0400 | [diff] [blame] | 3 | use core::mem::{ManuallyDrop, MaybeUninit}; |
| 4 | use core::ptr; |
| 5 | use core::slice; |
| 6 | use core::str; |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 7 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 8 | #[export_name = "cxxbridge05$string$new"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 9 | unsafe extern "C" fn string_new(this: &mut MaybeUninit<String>) { |
| 10 | ptr::write(this.as_mut_ptr(), String::new()); |
| 11 | } |
| 12 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 13 | #[export_name = "cxxbridge05$string$clone"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 14 | unsafe extern "C" fn string_clone(this: &mut MaybeUninit<String>, other: &String) { |
| 15 | ptr::write(this.as_mut_ptr(), other.clone()); |
| 16 | } |
| 17 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 18 | #[export_name = "cxxbridge05$string$from"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 19 | 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 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 34 | #[export_name = "cxxbridge05$string$drop"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 35 | unsafe extern "C" fn string_drop(this: &mut ManuallyDrop<String>) { |
| 36 | ManuallyDrop::drop(this); |
| 37 | } |
| 38 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 39 | #[export_name = "cxxbridge05$string$ptr"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 40 | unsafe extern "C" fn string_ptr(this: &String) -> *const u8 { |
| 41 | this.as_ptr() |
| 42 | } |
| 43 | |
| David Tolnay | 8f16ae7 | 2020-10-08 18:21:13 -0700 | [diff] [blame] | 44 | #[export_name = "cxxbridge05$string$len"] |
| David Tolnay | 3c90cd2 | 2020-04-30 07:28:21 -0700 | [diff] [blame] | 45 | unsafe extern "C" fn string_len(this: &String) -> usize { |
| 46 | this.len() |
| 47 | } |