How expensive is boxing a ZST

I understand from the docs that there are still restrictions to the box pointer even if the pointee is a ZST value (the Box doc mentions validity and alignment), which makes sense.

But how expensive is it for the pointee side? Is anything actually heap-allocated?
It's easy to assume not, but I'd like to know just how expensive boxing a ZST value really is.

Nothing gets allocated if you box a zst

3 Likes

The Box itself still occupies normal pointer size though.

3 Likes

@cuviper I expected as much. So ultimately the price paid for boxing a ZST value is the size of a pointer. That's acceptable in this case.

Thanks guys!

A quick code demonstration: https://rust.godbolt.org/z/Y9c8Y7hGr

It just gives a constant pointer.

1 Like