smessmer / binary-layout

The binary-layout library allows type-safe, inplace, zero-copy access to structured binary data. You define a custom data layout and give it a slice of binary data, and it will allow you to read and write the fields defined in the layout from the binary data without having to copy any of the data. It's similar to transmuting to/from a #[repr(packed)] struct, but much safer.
Apache License 2.0
66 stars 9 forks source link

Supporting `unit` and `never`. #9

Closed ckaran closed 2 years ago

ckaran commented 2 years ago

Both unit (()) and never (!) are zero-sized types, so there isn't any true binary representation for them. That said, they are primitive types, and define_layout!() could in theory encounter one (or both) of them. In addition, the documentation for never says that we should implement non-panicing traits for it whenever possible. Would it make sense to implement all the various traits for them except LayoutAs? My thought is that they accept and produce slices of zero length, which shouldn't cause any issues.

ckaran commented 2 years ago

OK, I created a PR for this (#11), but realized that I have no idea how to deal with the never type. The issue is that I want to make the implementation's existence contingent on someone activating the never feature via #![feature(never_type)]. Unfortunately, using cfg the way I thought it would work, doesn't actually work. Any ideas?

smessmer commented 2 years ago

Which traits concretely are you thinking about implementing for never?

ckaran commented 2 years ago

Everything except LayoutAs. I think that because ! can coerce into any type, it can also coerce to a slice type, and since the documentation for ! states that we should implement all traits for it, I think it makes to sense to follow the docs and implement everything except for LayoutAs.

ckaran commented 2 years ago

OK, I managed to sort of get it to work, but I discovered that it can't be tested because the never type has no values. That makes it impossible to write it out, and makes it impossible to verify that you've read it back in correctly. So, I'm not sure how we're supposed to add traits to it. I mean, I can add the appropriate traits to it, but then we can't do anything with them... should we close this issue?

smessmer commented 2 years ago

sounds good. If we really end up needing it for something, we can still add it.

ckaran commented 2 years ago

OK, I'm going to close this issue for now. If it needs to be reopened in the future, we can always do so then.

ckaran commented 2 years ago

OK, I'm going to close this issue for now. If it needs to be reopened in the future, we can always do so then.