From 56377fc4f8ccbf7e186ac4f6716e54a9b44b05f9 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sat, 10 Apr 2021 12:18:31 -0700 Subject: Resolve improper ctype lint affecting RustString --- src/rust_string.rs | 18 ++++++++++-------- 1 file 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; mem::size_of::() / mem::size_of::()], } impl RustString { pub fn from(s: String) -> Self { - RustString { repr: s } + unsafe { mem::transmute::(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::(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::()); -const_assert_eq!(mem::align_of::(), mem::align_of::()); +const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::()); +const_assert_eq!(mem::size_of::(), mem::size_of::()); +const_assert_eq!(mem::align_of::(), mem::align_of::()); -- cgit v1.2.3