mz_repr/adt/
jsonb.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//! JSON representation.
11//!
12//! This module provides a [`serde_json`]-like API that is backed by the native
13//! Materialize data format, i.e., [`Row`] and [`Datum`]. It supports
14//! efficiently parsing and serializing JSON strings and byte slices with
15//! minimal allocations. It also provides seamless interop with APIs that
16//! require [`serde_json::Value`], though at a small performance cost.
17//!
18//! There are two core types in the module:
19//!
20//!   * [`Jsonb`] represents owned JSON data. This type houses the
21//!     deserialization functions.
22//!
23//!   * [`JsonbRef`] is a borrowed view of JSON data. This type houses the
24//!     serialization functions.
25//!
26//! The name "jsonb" is based on the PostgreSQL data type of the same name.
27//! Various sources claim this stands for "JSON better", as compared to the
28//! less-efficient `json` data type in PostgreSQL.
29//!
30//! ## Constructing JSON objects
31//!
32//! To parse JSON from a string, use the [`FromStr`] implementation. Once
33//! parsed, the underlying [`Row`] can be extracted with [`Jsonb::into_row`].
34//!
35//! ```
36//! # use mz_repr::adt::jsonb::Jsonb;
37//! let jsonb: Jsonb = r#"{"a": 1, "b": 2}"#.parse()?;
38//! let row = jsonb.into_row();
39//! # Ok::<(), Box<dyn std::error::Error>>(())
40//! ```
41//!
42//! If the source JSON is in bytes, use [`Jsonb::from_slice`] instead:
43//!
44//! ```
45//! # use mz_repr::adt::jsonb::Jsonb;
46//! let jsonb = Jsonb::from_slice(br#"{"a": 1, "b": 2}"#);
47//! # Ok::<(), Box<dyn std::error::Error>>(())
48//! ```
49//!
50//! ## Serializing JSON objects
51//!
52//! To write a JSON object to a string, use the [`fmt::Display`] implementation.
53//! The alternate format produces pretty output.
54//!
55//! ```
56//! # use mz_repr::adt::jsonb::Jsonb;
57//! # let jsonb: Jsonb = "null".parse().unwrap();
58//! format!("compressed: {}", jsonb);
59//! format!("pretty: {:#}", jsonb);
60//! ```
61//!
62//! ## Direct JSON deserialization
63//!
64//! You can skip [`Jsonb`] entirely and deserialize JSON directly into an
65//! existing [`Row`] with [`JsonbPacker`]. This saves an allocation and a
66//! copy.
67//!
68//! ```rust
69//! # use mz_repr::adt::jsonb::JsonbPacker;
70//! # use mz_repr::{Datum, Row};
71//! let mut row = Row::default();
72//! let mut packer = row.packer();
73//! packer.push(Datum::Int32(42));
74//! JsonbPacker::new(&mut packer).pack_str("[1, 2]")?;
75//! # Ok::<(), Box<dyn std::error::Error>>(())
76//! ```
77
78use std::borrow::Cow;
79use std::str::{self, FromStr};
80use std::{fmt, io};
81
82use dec::OrderedDecimal;
83use serde::de::{self, DeserializeSeed, Deserializer, MapAccess, SeqAccess, Visitor};
84use serde::ser::{Serialize, SerializeMap, SerializeSeq, SerializeStruct, Serializer};
85
86use crate::adt::jsonb::vec_stack::VecStack;
87use crate::adt::numeric::Numeric;
88use crate::{Datum, Row, RowPacker, SharedRow, strconv};
89
90/// An owned JSON value backed by a [`Row`].
91///
92/// Similar to [`serde_json::Value`], but the conversion to [`Row`] is free.
93///
94/// All numbers are represented as [`f64`]s. It is not possible to construct a
95/// `Jsonb` from a JSON object that contains integers that cannot be represented
96/// exactly as `f64`s.
97#[derive(Debug, Clone, PartialOrd, Ord, PartialEq, Eq)]
98pub struct Jsonb {
99    row: Row,
100}
101
102impl Jsonb {
103    /// Constructs a new `Jsonb` from a [`serde_json::Value`].
104    pub fn from_serde_json(val: serde_json::Value) -> Result<Self, anyhow::Error> {
105        let mut row = Row::default();
106        JsonbPacker::new(&mut row.packer()).pack_serde_json(val)?;
107        Ok(Jsonb { row })
108    }
109
110    /// Parses a `Jsonb` from a byte slice `buf`.
111    ///
112    /// Errors if the slice is not valid JSON.
113    pub fn from_slice(buf: &[u8]) -> Result<Jsonb, anyhow::Error> {
114        let binding = SharedRow::get();
115        let mut row_builder = binding.borrow_mut();
116        let mut packer = row_builder.packer();
117        JsonbPacker::new(&mut packer).pack_slice(buf)?;
118        Ok(Jsonb {
119            row: row_builder.clone(),
120        })
121    }
122
123    /// Constructs a [`JsonbRef`] that references the JSON in this `Jsonb`.
124    pub fn as_ref(&self) -> JsonbRef {
125        JsonbRef {
126            datum: self.row.unpack_first(),
127        }
128    }
129
130    /// Constructs a `Jsonb` from a [`Row`] containing a single [`Datum`].
131    ///
132    /// Note that `row` is not checked for validity. Not all `Datum`s are
133    /// valid JSON.
134    pub fn from_row(row: Row) -> Jsonb {
135        Jsonb { row }
136    }
137
138    /// Consumes this `Jsonb` and returns the underlying [`Row`].
139    pub fn into_row(self) -> Row {
140        self.row
141    }
142
143    /// Returns a reference to the underlying [`Row`].
144    pub fn row(&self) -> &Row {
145        &self.row
146    }
147}
148
149impl FromStr for Jsonb {
150    type Err = anyhow::Error;
151
152    fn from_str(s: &str) -> Result<Self, Self::Err> {
153        let mut row = Row::default();
154        JsonbPacker::new(&mut row.packer()).pack_str(s)?;
155        Ok(Jsonb { row })
156    }
157}
158
159impl fmt::Display for Jsonb {
160    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
161        self.as_ref().fmt(f)
162    }
163}
164
165/// A borrowed JSON value.
166///
167/// `JsonbRef` is to [`Jsonb`] as [`&str`](prim@str) is to [`String`].
168#[derive(Debug, Copy, Clone)]
169pub struct JsonbRef<'a> {
170    datum: Datum<'a>,
171}
172
173impl<'a> JsonbRef<'a> {
174    /// Constructs a `JsonbRef` from a [`Datum`].
175    ///
176    /// Note that `datum` is not checked for validity. Not all `Datum`s are
177    /// valid JSON.
178    pub fn from_datum(datum: Datum) -> JsonbRef {
179        JsonbRef { datum }
180    }
181
182    /// Unwraps a `JsonbRef` and returns the inner [`Datum`].
183    pub fn into_datum(self) -> Datum<'a> {
184        self.datum
185    }
186
187    /// Constructs an owned [`Jsonb`] from this `JsonbRef`.
188    pub fn to_owned(&self) -> Jsonb {
189        Jsonb {
190            row: Row::pack_slice(&[self.datum]),
191        }
192    }
193
194    /// Serializes the JSON value into the given IO stream.
195    ///
196    /// # Panics
197    ///
198    /// Panics if this `JsonbRef` was constructed with a [`Datum`] that is not
199    /// representable as JSON.
200    pub fn to_writer<W>(&self, writer: W) -> Result<(), anyhow::Error>
201    where
202        W: io::Write,
203    {
204        serde_json::to_writer(writer, &JsonbDatum(self.datum))?;
205        Ok(())
206    }
207
208    /// Constructs an owned [`serde_json::Value`] from this `JsonbRef`.
209    ///
210    /// # Panics
211    ///
212    /// Panics if this `JsonbRef` was constructed with a [`Datum`] that is not
213    /// representable as JSON.
214    pub fn to_serde_json(&self) -> serde_json::Value {
215        serde_json::to_value(JsonbDatum(self.datum))
216            .expect("conversion to serde_json::Value known to be valid")
217    }
218}
219
220impl fmt::Display for JsonbRef<'_> {
221    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
222        let alternate = f.alternate();
223        let mut w = WriterFormatter { inner: f };
224        if alternate {
225            serde_json::to_writer_pretty(&mut w, &JsonbDatum(self.datum)).map_err(|_| fmt::Error)
226        } else {
227            serde_json::to_writer(&mut w, &JsonbDatum(self.datum)).map_err(|_| fmt::Error)
228        }
229    }
230}
231
232/// A JSON deserializer that decodes directly into an existing [`RowPacker`].
233#[derive(Debug)]
234pub struct JsonbPacker<'a, 'row> {
235    packer: &'a mut RowPacker<'row>,
236}
237
238impl<'a, 'row> JsonbPacker<'a, 'row> {
239    /// Constructs a new `JsonbPacker` that will pack into `row`.
240    pub fn new(packer: &'a mut RowPacker<'row>) -> JsonbPacker<'a, 'row> {
241        JsonbPacker { packer }
242    }
243
244    /// Packs a [`serde_json::Value`].
245    pub fn pack_serde_json(self, val: serde_json::Value) -> Result<(), serde_json::Error> {
246        let mut commands = vec![];
247        Collector(&mut commands).deserialize(val)?;
248        pack(self.packer, &commands);
249        Ok(())
250    }
251
252    /// Parses and packs a JSON-formatted byte slice.
253    ///
254    /// Errors if the slice is not valid JSON.
255    pub fn pack_slice(self, buf: &[u8]) -> Result<(), serde_json::Error> {
256        let mut commands = vec![];
257        let mut deserializer = serde_json::Deserializer::from_slice(buf);
258        Collector(&mut commands).deserialize(&mut deserializer)?;
259        deserializer.end()?;
260        pack(self.packer, &commands);
261        Ok(())
262    }
263
264    /// Parses and packs a JSON-formatted string.
265    ///
266    /// Errors if the string is not valid JSON.
267    pub fn pack_str(self, s: &str) -> Result<(), serde_json::Error> {
268        let mut commands = vec![];
269        let mut deserializer = serde_json::Deserializer::from_str(s);
270        Collector(&mut commands).deserialize(&mut deserializer)?;
271        deserializer.end()?;
272        pack(self.packer, &commands);
273        Ok(())
274    }
275}
276
277// The magic internal key name that serde_json uses to indicate that an
278// arbitrary-precision number should be serialized or deserialized. Yes, this is
279// technically private API, but there's no other way to hook into this machinery
280// that doesn't involve all the allocations that come with `serde_json::Value`.
281// See the comments in `JsonbDatum::Serialize` and `Collector::visit_map` for
282// details.
283const SERDE_JSON_NUMBER_TOKEN: &str = "$serde_json::private::Number";
284
285// To support arbitrary-precision numbers, serde_json pretends the JSON
286// contained a map like the following:
287//
288//     {"$serde_json::private::Number": "NUMBER AS STRING"}
289//
290// The code here sniffs out that special map and emits just the numeric
291// value.
292
293#[derive(Debug)]
294pub(crate) enum KeyClass<'de> {
295    Number,
296    MapKey(Cow<'de, str>),
297}
298
299pub(crate) struct KeyClassifier;
300
301impl<'de> DeserializeSeed<'de> for KeyClassifier {
302    type Value = KeyClass<'de>;
303
304    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
305    where
306        D: serde::Deserializer<'de>,
307    {
308        deserializer.deserialize_str(self)
309    }
310}
311
312impl<'de> Visitor<'de> for KeyClassifier {
313    type Value = KeyClass<'de>;
314
315    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
316        formatter.write_str("a string key")
317    }
318
319    #[inline]
320    fn visit_borrowed_str<E>(self, key: &'de str) -> Result<Self::Value, E> {
321        match key {
322            SERDE_JSON_NUMBER_TOKEN => Ok(KeyClass::Number),
323            _ => Ok(KeyClass::MapKey(Cow::Borrowed(key))),
324        }
325    }
326
327    #[inline]
328    fn visit_str<E>(self, key: &str) -> Result<Self::Value, E> {
329        match key {
330            SERDE_JSON_NUMBER_TOKEN => Ok(KeyClass::Number),
331            _ => Ok(KeyClass::MapKey(Cow::Owned(key.to_owned()))),
332        }
333    }
334}
335
336pub(crate) struct NumberParser;
337
338impl<'de> DeserializeSeed<'de> for NumberParser {
339    type Value = OrderedDecimal<Numeric>;
340
341    fn deserialize<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
342    where
343        D: serde::Deserializer<'de>,
344    {
345        deserializer.deserialize_str(self)
346    }
347}
348
349impl<'de> Visitor<'de> for NumberParser {
350    type Value = OrderedDecimal<Numeric>;
351
352    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
353        formatter.write_str("a valid number")
354    }
355
356    #[inline]
357    fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
358    where
359        E: de::Error,
360    {
361        strconv::parse_numeric(value).map_err(de::Error::custom)
362    }
363}
364
365#[derive(Debug)]
366enum Command<'de> {
367    Null,
368    Bool(bool),
369    Numeric(OrderedDecimal<Numeric>),
370    String(Cow<'de, str>),
371    Array(usize), // further commands
372    Map(usize),   // further commands
373}
374
375struct Collector<'a, 'de>(&'a mut Vec<Command<'de>>);
376
377impl<'a, 'de> DeserializeSeed<'de> for Collector<'a, 'de> {
378    type Value = ();
379
380    fn deserialize<D>(self, deserializer: D) -> Result<(), D::Error>
381    where
382        D: Deserializer<'de>,
383    {
384        deserializer.deserialize_any(self)
385    }
386}
387
388impl<'a, 'de> Visitor<'de> for Collector<'a, 'de> {
389    type Value = ();
390
391    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
392        formatter.write_str("a valid JSON datum")
393    }
394
395    #[inline]
396    fn visit_unit<E>(self) -> Result<(), E> {
397        self.0.push(Command::Null);
398        Ok(())
399    }
400
401    #[inline]
402    fn visit_bool<E>(self, value: bool) -> Result<(), E> {
403        self.0.push(Command::Bool(value));
404        Ok(())
405    }
406
407    #[inline]
408    fn visit_i64<E>(self, value: i64) -> Result<(), E>
409    where
410        E: de::Error,
411    {
412        self.0.push(Command::Numeric(OrderedDecimal(value.into())));
413        Ok(())
414    }
415
416    #[inline]
417    fn visit_u64<E>(self, value: u64) -> Result<(), E>
418    where
419        E: de::Error,
420    {
421        self.0.push(Command::Numeric(OrderedDecimal(value.into())));
422        Ok(())
423    }
424
425    #[inline]
426    fn visit_f64<E>(self, value: f64) -> Result<(), E> {
427        self.0.push(Command::Numeric(OrderedDecimal(value.into())));
428        Ok(())
429    }
430
431    #[inline]
432    fn visit_str<E>(self, value: &str) -> Result<(), E> {
433        self.0.push(Command::String(Cow::Owned(value.to_owned())));
434        Ok(())
435    }
436
437    #[inline]
438    fn visit_borrowed_str<E>(self, value: &'de str) -> Result<(), E> {
439        self.0.push(Command::String(Cow::Borrowed(value)));
440        Ok(())
441    }
442
443    #[inline]
444    fn visit_seq<V>(self, mut visitor: V) -> Result<(), V::Error>
445    where
446        V: SeqAccess<'de>,
447    {
448        self.0.push(Command::Array(0));
449        let start = self.0.len();
450        while visitor.next_element_seed(Collector(self.0))?.is_some() {}
451        self.0[start - 1] = Command::Array(self.0.len() - start);
452        Ok(())
453    }
454
455    #[inline]
456    fn visit_map<V>(self, mut visitor: V) -> Result<(), V::Error>
457    where
458        V: MapAccess<'de>,
459    {
460        match visitor.next_key_seed(KeyClassifier)? {
461            Some(KeyClass::Number) => {
462                let n = visitor.next_value_seed(NumberParser)?;
463                self.0.push(Command::Numeric(n));
464            }
465            Some(KeyClass::MapKey(key)) => {
466                self.0.push(Command::Map(0));
467                let start = self.0.len();
468                self.0.push(Command::String(key));
469                visitor.next_value_seed(Collector(self.0))?;
470                while visitor.next_key_seed(Collector(self.0))?.is_some() {
471                    visitor.next_value_seed(Collector(self.0))?;
472                }
473                self.0[start - 1] = Command::Map(self.0.len() - start);
474            }
475            None => self.0.push(Command::Map(0)),
476        }
477
478        Ok(())
479    }
480}
481
482struct DictEntry<'a> {
483    key: &'a str,
484    val: &'a [Command<'a>],
485}
486
487fn pack(packer: &mut RowPacker, value: &[Command]) {
488    let mut buf = vec![];
489    pack_value(packer, VecStack::new(&mut buf), value);
490}
491
492#[inline]
493fn pack_value<'a, 'scratch>(
494    packer: &mut RowPacker,
495    scratch: VecStack<'scratch, DictEntry<'a>>,
496    value: &'a [Command<'a>],
497) {
498    match &value[0] {
499        Command::Null => packer.push(Datum::JsonNull),
500        Command::Bool(b) => packer.push(if *b { Datum::True } else { Datum::False }),
501        Command::Numeric(n) => packer.push(Datum::Numeric(*n)),
502        Command::String(s) => packer.push(Datum::String(s)),
503        Command::Array(further) => {
504            let range = &value[1..][..*further];
505            pack_list(packer, scratch, range);
506        }
507        Command::Map(further) => {
508            let range = &value[1..][..*further];
509            pack_dict(packer, scratch, range);
510        }
511    }
512}
513
514/// Packs a sequence of values as an ordered list.
515fn pack_list<'a, 'scratch>(
516    packer: &mut RowPacker,
517    mut scratch: VecStack<'scratch, DictEntry<'a>>,
518    mut values: &'a [Command<'a>],
519) {
520    packer.push_list_with(|packer| {
521        while !values.is_empty() {
522            let value = extract_value(&mut values);
523            pack_value(packer, scratch.fresh(), value);
524        }
525    })
526}
527
528/// Packs a sequence of (key, val) pairs as an ordered dictionary.
529///
530/// The keys are required to be `Command::String` variants, and
531/// the entries in the dictionary are sorted by these strings.
532/// Multiple values for the same key are detected and only
533/// the last value is kept.
534fn pack_dict<'a, 'scratch>(
535    packer: &mut RowPacker,
536    mut scratch: VecStack<'scratch, DictEntry<'a>>,
537    mut entries: &'a [Command<'a>],
538) {
539    while !entries.is_empty() {
540        if let Command::String(key) = &entries[0] {
541            entries = &entries[1..];
542            let val = extract_value(&mut entries);
543            scratch.push(DictEntry { key, val });
544        } else {
545            unreachable!("JSON decoding produced invalid command sequence");
546        }
547    }
548
549    // Keys must be written in ascending order, per the requirements of our
550    // `Row` representation. If keys are duplicated, we must keep only the last
551    // value for the key, as ordered by appearance in the source JSON, per
552    // PostgreSQL's implementation.
553    scratch.sort_by_key(|entry| entry.key);
554    packer.push_dict_with(|packer| {
555        for i in 0..scratch.len() {
556            if i == scratch.len() - 1 || scratch[i].key != scratch[i + 1].key {
557                let DictEntry { key, val } = scratch[i];
558                packer.push(Datum::String(key));
559                pack_value(packer, scratch.fresh(), val);
560            }
561        }
562    });
563}
564
565/// Extracts a self-contained slice of commands for the next parse node.
566fn extract_value<'a>(values: &mut &'a [Command<'a>]) -> &'a [Command<'a>] {
567    let result = match values[0] {
568        Command::Array(further) => &values[..further + 1],
569        Command::Map(further) => &values[..further + 1],
570        _ => &values[0..1],
571    };
572    *values = &values[result.len()..];
573    result
574}
575
576/// A wrapper for [`Datum`] that implements [`Serialize`].
577///
578/// This is a separate type from `JsonbRef` because the `Serialize`
579/// implementation is only valid for use with serde_json. If we implemented it
580/// on `JsonbRef` directly, it would be possible to serialize a `JsonbRef` into
581/// any serde format, like YAML, but that transformation would not be tested
582/// and would likely be invalid.
583struct JsonbDatum<'a>(Datum<'a>);
584
585impl Serialize for JsonbDatum<'_> {
586    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
587    where
588        S: Serializer,
589    {
590        match self.0 {
591            Datum::JsonNull => serializer.serialize_none(),
592            Datum::True => serializer.serialize_bool(true),
593            Datum::False => serializer.serialize_bool(false),
594            Datum::Numeric(n) => {
595                // To serialize an arbitrary-precision number, we present
596                // serde_json with the following magic struct:
597                //
598                //     struct $serde_json::private::Number {
599                //          $serde_json::private::Number: <NUMBER VALUE>,
600                //     }
601                //
602                let mut s = serializer.serialize_struct(SERDE_JSON_NUMBER_TOKEN, 1)?;
603                s.serialize_field(
604                    SERDE_JSON_NUMBER_TOKEN,
605                    &n.into_inner().to_standard_notation_string(),
606                )?;
607                s.end()
608            }
609            Datum::String(s) => serializer.serialize_str(s),
610            Datum::List(list) => {
611                let mut seq = serializer.serialize_seq(None)?;
612                for e in list.iter() {
613                    seq.serialize_element(&JsonbDatum(e))?;
614                }
615                seq.end()
616            }
617            Datum::Map(dict) => {
618                let mut map = serializer.serialize_map(None)?;
619                for (k, v) in dict.iter() {
620                    map.serialize_entry(k, &JsonbDatum(v))?;
621                }
622                map.end()
623            }
624            d => unreachable!("not a json-compatible datum: {:?}", d),
625        }
626    }
627}
628
629/// Implements `io::Write` for `fmt::Formatter`.
630struct WriterFormatter<'a, 'b: 'a> {
631    inner: &'a mut fmt::Formatter<'b>,
632}
633
634impl<'a, 'b> io::Write for WriterFormatter<'a, 'b> {
635    fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
636        fn io_error<E>(_: E) -> io::Error {
637            // Error value is dropped by `fmt::Display` implementations above.
638            io::Error::new(io::ErrorKind::Other, "fmt error")
639        }
640        let s = str::from_utf8(buf).map_err(io_error)?;
641        self.inner.write_str(s).map_err(io_error)?;
642        Ok(buf.len())
643    }
644
645    fn flush(&mut self) -> io::Result<()> {
646        Ok(())
647    }
648}
649
650mod vec_stack {
651    use std::ops::Index;
652
653    /// A `VecStack` presents as a stack of [`Vec`]s where only the vector at
654    /// the top of the stack is accessible. It is backed by a single [`Vec`]
655    /// whose allocation is reused as elements are added to and dropped from the
656    /// stack, and so it can be more efficient than allocating individual
657    /// vectors.
658    pub struct VecStack<'a, T> {
659        buf: &'a mut Vec<T>,
660        i: usize,
661    }
662
663    impl<'a, T> VecStack<'a, T> {
664        /// Creates a new `VecStack` backed by `buf`.
665        ///
666        /// The stack starts with a single psuedo-vector.
667        pub fn new(buf: &'a mut Vec<T>) -> VecStack<'a, T> {
668            VecStack { buf, i: 0 }
669        }
670
671        /// Adds a new element to the psuedo-vector at the top of the stack.
672        pub fn push(&mut self, t: T) {
673            self.buf.push(t)
674        }
675
676        /// Sorts the psuedo-vector at the top of the stack by the key
677        /// identified by `f`.
678        pub fn sort_by_key<F, K>(&mut self, f: F)
679        where
680            F: FnMut(&T) -> K,
681            K: Ord,
682        {
683            self.buf[self.i..].sort_by_key(f)
684        }
685
686        /// Returns the length of the psuedo-vector at the top of the stack.
687        pub fn len(&self) -> usize {
688            self.buf.len() - self.i
689        }
690
691        /// Push a fresh vector onto the stack.
692        ///
693        /// The returned `VecStack` is a handle to this vector. The
694        /// psuedo-vector beneath the new vector is inaccessible until the new
695        /// handle is dropped.
696        pub fn fresh<'b>(&'b mut self) -> VecStack<'b, T> {
697            let i = self.buf.len();
698            VecStack { buf: self.buf, i }
699        }
700    }
701
702    impl<'a, T> Index<usize> for VecStack<'a, T> {
703        type Output = T;
704
705        fn index(&self, i: usize) -> &Self::Output {
706            &self.buf[self.i + i]
707        }
708    }
709
710    impl<'a, T> Drop for VecStack<'a, T> {
711        fn drop(&mut self) {
712            self.buf.truncate(self.i)
713        }
714    }
715}