aboutsummaryrefslogtreecommitdiff
path: root/src/rust_string.rs
blob: a923ced923246ce2753a1de3bae5c1e5505dcdd9 (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
29
30
31
use std::mem;

#[repr(C)]
pub struct RustString {
    repr: String,
}

impl RustString {
    pub fn from(s: String) -> Self {
        RustString { repr: s }
    }

    pub fn from_ref(s: &String) -> &Self {
        unsafe { &*(s as *const String as *const RustString) }
    }

    pub fn into_string(self) -> String {
        self.repr
    }

    pub fn as_string(&self) -> &String {
        &self.repr
    }

    pub fn as_mut_string(&mut self) -> &mut String {
        &mut self.repr
    }
}

const_assert_eq!(mem::size_of::<[usize; 3]>(), mem::size_of::<String>());
const_assert_eq!(mem::align_of::<usize>(), mem::align_of::<String>());