[revised, tests added] added support for buffer / memoryview data #147
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I iterated on #108 to add support for direct serialization of buffer protocol objects.
Closes #107.
I've added as much tests as I could imagine.
The generated code is a bit ugly because I found that if you pass a string to
uint8[]
field, its length is not checked. I wanted to explicitly make sure this behavior is kept for strings, but it did not occur good to me to keep it for the buffer protocol objects.One of the use-cases I have for this feature is incrementally building a
PointCloud2
. If you represent the data field asbytearray
, you get all the nicestd::vector<>
-like behavior (amortizedO(1)
append). And if thisbytearray
can be directly used in serialization, then the only copy you need to make is the serialization itself. Currently, I need to callbytes(my_bytearray_data)
, which makes a copy, and then pass this copy to the serialization. So that's an unnecessary copy of a whole lot of data (in the case of pointclouds).Struct.pack
pads with zeros if a shorter string is given, or cuts the string it if is longer: