Skip to main content

differential_dataflow/trace/implementations/
mod.rs

1//! Implementations of `Trace` and associated traits.
2//!
3//! The `Trace` trait provides access to an ordered collection of `(key, val, time, diff)` tuples, but
4//! there is substantial flexibility in implementations of this trait. Depending on characteristics of
5//! the data, we may wish to represent the data in different ways. This module contains several of these
6//! implementations, and combiners for merging the results of different traces.
7//!
8//! As examples of implementations,
9//!
10//! *  The `trie` module is meant to represent general update tuples, with no particular assumptions made
11//!    about their contents. It organizes the data first by key, then by val, and then leaves the rest
12//!    in an unordered pile.
13//!
14//! *  The `keys` module is meant for collections whose value type is `()`, which is to say there is no
15//!    (key, val) structure on the records; all of them are just viewed as "keys".
16//!
17//! *  The `time` module is meant for collections with a single time value. This can remove repetition
18//!    from the representation, at the cost of requiring more instances and run-time merging.
19//!
20//! *  The `base` module is meant for collections with a single time value equivalent to the least time.
21//!    These collections must always accumulate to non-negative collections, and as such we can indicate
22//!    the frequency of an element by its multiplicity. This removes both the time and weight from the
23//!    representation, but is only appropriate for a subset (often substantial) of the data.
24//!
25//! Each of these representations is best suited for different data, but they can be combined to get the
26//! benefits of each, as appropriate. There are several `Cursor` combiners, `CursorList` and `CursorPair`,
27//! for homogeneous and inhomogeneous cursors, respectively.
28//!
29//! #Musings
30//!
31//! What is less clear is how to transfer updates between the representations at merge time in a tasteful
32//! way. Perhaps we could put an ordering on the representations, each pair with a dominant representation,
33//! and part of merging the latter filters updates into the former. Although back and forth might be
34//! appealing, more thinking is required to negotiate all of these policies.
35//!
36//! One option would be to require the layer builder to handle these smarts. Merging is currently done by
37//! the layer as part of custom code, but we could make it simply be "iterate through cursor, push results
38//! into 'ordered builder'". Then the builder would be bright enough to emit a "batch" for the composite
39//! trace, rather than just a batch of the type merged.
40
41pub mod spine_fueled;
42
43pub mod merge_batcher;
44pub mod ord_neu;
45pub mod chunker;
46
47// Opinionated takes on default spines.
48pub use self::chunker::ContainerChunker;
49pub use self::ord_neu::OrdValSpine as ValSpine;
50pub use self::ord_neu::OrdValBatcher as ValBatcher;
51pub use self::ord_neu::RcOrdValBuilder as ValBuilder;
52pub use self::ord_neu::OrdKeySpine as KeySpine;
53pub use self::ord_neu::OrdKeyBatcher as KeyBatcher;
54pub use self::ord_neu::RcOrdKeyBuilder as KeyBuilder;
55
56use std::convert::TryInto;
57
58use serde::{Deserialize, Serialize};
59use timely::container::{DrainContainer, PushInto};
60use timely::progress::Timestamp;
61
62use crate::lattice::Lattice;
63use crate::difference::Semigroup;
64
65/// A type that names constituent update types.
66pub trait Update {
67    /// Key by which data are grouped.
68    type Key: Ord + Clone + 'static;
69    /// Values associated with the key.
70    type Val: Ord + Clone + 'static;
71    /// Time at which updates occur.
72    type Time: Lattice + timely::progress::Timestamp;
73    /// Way in which updates occur.
74    type Diff: Ord + Semigroup + 'static;
75}
76
77impl<K,V,T,R> Update for ((K, V), T, R)
78where
79    K: Ord+Clone+'static,
80    V: Ord+Clone+'static,
81    T: Lattice+timely::progress::Timestamp,
82    R: Ord+Semigroup+'static,
83{
84    type Key = K;
85    type Val = V;
86    type Time = T;
87    type Diff = R;
88}
89
90/// A type with opinions on how updates should be laid out.
91pub trait Layout {
92    /// Container for update keys.
93    type KeyContainer: BatchContainer;
94    /// Container for update vals.
95    type ValContainer: BatchContainer;
96    /// Container for times.
97    type TimeContainer: BatchContainer<Owned: Lattice + timely::progress::Timestamp>;
98    /// Container for diffs.
99    type DiffContainer: BatchContainer<Owned: Semigroup + 'static>;
100    /// Container for offsets.
101    type OffsetContainer: for<'a> BatchContainer<ReadItem<'a> = usize>;
102}
103
104/// A type bearing a layout.
105pub trait WithLayout {
106    /// The layout.
107    type Layout: Layout;
108}
109
110/// Automatically implemented trait for types with layouts.
111pub trait LayoutExt : WithLayout<Layout: Layout<KeyContainer = Self::KeyContainer, ValContainer = Self::ValContainer, TimeContainer = Self::TimeContainer, DiffContainer = Self::DiffContainer>> {
112    /// Alias for an borrowed key of a layout.
113    type Key<'a>: Copy + Ord;
114    /// Alias for an owned val of a layout.
115    type ValOwn: Clone + Ord;
116    /// Alias for an borrowed val of a layout.
117    type Val<'a>: Copy + Ord;
118    /// Alias for an owned time of a layout.
119    type Time: Lattice + timely::progress::Timestamp;
120    /// Alias for an borrowed time of a layout.
121    type TimeGat<'a>: Copy + Ord;
122    /// Alias for an owned diff of a layout.
123    type Diff: Semigroup + 'static;
124    /// Alias for an borrowed diff of a layout.
125    type DiffGat<'a>: Copy + Ord;
126
127    /// Container for update keys.
128    type KeyContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Key<'a>>;
129    /// Container for update vals.
130    type ValContainer: for<'a> BatchContainer<ReadItem<'a> = Self::Val<'a>, Owned = Self::ValOwn>;
131    /// Container for times.
132    type TimeContainer: for<'a> BatchContainer<ReadItem<'a> = Self::TimeGat<'a>, Owned = Self::Time>;
133    /// Container for diffs.
134    type DiffContainer: for<'a> BatchContainer<ReadItem<'a> = Self::DiffGat<'a>, Owned = Self::Diff>;
135
136    /// Construct an owned val from a reference.
137    fn owned_val(val: Self::Val<'_>) -> Self::ValOwn;
138    /// Construct an owned time from a reference.
139    fn owned_time(time: Self::TimeGat<'_>) -> Self::Time;
140    /// Construct an owned diff from a reference.
141    fn owned_diff(diff: Self::DiffGat<'_>) -> Self::Diff;
142
143    /// Clones a reference time onto an owned time.
144    fn clone_time_onto(time: Self::TimeGat<'_>, onto: &mut Self::Time);
145}
146
147impl<L: WithLayout> LayoutExt for L {
148    type Key<'a> = <<L::Layout as Layout>::KeyContainer as BatchContainer>::ReadItem<'a>;
149    type ValOwn = <<L::Layout as Layout>::ValContainer as BatchContainer>::Owned;
150    type Val<'a> = <<L::Layout as Layout>::ValContainer as BatchContainer>::ReadItem<'a>;
151    type Time = <<L::Layout as Layout>::TimeContainer as BatchContainer>::Owned;
152    type TimeGat<'a> = <<L::Layout as Layout>::TimeContainer as BatchContainer>::ReadItem<'a>;
153    type Diff = <<L::Layout as Layout>::DiffContainer as BatchContainer>::Owned;
154    type DiffGat<'a> = <<L::Layout as Layout>::DiffContainer as BatchContainer>::ReadItem<'a>;
155
156    type KeyContainer = <L::Layout as Layout>::KeyContainer;
157    type ValContainer = <L::Layout as Layout>::ValContainer;
158    type TimeContainer = <L::Layout as Layout>::TimeContainer;
159    type DiffContainer = <L::Layout as Layout>::DiffContainer;
160
161    #[inline(always)] fn owned_val(val: Self::Val<'_>) -> Self::ValOwn { <Self::Layout as Layout>::ValContainer::into_owned(val) }
162    #[inline(always)] fn owned_time(time: Self::TimeGat<'_>) -> Self::Time { <Self::Layout as Layout>::TimeContainer::into_owned(time) }
163    #[inline(always)] fn owned_diff(diff: Self::DiffGat<'_>) -> Self::Diff { <Self::Layout as Layout>::DiffContainer::into_owned(diff) }
164    #[inline(always)] fn clone_time_onto(time: Self::TimeGat<'_>, onto: &mut Self::Time) { <Self::Layout as Layout>::TimeContainer::clone_onto(time, onto) }
165
166}
167
168// An easy way to provide an explicit layout: as a 5-tuple.
169// Valuable when one wants to perform layout surgery.
170impl<KC, VC, TC, DC, OC> Layout for (KC, VC, TC, DC, OC)
171where
172    KC: BatchContainer,
173    VC: BatchContainer,
174    TC: BatchContainer<Owned: Lattice + timely::progress::Timestamp>,
175    DC: BatchContainer<Owned: Semigroup>,
176    OC: for<'a> BatchContainer<ReadItem<'a> = usize>,
177{
178    type KeyContainer = KC;
179    type ValContainer = VC;
180    type TimeContainer = TC;
181    type DiffContainer = DC;
182    type OffsetContainer = OC;
183}
184
185/// Aliases for types provided by the containers within a `Layout`.
186///
187/// For clarity, prefer `use layout;` and then `layout::Key<L>` to retain the layout context.
188pub mod layout {
189    use crate::trace::implementations::{BatchContainer, Layout};
190
191    /// Alias for an owned key of a layout.
192    pub type Key<L> = <<L as Layout>::KeyContainer as BatchContainer>::Owned;
193    /// Alias for an borrowed key of a layout.
194    pub type KeyRef<'a, L> = <<L as Layout>::KeyContainer as BatchContainer>::ReadItem<'a>;
195    /// Alias for an owned val of a layout.
196    pub type Val<L> = <<L as Layout>::ValContainer as BatchContainer>::Owned;
197    /// Alias for an borrowed val of a layout.
198    pub type ValRef<'a, L> = <<L as Layout>::ValContainer as BatchContainer>::ReadItem<'a>;
199    /// Alias for an owned time of a layout.
200    pub type Time<L> = <<L as Layout>::TimeContainer as BatchContainer>::Owned;
201    /// Alias for an borrowed time of a layout.
202    pub type TimeRef<'a, L> = <<L as Layout>::TimeContainer as BatchContainer>::ReadItem<'a>;
203    /// Alias for an owned diff of a layout.
204    pub type Diff<L> = <<L as Layout>::DiffContainer as BatchContainer>::Owned;
205    /// Alias for an borrowed diff of a layout.
206    pub type DiffRef<'a, L> = <<L as Layout>::DiffContainer as BatchContainer>::ReadItem<'a>;
207}
208
209/// A layout that uses vectors
210pub struct Vector<U: Update> {
211    phantom: std::marker::PhantomData<U>,
212}
213
214impl<U: Update<Diff: Ord>> Layout for Vector<U> {
215    type KeyContainer = Vec<U::Key>;
216    type ValContainer = Vec<U::Val>;
217    type TimeContainer = Vec<U::Time>;
218    type DiffContainer = Vec<U::Diff>;
219    type OffsetContainer = OffsetList;
220}
221
222/// A list of unsigned integers that uses `u32` elements as long as they are small enough, and switches to `u64` once they are not.
223#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Serialize, Deserialize)]
224pub struct OffsetList {
225    /// Length of a prefix of zero elements.
226    pub zero_prefix: usize,
227    /// Offsets that fit within a `u32`.
228    pub smol: Vec<u32>,
229    /// Offsets that either do not fit in a `u32`, or are inserted after some offset that did not fit.
230    pub chonk: Vec<u64>,
231}
232
233impl std::fmt::Debug for OffsetList {
234    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235        f.debug_list().entries(self.into_iter()).finish()
236    }
237}
238
239impl OffsetList {
240    /// Allocate a new list with a specified capacity.
241    pub fn with_capacity(cap: usize) -> Self {
242        Self {
243            zero_prefix: 0,
244            smol: Vec::with_capacity(cap),
245            chonk: Vec::new(),
246        }
247    }
248    /// Inserts the offset, as a `u32` if that is still on the table.
249    pub fn push(&mut self, offset: usize) {
250        if self.smol.is_empty() && self.chonk.is_empty() && offset == 0 {
251            self.zero_prefix += 1;
252        }
253        else if self.chonk.is_empty() {
254            if let Ok(smol) = offset.try_into() {
255                self.smol.push(smol);
256            }
257            else {
258                self.chonk.push(offset.try_into().unwrap())
259            }
260        }
261        else {
262            self.chonk.push(offset.try_into().unwrap())
263        }
264    }
265    /// Like `std::ops::Index`, which we cannot implement as it must return a `&usize`.
266    pub fn index(&self, index: usize) -> usize {
267        if index < self.zero_prefix {
268            0
269        }
270        else if index - self.zero_prefix < self.smol.len() {
271            self.smol[index - self.zero_prefix].try_into().unwrap()
272        }
273        else {
274            self.chonk[index - self.zero_prefix - self.smol.len()].try_into().unwrap()
275        }
276    }
277    /// The number of offsets in the list.
278    pub fn len(&self) -> usize {
279        self.zero_prefix + self.smol.len() + self.chonk.len()
280    }
281}
282
283impl<'a> IntoIterator for &'a OffsetList {
284    type Item = usize;
285    type IntoIter = OffsetListIter<'a>;
286
287    fn into_iter(self) -> Self::IntoIter {
288        OffsetListIter {list: self, index: 0 }
289    }
290}
291
292/// An iterator for [`OffsetList`].
293pub struct OffsetListIter<'a> {
294    list: &'a OffsetList,
295    index: usize,
296}
297
298impl<'a> Iterator for OffsetListIter<'a> {
299    type Item = usize;
300
301    fn next(&mut self) -> Option<Self::Item> {
302        if self.index < self.list.len() {
303            let res = Some(self.list.index(self.index));
304            self.index += 1;
305            res
306        } else {
307            None
308        }
309    }
310}
311
312impl PushInto<usize> for OffsetList {
313    fn push_into(&mut self, item: usize) {
314        self.push(item);
315    }
316}
317
318impl BatchContainer for OffsetList {
319    type Owned = usize;
320    type ReadItem<'a> = usize;
321
322    #[inline(always)]
323    fn into_owned<'a>(item: Self::ReadItem<'a>) -> Self::Owned { item }
324    #[inline(always)]
325    fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> { item }
326
327    fn push_ref(&mut self, item: Self::ReadItem<'_>) { self.push_into(item) }
328    fn push_own(&mut self, item: &Self::Owned) { self.push_into(*item) }
329
330    fn clear(&mut self) { self.zero_prefix = 0; self.smol.clear(); self.chonk.clear(); }
331
332    fn with_capacity(size: usize) -> Self {
333        Self::with_capacity(size)
334    }
335
336    fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
337        Self::with_capacity(cont1.len() + cont2.len())
338    }
339
340    fn index(&self, index: usize) -> Self::ReadItem<'_> {
341        self.index(index)
342    }
343
344    fn len(&self) -> usize {
345        self.len()
346    }
347}
348
349/// Behavior to split an update into principal components.
350pub trait BuilderInput<K: BatchContainer, V: BatchContainer>: DrainContainer + Sized {
351    /// Key portion
352    type Key<'a>: Ord;
353    /// Value portion
354    type Val<'a>: Ord;
355    /// Time
356    type Time;
357    /// Diff
358    type Diff;
359
360    /// Split an item into separate parts.
361    fn into_parts<'a>(item: Self::Item<'a>) -> (Self::Key<'a>, Self::Val<'a>, Self::Time, Self::Diff);
362
363    /// Test that the key equals a key in the layout's key container.
364    fn key_eq(this: &Self::Key<'_>, other: K::ReadItem<'_>) -> bool;
365
366    /// Test that the value equals a key in the layout's value container.
367    fn val_eq(this: &Self::Val<'_>, other: V::ReadItem<'_>) -> bool;
368
369    /// Count the number of distinct keys, (key, val) pairs, and total updates.
370    fn key_val_upd_counts(chain: &[Self]) -> (usize, usize, usize);
371}
372
373impl<K,KBC,V,VBC,T,R> BuilderInput<KBC, VBC> for Vec<((K, V), T, R)>
374where
375    K: Ord + Clone + 'static,
376    KBC: for<'a> BatchContainer<ReadItem<'a>: PartialEq<&'a K>>,
377    V: Ord + Clone + 'static,
378    VBC: for<'a> BatchContainer<ReadItem<'a>: PartialEq<&'a V>>,
379    T: Timestamp + Lattice + 'static,
380    R: Ord + Semigroup + 'static,
381{
382    type Key<'a> = K;
383    type Val<'a> = V;
384    type Time = T;
385    type Diff = R;
386
387    fn into_parts<'a>(((key, val), time, diff): Self::Item<'a>) -> (Self::Key<'a>, Self::Val<'a>, Self::Time, Self::Diff) {
388        (key, val, time, diff)
389    }
390
391    fn key_eq(this: &K, other: KBC::ReadItem<'_>) -> bool {
392        KBC::reborrow(other) == this
393    }
394
395    fn val_eq(this: &V, other: VBC::ReadItem<'_>) -> bool {
396        VBC::reborrow(other) == this
397    }
398
399    fn key_val_upd_counts(chain: &[Self]) -> (usize, usize, usize) {
400        let mut keys = 0;
401        let mut vals = 0;
402        let mut upds = 0;
403        let mut prev_keyval = None;
404        for link in chain.iter() {
405            for ((key, val), _, _) in link.iter() {
406                if let Some((p_key, p_val)) = prev_keyval {
407                    if p_key != key {
408                        keys += 1;
409                        vals += 1;
410                    } else if p_val != val {
411                        vals += 1;
412                    }
413                } else {
414                    keys += 1;
415                    vals += 1;
416                }
417                upds += 1;
418                prev_keyval = Some((key, val));
419            }
420        }
421        (keys, vals, upds)
422    }
423}
424
425pub use self::containers::{BatchContainer, SliceContainer};
426
427/// Containers for data that resemble `Vec<T>`, with leaner implementations.
428pub mod containers {
429
430    use timely::container::PushInto;
431
432    /// A general-purpose container resembling `Vec<T>`.
433    pub trait BatchContainer: 'static {
434        /// An owned instance of `Self::ReadItem<'_>`.
435        type Owned: Clone + Ord;
436
437        /// The type that can be read back out of the container.
438        type ReadItem<'a>: Copy + Ord;
439
440
441        /// Conversion from an instance of this type to the owned type.
442        #[must_use]
443        fn into_owned<'a>(item: Self::ReadItem<'a>) -> Self::Owned;
444        /// Clones `self` onto an existing instance of the owned type.
445        #[inline(always)]
446        fn clone_onto<'a>(item: Self::ReadItem<'a>, other: &mut Self::Owned) {
447            *other = Self::into_owned(item);
448        }
449
450        /// Push an item into this container
451        fn push_ref(&mut self, item: Self::ReadItem<'_>);
452        /// Push an item into this container
453        fn push_own(&mut self, item: &Self::Owned);
454
455        /// Clears the container. May not release resources.
456        fn clear(&mut self);
457
458        /// Creates a new container with sufficient capacity.
459        fn with_capacity(size: usize) -> Self;
460        /// Creates a new container with sufficient capacity.
461        fn merge_capacity(cont1: &Self, cont2: &Self) -> Self;
462
463        /// Converts a read item into one with a narrower lifetime.
464        fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b>;
465
466        /// Reference to the element at this position.
467        fn index(&self, index: usize) -> Self::ReadItem<'_>;
468
469        /// Reference to the element at this position, if it exists.
470        fn get(&self, index: usize) -> Option<Self::ReadItem<'_>> {
471            if index < self.len() {
472                Some(self.index(index))
473            }
474            else { None }
475        }
476
477        /// Number of contained elements
478        fn len(&self) -> usize;
479        /// Returns the last item if the container is non-empty.
480        fn last(&self) -> Option<Self::ReadItem<'_>> {
481            if self.len() > 0 {
482                Some(self.index(self.len()-1))
483            }
484            else {
485                None
486            }
487        }
488        /// Indicates if the length is zero.
489        fn is_empty(&self) -> bool { self.len() == 0 }
490
491        /// Reports the number of elements satisfying the predicate.
492        ///
493        /// This methods *relies strongly* on the assumption that the predicate
494        /// stays false once it becomes false, a joint property of the predicate
495        /// and the layout of `Self. This allows `advance` to use exponential search to
496        /// count the number of elements in time logarithmic in the result.
497        fn advance<F: for<'a> Fn(Self::ReadItem<'a>)->bool>(&self, start: usize, end: usize, function: F) -> usize {
498
499            let small_limit = 8;
500
501            // Exponential search if the answer isn't within `small_limit`.
502            if end > start + small_limit && function(self.index(start + small_limit)) {
503
504                // start with no advance
505                let mut index = small_limit + 1;
506                if start + index < end && function(self.index(start + index)) {
507
508                    // advance in exponentially growing steps.
509                    let mut step = 1;
510                    while start + index + step < end && function(self.index(start + index + step)) {
511                        index += step;
512                        step <<= 1;
513                    }
514
515                    // advance in exponentially shrinking steps.
516                    step >>= 1;
517                    while step > 0 {
518                        if start + index + step < end && function(self.index(start + index + step)) {
519                            index += step;
520                        }
521                        step >>= 1;
522                    }
523
524                    index += 1;
525                }
526
527                index
528            }
529            else {
530                let limit = std::cmp::min(end, start + small_limit);
531                (start .. limit).filter(|x| function(self.index(*x))).count()
532            }
533        }
534    }
535
536    // All `T: Clone` also implement `ToOwned<Owned = T>`, but without the constraint Rust
537    // struggles to understand why the owned type must be `T` (i.e. the one blanket impl).
538    impl<T: Ord + Clone + 'static> BatchContainer for Vec<T> {
539        type Owned = T;
540        type ReadItem<'a> = &'a T;
541
542        #[inline(always)] fn into_owned<'a>(item: Self::ReadItem<'a>) -> Self::Owned { item.clone() }
543        #[inline(always)] fn clone_onto<'a>(item: Self::ReadItem<'a>, other: &mut Self::Owned) { other.clone_from(item); }
544
545        fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> { item }
546
547        fn push_ref(&mut self, item: Self::ReadItem<'_>) { self.push_into(item) }
548        fn push_own(&mut self, item: &Self::Owned) { self.push_into(item.clone()) }
549
550        fn clear(&mut self) { self.clear() }
551
552        fn with_capacity(size: usize) -> Self {
553            Vec::with_capacity(size)
554        }
555        fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
556            Vec::with_capacity(cont1.len() + cont2.len())
557        }
558        fn index(&self, index: usize) -> Self::ReadItem<'_> {
559            &self[index]
560        }
561        fn get(&self, index: usize) -> Option<Self::ReadItem<'_>> {
562            <[T]>::get(self, index)
563        }
564        fn len(&self) -> usize {
565            self[..].len()
566        }
567    }
568
569    /// A container that accepts slices `[B::Item]`.
570    pub struct SliceContainer<B> {
571        /// Offsets that bound each contained slice.
572        ///
573        /// The length will be one greater than the number of contained slices,
574        /// starting with zero and ending with `self.inner.len()`.
575        offsets: Vec<usize>,
576        /// An inner container for sequences of `B` that dereferences to a slice.
577        inner: Vec<B>,
578    }
579
580    impl<B: Ord + Clone + 'static> PushInto<&[B]> for SliceContainer<B> {
581        fn push_into(&mut self, item: &[B]) {
582            for x in item.iter() {
583                self.inner.push_into(x);
584            }
585            self.offsets.push(self.inner.len());
586        }
587    }
588
589    impl<B: Ord + Clone + 'static> PushInto<&Vec<B>> for SliceContainer<B> {
590        fn push_into(&mut self, item: &Vec<B>) {
591            self.push_into(&item[..]);
592        }
593    }
594
595    impl<B> BatchContainer for SliceContainer<B>
596    where
597        B: Ord + Clone + Sized + 'static,
598    {
599        type Owned = Vec<B>;
600        type ReadItem<'a> = &'a [B];
601
602        #[inline(always)] fn into_owned<'a>(item: Self::ReadItem<'a>) -> Self::Owned { item.to_vec() }
603        #[inline(always)] fn clone_onto<'a>(item: Self::ReadItem<'a>, other: &mut Self::Owned) { other.clone_from_slice(item); }
604
605        fn reborrow<'b, 'a: 'b>(item: Self::ReadItem<'a>) -> Self::ReadItem<'b> { item }
606
607        fn push_ref(&mut self, item: Self::ReadItem<'_>) { self.push_into(item) }
608        fn push_own(&mut self, item: &Self::Owned) { self.push_into(item) }
609
610        fn clear(&mut self) {
611            self.offsets.clear();
612            self.offsets.push(0);
613            self.inner.clear();
614        }
615
616        fn with_capacity(size: usize) -> Self {
617            let mut offsets = Vec::with_capacity(size + 1);
618            offsets.push(0);
619            Self {
620                offsets,
621                inner: Vec::with_capacity(size),
622            }
623        }
624        fn merge_capacity(cont1: &Self, cont2: &Self) -> Self {
625            let mut offsets = Vec::with_capacity(cont1.inner.len() + cont2.inner.len() + 1);
626            offsets.push(0);
627            Self {
628                offsets,
629                inner: Vec::with_capacity(cont1.inner.len() + cont2.inner.len()),
630            }
631        }
632        fn index(&self, index: usize) -> Self::ReadItem<'_> {
633            let lower = self.offsets[index];
634            let upper = self.offsets[index+1];
635            &self.inner[lower .. upper]
636        }
637        fn len(&self) -> usize {
638            self.offsets.len() - 1
639        }
640    }
641
642    /// Default implementation introduces a first offset.
643    impl<B> Default for SliceContainer<B> {
644        fn default() -> Self {
645            Self {
646                offsets: vec![0],
647                inner: Default::default(),
648            }
649        }
650    }
651}