1 use core::mem;
2 use core::ptr::NonNull;
3 use core::slice;
4 use core::str;
5 
6 // Not necessarily ABI compatible with &str. Codegen performs the translation.
7 #[repr(C)]
8 #[derive(Copy, Clone)]
9 pub struct RustStr {
10     pub(crate) ptr: NonNull<u8>,
11     pub(crate) len: usize,
12 }
13 
14 impl RustStr {
from(s: &str) -> Self15     pub fn from(s: &str) -> Self {
16         RustStr {
17             ptr: NonNull::from(s).cast::<u8>(),
18             len: s.len(),
19         }
20     }
21 
as_str<'a>(self) -> &'a str22     pub unsafe fn as_str<'a>(self) -> &'a str {
23         let slice = slice::from_raw_parts(self.ptr.as_ptr(), self.len);
24         str::from_utf8_unchecked(slice)
25     }
26 }
27 
28 const_assert_eq!(mem::size_of::<Option<RustStr>>(), mem::size_of::<RustStr>());
29