phiryll / lexy

Lexicographical Byte Order Encodings
MIT License
0 stars 0 forks source link

Implement StructCodec helpers or provide examples on how to roll your own #11

Closed phiryll closed 2 months ago

phiryll commented 3 months ago

See the first comment. Just looking for a good way to help clients create their own at this point.

phiryll commented 2 months ago

This appears to be nearly impossible while retaining the generic types in this project. Struct fields can have any type, and it's not possible to have a Codec[some unknown type], even if you happen to have discovered the type through reflection. You simply have to know it at compile time.

In a nutshell, it's impossible for non-generic code to refer to generic code without instantiating it.

The only alternative seems to be to untype the entire framework (all Codecs read/write "any"), and that's not a step I'm willing to take. Besides the loss in type safety, some things become much more difficult, like dereferencing pointers.

It might be possible to provide helpers to allow clients to more easily create their own Codecs for struct types, but that's as far as lexy can possibly go. If that isn't feasible, then provide good examples on how to do it properly.

phiryll commented 2 months ago

Exploring using internal non-generic implementations for the aggregate types (pointer, slice, map, struct). The logic would be in those implementations, and the generic (exported) versions would delegate to them.

phiryll commented 2 months ago

Too much time spent trying to make aggregate types work. Abandoning that approach for now, will file a new issue to work on later.

Need to make rolling your own easy instead. That's at least easily achievable.

phiryll commented 2 months ago

Include two examples. One where the fields and their order are expected to never change, and another where they might (field names must be serialized as well).