I'm revisiting some of the basic types (and smart pointers), and I decided to make a simple Rust program to printout the details for each of them, with some types it's working ok yielding correct info, but when it comes to the Rc type, it's Deref trait implementation returns a private type, that I cannot somewhat get the details of (I'd expect to have memory for both reference counts + a ptr to the actual data)
Is there another way to do this?
I haven't looked at your code, but note that
Rc does store the data next to the reference counts, there is no second indirection.
Rc<u32> is a pointer to a memory location where the reference counts are stored directly next to the value of the
u32 itself. Also note - in case that isn't clear - that the layout of
Rc (both the
Rc itself, and also the struct it points to that contains the reference counts and the data) is not stable, so programs must not rely on these implementation details. (But it can be useful for learning-purposes, of course.)
So in practice, the length in question of RcBox<u32> is likely going to be (on a 64-bit machine): 2*8 bytes for the 2 counters, and 4 bytes for the u32 value, plus additional 4 bytes padding to get a multiple of 8, i.e. 32 bytes total.
Unrelated: is the website you link to officially maintained and up to date?
Thanks for the explanation.
My intention is for the program to be standalone, so that without intervention it can print out all the correct values. This way, without changing the code, it will be able to dump the sizes for each type no matter the rust version is compiled with
If you want to see a type's components, you can obviously follow the jump-to-definition provided by Rust-Analyzer along the way.
And it's much simpler to do this on rust-lang/rust - Sourcegraph.
It's not an official website, but it should be up-to-date, and it's super useful for inspecting implementation details of std as private and hidden documentation is included, and the linked source code has hyperlinks enabled.
On nightly, you can use
-Z print-type-sizes=yes: https://rust.godbolt.org/z/c4M91qTE4
That'll get you all kinds of stuff, like
print-type-size type: `std::panic::Location`: 24 bytes, alignment: 8 bytes
print-type-size field `.file`: 16 bytes
print-type-size field `.line`: 4 bytes
print-type-size field `.col`: 4 bytes
print-type-size type: `std::rc::RcBox<i32>`: 24 bytes, alignment: 8 bytes
print-type-size field `.strong`: 8 bytes
print-type-size field `.weak`: 8 bytes
print-type-size field `.value`: 4 bytes
print-type-size end padding: 4 bytes
print-type-size type: `std::alloc::Layout`: 16 bytes, alignment: 8 bytes
print-type-size field `.size`: 8 bytes
print-type-size field `.align`: 8 bytes
print-type-size type: `std::result::Result<std::ptr::NonNull<u8>, std::alloc::AllocError>`: 8 bytes, alignment: 8 bytes
print-type-size variant `Ok`: 8 bytes
print-type-size field `.0`: 8 bytes
print-type-size variant `Err`: 0 bytes
print-type-size field `.0`: 0 bytes
Thats exactly whats needed! Thanks!