serde-rs / bytes

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

Allow `Cow<'a, [u8]>` to be deserialized even when visitor receives owned bytes. #23

Closed zakarumych closed 4 years ago

zakarumych commented 4 years ago

Currently Cow<'a, [u8]> uses &'a [u8] deserialization implementation which would fail when deserializing from any non-binary format. While this is expected for &'a [u8] but Cow<'a, [u8]> can contain Vec<u8> as well and doesn't have to be a pointer into deserializer owned data.

I suggest to write a special visitor for Cow<'a, [u8]> that will create Cow::Borrowed when visit_borrowed_bytes or visit_borrowed_str is called, and Cow::Owned for visit_str, visit_string, visit_bytes or visit_byte_buf.

zakarumych commented 4 years ago

Probably the same must be done for Cow<'a, Bytes>

dtolnay commented 4 years ago

I would accept a PR to fix this.

timotree3 commented 4 years ago

I have code for this and am working on a PR now.