[Feature] Zero-copy, zero-allocation decoding of zvariant
The layout of D-bus messages is very friendly towards zero-copy decoding: since everything is already properly aligned, you can access the data in memory directly instead of allocating and copying it to the outside.
This can be done without major changes to the API since this is only relevant for strings and arrays, not primitive types. All you'd need to add is converting zvariant::Array into a slice instead of a Vec and zvariant::String into an &str
.
For &str
there is already a function to perform the checks and convert: https://doc.rust-lang.org/std/str/fn.from_utf8.html
All array types except bool
are covered by try_cast()
methods from bytemuck
crate: https://docs.rs/bytemuck/1.2.0/bytemuck/
&[bool]
has additional validity invariants (only values of 0 or 1 are allowed) and will require a traversal for validation, and a manual unsafe cast.
The only tricky part is that you'd need to align the entirety of zvariant heap allocation to the largest possible alignment requirement (8 bytes for u64) so you might have to add up to 7 padding bytes before the zvariant itself even begins.