• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #![cfg(feature = "alloc")]
2 #![allow(missing_docs)]
3 
4 use alloc::string::String;
5 use core::mem::{self, MaybeUninit};
6 use core::ptr;
7 
8 // ABI compatible with C++ rust::String (not necessarily alloc::string::String).
9 #[repr(C)]
10 pub struct RustString {
11     repr: [MaybeUninit<usize>; mem::size_of::<String>() / mem::size_of::<usize>()],
12 }
13 
14 impl RustString {
from(s: String) -> Self15     pub fn from(s: String) -> Self {
16         unsafe { mem::transmute::<String, RustString>(s) }
17     }
18 
from_ref(s: &String) -> &Self19     pub fn from_ref(s: &String) -> &Self {
20         unsafe { &*(s as *const String as *const RustString) }
21     }
22 
from_mut(s: &mut String) -> &mut Self23     pub fn from_mut(s: &mut String) -> &mut Self {
24         unsafe { &mut *(s as *mut String as *mut RustString) }
25     }
26 
into_string(self) -> String27     pub fn into_string(self) -> String {
28         unsafe { mem::transmute::<RustString, String>(self) }
29     }
30 
as_string(&self) -> &String31     pub fn as_string(&self) -> &String {
32         unsafe { &*(self as *const RustString as *const String) }
33     }
34 
as_mut_string(&mut self) -> &mut String35     pub fn as_mut_string(&mut self) -> &mut String {
36         unsafe { &mut *(self as *mut RustString as *mut String) }
37     }
38 }
39 
40 impl Drop for RustString {
drop(&mut self)41     fn drop(&mut self) {
42         unsafe { ptr::drop_in_place(self.as_mut_string()) }
43     }
44 }
45 
46 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<RustString>());
47 const_assert_eq!(mem::size_of::<String>(), mem::size_of::<RustString>());
48 const_assert_eq!(mem::align_of::<String>(), mem::align_of::<RustString>());
49