byte icon indicating copy to clipboard operation
byte copied to clipboard

Usability of TryWrite trait

Open porky11 opened this issue 2 years ago • 2 comments

In order to use the TryWrite trait, you would need to allocate a byte array of the correct size in advance.

But how is it meant to be used? You can't know the size in advance.

I think, the API of the trait would need to change. The easiest change would be adding a second method, which returns the size in bytes without allocating anything. Another option would be to return a Result<Vec> instaed of supplying a mutable reference to an u8 slice and returning the size, but I guess, that would be inefficient. Or maybe the current trait could be used with some kind of fake slice?

So if you don't have a better solution, I'll add a new size method to the trait.

porky11 avatar Jul 19 '21 19:07 porky11

In order to use the TryWrite trait, you would need to allocate a byte array of the correct size in advance. But how is it meant to be used?

This crate was designed for embedded devices where allocation is not available and the buffer size is fixed at compilation. Of course, the max package size is known in advance in this case, so it wasn't a problem.

andylokandy avatar Jul 20 '21 16:07 andylokandy

I wanted to use it for simple conversion to binary, similar to serde bincode. I also need std and heap allocation.

Serde just seems like bloat to me, especially when just converting stuff to binary. For example when you have to implement some Deserializer yourself (which was neccessary for me because of some uncommon trait bounds), you even have to implement two different traits. And besides that, I don't like to have so many diffierent functions for serializing fields. This also should make it less efficient, if it has to read in the names of the structs, fields and enum variants, if they aren't even used.

porky11 avatar Jul 20 '21 20:07 porky11