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

types: serialize: constrain the new serialization traits to make them easier and safer to use #855

Merged
merged 3 commits into from
Nov 24, 2023

Commits on Nov 20, 2023

  1. types: introduce read_value

    Introduce the `read_value` function which is able to read a [value], as
    specified in the CQL protocol. It will be used in the next commit, in
    order to make the interface of the SerializedValue iterators more
    correct.
    piodul committed Nov 20, 2023
    Configuration menu
    Copy the full SHA
    7767f17 View commit details
    Browse the repository at this point in the history
  2. frame: adjust serialized values iterator to return RawValue

    Currently, the SerializedValues' `iter()` method treats both null and
    unset values as None, and `iter_name_value_pairs()` just assumes that
    values are never null/unset and panics if they are.
    
    Make the interface more correct by adjusting both methods to return
    RawValue. The iterators will be used in the next commit to implement the
    fallback that allows to implement `SerializeRow`/`SerializeCql` via
    legacy `ValueList`/`Value` traits.
    piodul committed Nov 20, 2023
    Configuration menu
    Copy the full SHA
    5e544c9 View commit details
    Browse the repository at this point in the history

Commits on Nov 23, 2023

  1. types: serialize: introduce new helpers for writing values and adjust…

    … interfaces
    
    Currently, `SerializeRow` and `SerializeCql` traits are just given a
    mutable reference to a Vec<u8> and asked to append their CQL
    representation to the end. While simple, there are some issues with the
    interface:
    
    - The serialize method has access to the serialized representation of
      the values that were appended before it. It's not necessary for a
      correct implementation to have access to it.
    - Implementors technically can append any byte sequence to the end, but
      actually are expected to produce a CQL [value] containing the
      serialized value.
    
    While the `SerializeRow` and `SerializeCql` traits are not generally
    meant to be manually implemented by the users, we can make the interface
    easier to use and harder to misuse by making it append-only, restricting
    what the users are allowed to append and requiring the users to append
    anything by using a dash of type-level magic.
    
    Introduce `RowWriter` and `CellWriter` traits which satisfy the above
    wishes and constraints, and pass them instead of Vec<u8> in
    `SerializeRow` and `SerializeCql`.
    
    The new traits have two implementations - a Vec<u8> backed one that
    actually appends the bytes given to it, and a usize-backed one which
    just measures the length of the output without writing anything. Passing
    the latter before doing the actual serialization will allow to
    preallocate the right amount of bytes and then serialize without
    reallocations. It should be measured whether the reallocation cost
    always outweighs the calculation cost before implementing this
    optimization.
    piodul committed Nov 23, 2023
    Configuration menu
    Copy the full SHA
    29a37b4 View commit details
    Browse the repository at this point in the history