zerocopy icon indicating copy to clipboard operation
zerocopy copied to clipboard

Make the inner type wrapped in `Unalign` public

Open ChayimFriedman2 opened this issue 6 months ago • 2 comments

This is not exactly a customer request since I'm not using this crate currently, but I was looking on the docs and I wondered why won't you make the inner field of Unalign public?

Usually when prefer to make newtype's field private, but in this case making it public has a big advantage: we cannot make a reference to it and so we cannot expose it in a non-copying way, but if it is public the compiler allows reads/writes to fields of it as long as we never take a reference and only read/write directly. This can make working with this type more comfortable.

ChayimFriedman2 avatar Feb 03 '24 21:02 ChayimFriedman2

I'm concerned that this will back us into a corner. In particular, there's a chance that we may want to support unsized Unalign types in the future (i.e., Unalign<T: ?Sized>) (https://github.com/google/zerocopy/issues/209). We probably can't do that without some heavy type machinery that might require changing the fields of Unalign to "manually reconstruct" the layout we're trying to build. (This comes from various limitations regarding repr(packed) with unsized types.)

I agree that there's a minor ergonomic benefit with making the inner field public, but are there any patterns that can't be expressed with our existing API and methods? If it's just an ergonomic thing, I'm inclined to not do this right now, but if it's an expressibility issue, then that's a different story.

joshlf avatar Mar 27 '24 19:03 joshlf

Note that https://github.com/google/zerocopy/pull/1076 implements this. I've closed it, but we can re-open if we decide to move forward with this.

joshlf avatar Mar 27 '24 19:03 joshlf