New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added serialization, bitstreams, traits for int sign, TagPayloadType, some fixes to std #1775
Conversation
The [de]serializer doc comments should probably mention the format used (e.g. JSON, CBOR, Flat Buffers, Protocol Buffers, a custom format, etc.) |
I'd like to suggest assert tests for edge cases such as u0, {ui}1,7,8,9 15,16,17,31,32,33,63,64,65,127,128 in big and little endian. As well as NaN and Inf for f32, f64. |
If you look at this function in io_test.zig, you will see that every bit width between 0 and I didn't test float edge cases, which is a good idea, however I expect they will work fine since floats are just bit-casted to an equivelent unsigned integer before being serialized since zig guarantees a float representation. |
Thanks for pointing out One other suggestion, prepend all of the test names with "io_test.", like test "io_test.BitInStream", find it makes running tests much faster. With --test-filter io the time is less than a second:
Compared to nearly 10s with no filter:
|
I see you made the test changes just as I was commenting, LGTM! Please consider renaming the tests. |
I normally name my tests after the package path (i.e: std.meta.x, std.meta.trait.x), but in this case I followed the practice of the files I added to because I didn't want to arbitrarily change a bunch of other people's stuff, you know? |
I understand, if you don't want to, I'll create a PR after this one gets merged. Making the name change as a separate PR is probably a good idea anyway. |
This looks like good stuff, it's just a lot, so I hope you don't mind if it takes me a while to merge. Do you mind if I re-organize your commits? I'll try to make sure you still get authorship, but it'll end up breaking stuff into smaller changes (and it would remove the "verified" status of the commits). The other option is for you to submit each independent thing separately. |
I said it in IRC but I'll repeat it here for the record, I don't mind if you re-organize my commits. |
…sts. Added tests for float inf and NaN.
…why this wasn't caught earlier.
Great work @tgschultz ! I'm only struggling with the POD requirement for serialization. How do you feel about an optional callback function for handling pointers as opposed to a compiletime error.
If the callback is not given the current behaviour would persist. Currently I write my own serialization and I can not replace it with the proposal because of the limitation. |
Just notice that you can implement fn serialize on your type. That's were serializations should go that require domain knowledge. Awesome! |
Thanks, bfloch. The idea behind this set of tools was to be very generic, and the problem with serializing pointers in structs, in my opinion, is that there just isn't an obvious way to do it that isn't dependent on the use case and is unsurprising (extern unions have a similar problem). As you've discovered, I included custom serializer/deserializer support to allow the user to handle such cases in whatever way their use case demands. Hopefully that will cover your needs. |
…side of the struct and aliased inside it. This will enable alternate generic serializers (i.e. one that follows pointers) on a struct-by-struct basis.
Added
BitInStream
/BitOutStream
tostd.io
:These streams wrap any other In/Out stream and allow you to read/write bitfields with a specified endianess.
Added
Serialzer
/Deserializer
tostd.io
:These wrap any other In/Out stream and provide the ability to read/write almost any type, including packed types, with specified endianess. Operates in either packed or unpacked mode. The former will bit-pack everything, the latter will byte-pack everything except packed structs which are bit-packed and then padded to the nearest byte.
Added traits for int sign to std.meta.trait:
trait.isSignedInt
,trait.isUnsignedInt
. These should be self-explanatory.Added
TagPayloadType
to std.meta:Given an enum tag and a union type, returns the type of the union field corresponding to the tag. Solves #1765.
Fixes:
std.meta.bitCount
andstd.meta.alignment
now return comptime_int.std.meta.eql
now handles Optionals.