solana_stable_layout/
stable_rc.rs

1//! Ensure Rc has a stable memory layout
2
3#[cfg(test)]
4mod tests {
5    use std::{
6        mem::{align_of, size_of},
7        rc::Rc,
8    };
9
10    #[test]
11    fn test_memory_layout() {
12        assert_eq!(align_of::<Rc<i32>>(), 8);
13        assert_eq!(size_of::<Rc<i32>>(), 8);
14
15        let value = 42;
16        let rc = Rc::new(value);
17        let _rc2 = Rc::clone(&rc); // used to increment strong count
18
19        let addr_rc = &rc as *const _ as usize;
20        let addr_ptr = addr_rc;
21        let addr_rcbox = unsafe { *(addr_ptr as *const *const i32) } as usize;
22        let addr_strong = addr_rcbox;
23        let addr_weak = addr_rcbox + 8;
24        let addr_value = addr_rcbox + 16;
25        assert_eq!(unsafe { *(addr_strong as *const usize) }, 2);
26        assert_eq!(unsafe { *(addr_weak as *const usize) }, 1);
27        assert_eq!(unsafe { *(addr_value as *const i32) }, 42);
28    }
29}