serde-rs / bytes

Wrapper types to enable optimized handling of &[u8] and Vec<u8>
Apache License 2.0
317 stars 37 forks source link

Add support for `[u8; N]` #28

Closed sgued closed 10 months ago

sgued commented 2 years ago

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).

Edit: Since 0504fcb86133c78a75560c907556f1d6031bdf23 already bumped MSRV to 1.56, this drawback doesn't apply anymore.

Xiretza commented 2 years ago

I'd love to see this added, would it be possible for a maintainer to have a look at it?

kamulos commented 2 years ago

I am also really interested in this. Any chance you could have a look @dtolnay ?

elichai commented 2 years ago

Note that unlike serde serde_bytes doesn't have the impl Serialize for [T; 0] problem

markfarnan commented 1 year ago

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 ?

emchristiansen commented 1 year ago

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?

sosthene-nitrokey commented 1 year ago

Given the lack of activity in this PR we published a similar crate specifically for [u8; N] as serde-byte-array

jonasbb commented 1 year ago

The serde_with::Bytes type supports const generic arrays in a bunch of ways.

emchristiansen commented 1 year ago

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?

so-schen commented 11 months ago

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