cxx/
rust_string.rs

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