blob: f0d1df19bd464e32c25fea7bd6717ca57ded95df [file] [log] [blame]
David Tolnay3c90cd22020-04-30 07:28:21 -07001use std::mem;
David Tolnay7db73692019-10-20 14:51:12 -04002
3#[repr(C)]
4pub struct RustString {
5 repr: String,
6}
7
8impl RustString {
9 pub fn from(s: String) -> Self {
10 RustString { repr: s }
11 }
12
13 pub fn from_ref(s: &String) -> &Self {
David Tolnaydb96ed92020-03-18 17:20:39 -070014 unsafe { &*(s as *const String as *const RustString) }
David Tolnay7db73692019-10-20 14:51:12 -040015 }
16
David Tolnayf1c7f322020-08-27 00:46:01 -070017 pub fn from_mut(s: &mut String) -> &mut Self {
18 unsafe { &mut *(s as *mut String as *mut RustString) }
19 }
20
David Tolnay7db73692019-10-20 14:51:12 -040021 pub fn into_string(self) -> String {
22 self.repr
23 }
24
25 pub fn as_string(&self) -> &String {
26 &self.repr
27 }
David Tolnay40226ab2020-03-03 00:05:35 -080028
29 pub fn as_mut_string(&mut self) -> &mut String {
30 &mut self.repr
31 }
David Tolnay7db73692019-10-20 14:51:12 -040032}
33
David Tolnayaa77e822020-04-24 15:43:05 -070034const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<String>());
35const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<String>());