Skip to content

Instantly share code, notes, and snippets.

@qatoqat
Created March 6, 2025 11:19
Show Gist options
  • Select an option

  • Save qatoqat/c2eca6e5ccdba7a7918582442ed56b8f to your computer and use it in GitHub Desktop.

Select an option

Save qatoqat/c2eca6e5ccdba7a7918582442ed56b8f to your computer and use it in GitHub Desktop.
Weak pointer sizes
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