serde-rs / bytes

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

Consider not using specialization even after it lands #8

Closed eternaleye closed 5 years ago

eternaleye commented 6 years ago

The README has this line:

Rust support for specialization is being tracked in rust-lang/rust#31844. Once it lands in the stable compiler Serde will be deprecating these wrapper types in favor of optimizing &[u8] and Vec<u8> out of the box.

Serialization in Go has taken a similar tack, always serialising []uint8 as []bytes, but this has significant knock-on effects which can be quite painful.

As an example, serializing []int8 (&[i8]) to JSON results in an array of numbers, while []uint8 (&[u8]) results in a base64 blob (credit @edef1c for the example).

Taking this choice away from the user, by having specialization rather than explicit wrapper types, may make it difficult to impossible to comply with schemas that originate somewhere other than the Rust project using Serde - in Go, the solution is an expensive promotion to 16-bit integers, which costs a copy.

dtolnay commented 6 years ago

Thanks for opening this. I agree. I have become much more skeptical of specialization since writing that. I removed that part in https://github.com/serde-rs/bytes/commit/cef654193e25d961471275eb3ad434963f3764c3.

eternaleye commented 6 years ago

Thanks!