bytes
bytes copied to clipboard
Add support for `[u8; N]`
Hi.
This PR adds support for constant length byte arrays [u8; N] and adds a ByteArray<const N: usize> wrapper type similar to BytesBuf. This would close #26.
Motivations
Fixed sized array are often used for cryptography and embedded systems. Having efficient support to serialize/deserialize keys would be great.
Parts of the PR requiring discussion
I'm not sure why ByteBuf and Bytes implements PartialEq and PartialOrd for types implementing AsRef<[u8]> and not Borrow<[u8]>. ByteArray implements PartialEq/Ord with types implementing Borrow<[u8; N]> so that it can be compared to itself and to [u8; N]
It might be worth considering adding a new_mut(&mut [u8]) -> Bytes function to Bytes so that the same (unsafe) code is reused for the BorrowMut<Bytes> implementations of ByteArray and ByteBuf.
Drawbacks
Due to the use of const generics, this would require raising the MSRV to 1.53 (which could be lowered to 1.51 by removing the IntoIterator implementation).
I'd love to see this added, would it be possible for a maintainer to have a look at it?
I am also really interested in this. Any chance you could have a look @dtolnay ?
Note that unlike serde serde_bytes doesn't have the impl Serialize for [T; 0] problem
This is blocking downstream feature in Avro/Rust for Fixed Types. I've tested it, and it works fine for my use case with Avro.
Can this be merged soon please ?
Haha, I created a pull request with almost exactly the same name, not realizing that this existed (I'll delete it now). But, can we please get this merged in?
Given the lack of activity in this PR we published a similar crate specifically for [u8; N] as serde-byte-array
The serde_with::Bytes type supports const generic arrays in a bunch of ways.
Does anyone know why this is taking so long? I.e., should I wait for a fix or should I switch to a different crate?
in case anyone looking for drop-in replacement with all bytes/slice support in one crate, can use this https://github.com/so-schen/serde-bytes-ng