diff options
author | David Tolnay <dtolnay@gmail.com> | 2021-04-10 12:18:31 -0700 |
---|---|---|
committer | David Tolnay <dtolnay@gmail.com> | 2021-04-10 12:49:09 -0700 |
commit | 56377fc4f8ccbf7e186ac4f6716e54a9b44b05f9 (patch) | |
tree | 3bbc3bb71bdb9a0ec37f78d990d468b4dc41a69b | |
parent | f9ad992c9930abfda50a1ce7f2892f59dc2633f1 (diff) | |
download | cxx-56377fc4f8ccbf7e186ac4f6716e54a9b44b05f9.tar.gz |
Resolve improper ctype lint affecting RustString
-rw-r--r-- | src/rust_string.rs | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/rust_string.rs b/src/rust_string.rs index a5fa3f49..77a83f62 100644 --- a/src/rust_string.rs +++ b/src/rust_string.rs @@ -1,14 +1,15 @@ use alloc::string::String; -use core::mem; +use core::mem::{self, MaybeUninit}; +// ABI compatible with C++ rust::String (not necessarily alloc::string::String). #[repr(C)] pub struct RustString { - repr: String, + repr: [MaybeUninit<usize>; mem::size_of::<String>() / mem::size_of::<usize>()], } impl RustString { pub fn from(s: String) -> Self { - RustString { repr: s } + unsafe { mem::transmute::<String, RustString>(s) } } pub fn from_ref(s: &String) -> &Self { @@ -20,17 +21,18 @@ impl RustString { } pub fn into_string(self) -> String { - self.repr + unsafe { mem::transmute::<RustString, String>(self) } } pub fn as_string(&self) -> &String { - &self.repr + unsafe { &*(self as *const RustString as *const String) } } pub fn as_mut_string(&mut self) -> &mut String { - &mut self.repr + unsafe { &mut *(self as *mut RustString as *mut String) } } } -const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<String>()); -const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<String>()); +const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<RustString>()); +const_assert_eq!(mem::size_of::<String>(), mem::size_of::<RustString>()); +const_assert_eq!(mem::align_of::<String>(), mem::align_of::<RustString>()); |