Copying unaligned bytes

As mentioned in (De)serialization, bytes are copied when they are turned into a type T due to unaligned bytes being returned from database backends.

Using a regular reference cast results in an improperly aligned type T; such a type even existing causes undefined behavior. In our case, bytemuck saves us by panicking before this occurs.

Thus, when using cuprate_database's database traits, an owned T is returned.

This is doubly unfortunately for &[u8] as this does not even need deserialization.

For example, StorableVec could have been this:

#![allow(unused)]
fn main() {
enum StorableBytes<'a, T: Storable> {
    Owned(T),
    Ref(&'a T),
}
}

but this would require supporting types that must be copied regardless with the occasional &[u8] that can be returned without casting. This was hard to do so in a generic way, thus all [u8]'s are copied and returned as owned StorableVecs.

This is a tradeoff cuprate_database takes as:

  • bytemuck::pod_read_unaligned is cheap enough
  • The main API, service, needs to return owned value anyway
  • Having no references removes a lot of lifetime complexity

The alternative is somehow fixing the alignment issues in the backends mentioned previously.

Last change: 2024-10-02, commit: a003e05