aboutsummaryrefslogtreecommitdiff
path: root/src/rust_str.rs
blob: b944ede95c9d821a119cff715d001bbfd7f15439 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use std::mem;
use std::ptr::NonNull;
use std::slice;
use std::str;

// Not necessarily ABI compatible with &str. Codegen performs the translation.
#[repr(C)]
#[derive(Copy, Clone)]
pub struct RustStr {
    pub(crate) ptr: NonNull<u8>,
    pub(crate) len: usize,
}

impl RustStr {
    pub fn from(s: &str) -> Self {
        RustStr {
            ptr: NonNull::from(s).cast::<u8>(),
            len: s.len(),
        }
    }

    pub unsafe fn as_str<'a>(self) -> &'a str {
        let slice = slice::from_raw_parts(self.ptr.as_ptr(), self.len);
        str::from_utf8_unchecked(slice)
    }
}

const_assert_eq!(mem::size_of::<Option<RustStr>>(), mem::size_of::<RustStr>());