byte icon indicating copy to clipboard operation
byte copied to clipboard

Create a derive library

Open porky11 opened this issue 2 years ago • 2 comments

It would be nice to be able to derive the traits TryRead and TryWrite. It would have to be done similar to serde-derive, but will probably be a bit easier because of the simpler format used in this crate.

This will have to be done in an additional crate and will be called byte-derive.

I'll try it myself, but I'm not that experienced with proc macros.

porky11 avatar Jul 19 '21 11:07 porky11

For simple (low-level) use cases, it usually doesn't have a common pattern like tuple, UTF-8, quotes, etc as the formats of serde have, but rather very flexible for individual protocols. Thus I think it may be hard to abstract a common way to derive the traits.

Anyway, if you've any idea about it, please share it with me 😄 .

andylokandy avatar Jul 20 '21 16:07 andylokandy

I don't see this problem. There don't need to be special cases for serialization of different kinds of data types. The special case is already the implementation of the serialization traits. The default implementation would be as simple as (de)serializing all elements of the tuple or fields of a struct in order. And for enums, it would also add a usize for the selected variant.

But I already found another problem. You sometimes switch the context used during parsing (mostly Endian for numbers, but other types need other contexts or no context).

I'm not sure, if this is possible without increasing the complexity of this crate. Maybe if the context was an associated type instead of a generic type of the traits, it would be easier. Currently only a single context is used for parsing most traits. But that might bring other disadvantages.

I might just try, when I really need it.

porky11 avatar Jul 20 '21 20:07 porky11