Created
March 6, 2025 11:19
-
-
Save qatoqat/c2eca6e5ccdba7a7918582442ed56b8f to your computer and use it in GitHub Desktop.
Weak pointer sizes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| use std::sync::{Arc, Weak}; | |
| fn main() { | |
| struct A { | |
| valid: Option<Arc<()>> | |
| } | |
| impl A { | |
| fn weak(&mut self) -> Weak<()> { | |
| let valid = Arc::new(()); | |
| let weak_valid = Arc::downgrade(&valid); | |
| self.valid = Some(valid); | |
| weak_valid | |
| } | |
| } | |
| struct B { | |
| valid: Weak<()> | |
| } | |
| struct C {} | |
| let mut a = A { valid: None }; | |
| println!("{}", size_of::<A>()); | |
| println!("{}", size_of::<HashMap<u64, u64>>()); | |
| println!("1 Size of s: {} bytes", size_of_val(&a)); | |
| let b = B{ valid: a.weak() }; | |
| println!("{}", size_of::<B>()); | |
| println!("2 Size of s: {} bytes", size_of_val(&b)); | |
| b.valid.upgrade().unwrap(); | |
| println!("3 Size of s: {} bytes", size_of_val(&b)); | |
| drop(a); | |
| println!("4 Size of s: {} bytes", size_of_val(&b)); | |
| // b.valid.upgrade().unwrap(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment