1 use alloc::string::String; 2 use core::mem; 3 4 #[repr(C)] 5 pub struct RustString { 6 repr: String, 7 } 8 9 impl RustString { from(s: String) -> Self10 pub fn from(s: String) -> Self { 11 RustString { repr: s } 12 } 13 from_ref(s: &String) -> &Self14 pub fn from_ref(s: &String) -> &Self { 15 unsafe { &*(s as *const String as *const RustString) } 16 } 17 from_mut(s: &mut String) -> &mut Self18 pub fn from_mut(s: &mut String) -> &mut Self { 19 unsafe { &mut *(s as *mut String as *mut RustString) } 20 } 21 into_string(self) -> String22 pub fn into_string(self) -> String { 23 self.repr 24 } 25 as_string(&self) -> &String26 pub fn as_string(&self) -> &String { 27 &self.repr 28 } 29 as_mut_string(&mut self) -> &mut String30 pub fn as_mut_string(&mut self) -> &mut String { 31 &mut self.repr 32 } 33 } 34 35 const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<String>()); 36 const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<String>()); 37