mz_repr/fixed_length.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Use of this software is governed by the Business Source License
4// included in the LICENSE file.
5//
6// As of the Change Date specified in that file, in accordance with
7// the Business Source License, use of this software will be governed
8// by the Apache License, Version 2.0.
9
10//! Provides abstractions for types that can append their datums to a vector.
11//! `Row` is the most obvious implementor, but other trace types that may use more advanced
12//! representations only need to commit to implementing this trait.
13
14use crate::{Datum, Row, RowArena};
15
16/// A helper trait for types that can append their datums to a `Vec<Datum>`.
17pub trait ExtendDatums {
18 /// Append datums to `target` — all of them (`max == None`) or at most
19 /// `max` (`Some`) — branching on representation ONCE rather than once per
20 /// datum.
21 ///
22 /// `arena` provides storage for data that the appended datums borrow from
23 /// but that does not already live in `self`. Representations backed directly
24 /// by packed bytes (`Row`, row-spine's `DatumSeq`) ignore it, while
25 /// compressed representations can decode into it so the resulting
26 /// `Datum<'a>`s borrow from the arena rather than from `self`. Implementors
27 /// should push directly into `target` in a tight loop, branching on their
28 /// representation once rather than once per datum.
29 fn extend_datums<'a>(
30 &'a self,
31 arena: &'a RowArena,
32 target: &mut Vec<Datum<'a>>,
33 max: Option<usize>,
34 );
35}
36
37impl<T: ExtendDatums + ?Sized> ExtendDatums for &T {
38 #[inline]
39 fn extend_datums<'a>(
40 &'a self,
41 arena: &'a RowArena,
42 target: &mut Vec<Datum<'a>>,
43 max: Option<usize>,
44 ) {
45 // Forward to T's impl so an override isn't lost behind a reference.
46 (**self).extend_datums(arena, target, max)
47 }
48}
49
50// Identity implementation for Row, whose datums borrow directly from its bytes.
51impl ExtendDatums for Row {
52 #[inline]
53 fn extend_datums<'a>(
54 &'a self,
55 _arena: &'a RowArena,
56 target: &mut Vec<Datum<'a>>,
57 max: Option<usize>,
58 ) {
59 match max {
60 Some(max) => target.extend(self.iter().take(max)),
61 None => target.extend(self.iter()),
62 }
63 }
64}