Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Advanced topics

Bulk decoding of metadata

To populate a Vec with decoded metadata and accounting for some rows not having metadata:

  • Realize that the decode methods of the MetadataRoundtrip trait are associated functions.
  • We can use a lending iterator over rows to avoid unnecessary copying.

Therefore:

    let mut decoded_md = vec![];
    for row in tables.mutations().iter() {
        match row.metadata() {
            Some(slice) => decoded_md.push(Some(MutationMetadata::decode(slice).unwrap())),
            None => decoded_md.push(None),
        }
    }

To filter out rows without metadata:

    let mut decoded_md = vec![];
    for row in tables.mutation_iter().filter(|rv| rv.metadata().is_some()) {
        decoded_md.push((
            row.id(),
            // The unwrap will never panic because of our filter
            MutationMetadata::decode(row.metadata().unwrap()).unwrap(),
        ));
    }

The first method gives Vec<Option<MutationMetadata>>. The second gives Vec<(MutationId, MutationMetadata)>.