Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Bytes type for more efficient byte sequences #277

Merged
merged 4 commits into from
Mar 12, 2021
Merged

Add a Bytes type for more efficient byte sequences #277

merged 4 commits into from
Mar 12, 2021

Conversation

jonasbb
Copy link
Owner

@jonasbb jonasbb commented Mar 11, 2021

The Bytes type is heavily inspired by serde_bytes and ports it to the
serde_as system.

#[serde_as(as = "Bytes")]
value: Vec<u8>,

Compared to serde_bytes these improvements are available

  1. Integration with the serde_as annotation.
    /cc Working with more complex types is difficult serde-rs/bytes#14
  2. Implementation for arrays of arbitrary size (Rust 1.51+).
    /cc Implement (de-)serializing for [u8; N] serde-rs/bytes#26

@jonasbb jonasbb force-pushed the bytes branch 2 times, most recently from 182b0a9 to d2c07b4 Compare March 11, 2021 23:40
@codecov
Copy link

codecov bot commented Mar 11, 2021

Codecov Report

Merging #277 (1b9c5a6) into master (5f68bb2) will decrease coverage by 2.02%.
The diff coverage is 0.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #277      +/-   ##
==========================================
- Coverage   76.70%   74.68%   -2.03%     
==========================================
  Files          40       40              
  Lines        2142     2200      +58     
==========================================
  Hits         1643     1643              
- Misses        499      557      +58     
Impacted Files Coverage Δ
src/de/const_arrays.rs 0.00% <0.00%> (ø)
src/de/impls.rs 59.73% <0.00%> (-20.45%) ⬇️
src/lib.rs 75.00% <ø> (ø)
src/ser/const_arrays.rs 0.00% <0.00%> (ø)
src/ser/impls.rs 90.38% <0.00%> (-7.54%) ⬇️
tests/serde_as.rs 100.00% <ø> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5f68bb2...1b9c5a6. Read the comment docs.

The `Bytes` type is heavily inspired by `serde_bytes` and ports it to the
serde_as system.

```rust
value: Vec<u8>,
```

Compared to `serde_bytes` these improvements are available

1. Integration with the `serde_as` annotation.
    /cc serde-rs/bytes#14
2. Implementation for arrays of arbitrary size (Rust 1.51+).
    /cc serde-rs/bytes#26
@jonasbb
Copy link
Owner Author

jonasbb commented Mar 12, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Mar 12, 2021

Build succeeded:

@bors bors bot merged commit a1b8cda into master Mar 12, 2021
@bors bors bot deleted the bytes branch March 12, 2021 21:00
bors bot added a commit that referenced this pull request Jun 14, 2021
325: Bytes const size borrowed arrays r=jonasbb a=matix2267

Currently `Bytes` (added in #277) supports some const size array types (e.g. `[u8; N]` and `Box<[u8; N]>`)
but not others (e.g. `&[u8; N]` and `Cow<'_, [u8; N]>`).

This pull request implements `Bytes` support for `&[u8; N]` and `Cow<'_, [u8; N]>`
This bring parity between dynamic and const sized types.

### Before:

| Sizing  | Owned          | Borrowed      | Cow                   | Box               |
| ------- | -------------- | ------------- | --------------------- | ----------------- |
| Const   | ✅ `[u8; N]`   | ❌ `&[u8; N]` | ❌ `Cow<'_, [u8; N]>` | ✅ `Box<[u8; N]>` |
| Dynamic | ✅ `Vec<u8>`\* | ✅ `&[u8]`    | ✅ `Cow<'_, [u8]>`    | ✅ `Box<[u8]>`    |

### After:

| Sizing  | Owned          | Borrowed      | Cow                   | Box               |
| ------- | -------------- | ------------- | --------------------- | ----------------- |
| Const   | ✅ `[u8; N]`   | ✅ `&[u8; N]` | ✅ `Cow<'_, [u8; N]>` | ✅ `Box<[u8; N]>` |
| Dynamic | ✅ `Vec<u8>`\* | ✅ `&[u8]`    | ✅ `Cow<'_, [u8]>`    | ✅ `Box<[u8]>`    |

I've also added a separate commit with an example of using fully borrowed types, but that example only does serialization
because RON doesn't support 0-copy deserialization for byte arrays (as it needs to decode them from base64).
Because of that I'm not sure if this example is useful so feel free to merge only the main commit.

<sub>\* Technically `[u8]` corresponds to `[u8; N]` but `[u8]` is an un`Sized` type and cannot be
directly used in structs.</sub>

Co-authored-by: matix2267 <[email protected]>
bors bot added a commit that referenced this pull request Dec 24, 2022
536: Mention "Bytes" on README.md and lib.rs r=jonasbb a=ryoqun

I think #277 can be more promoted. :)

As I wrote the current status of serialization of const generics like bytes (solana-labs/solana#29196 (comment)), i think `"Bytes"` is best solution so far.

`@jonasbb` hey, this functionary saved us. however it took a while to spot this. I think `Bytes` can be promoted. :)

Co-authored-by: Ryo Onodera <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant