<?xml version="1.0" encoding="utf-8"?>
<AutoVisualizer xmlns="http://schemas.microsoft.com/vstudio/debugger/natvis/2010">
  <!--
    std::collection::Hash* container visualizers

    Current std impls:
      std::collections::hash::set::HashSet<K, S>      is implemented in terms of...
      hashbrown::set::HashSet<K, S>                   is implemented in terms of...
      hashbrown::map::HashMap<K, V, S>                is implemented in terms of...
      hashbrown::raw::RawTable<(K, V)>

    Ideally, we'd teach rustc to scan dependencies/crates for .natvis files so
    the bulk of this could live alongside the hashbrown crate implementation,
    and std would just forward using e.g. <ExpandedItem>base</ExpandedItem>.

    However, Given that std...Hash*Set* is currently implemented in terms of
    hashbrown...Hash*Map*, which would visualize poorly, we want to customize the
    look/feel at the std type level *anyways*...

    References:
      https://github.com/rust-lang/rust/blob/master/src/libstd/collections/hash/map.rs
      https://github.com/rust-lang/rust/blob/master/src/libstd/collections/hash/set.rs
      https://github.com/rust-lang/hashbrown/blob/master/src/map.rs
      https://github.com/rust-lang/hashbrown/blob/master/src/set.rs
      https://github.com/rust-lang/hashbrown/blob/master/src/raw/mod.rs
  -->

  <Type Name="std::collections::hash::map::HashMap&lt;*,*,*&gt;">
    <DisplayString>{{ size={base.table.items} }}</DisplayString>
    <Expand>
      <Item Name="[size]">base.table.items</Item>
      <Item Name="[capacity]">base.table.items + base.table.growth_left</Item>
      <Item Name="[state]">base.hash_builder</Item>

      <CustomListItems>
        <Variable Name="i" InitialValue="0" />
        <Variable Name="n" InitialValue="base.table.items" />
        <Size>base.table.items</Size>
        <Loop>
          <Break Condition="n == 0" />
          <If Condition="(base.table.ctrl.pointer[i] &amp; 0x80) == 0">
            <!-- Bucket is populated -->
            <Exec>n--</Exec>
            <Item Name="{((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__0}">((tuple&lt;$T1, $T2&gt;*)base.table.ctrl.pointer)[-(i + 1)].__1</Item>
          </If>
          <Exec>i++</Exec>
        </Loop>
      </CustomListItems>
    </Expand>
  </Type>

  <Type Name="std::collections::hash::set::HashSet&lt;*,*&gt;">
    <DisplayString>{{ size={base.map.table.items} }}</DisplayString>
    <Expand>
      <Item Name="[size]">base.map.table.items</Item>
      <Item Name="[capacity]">base.map.table.items + base.map.table.growth_left</Item>
      <Item Name="[state]">base.map.hash_builder</Item>

      <CustomListItems>
        <Variable Name="i" InitialValue="0" />
        <Variable Name="n" InitialValue="base.map.table.items" />
        <Size>base.map.table.items</Size>
        <Loop>
          <Break Condition="n == 0" />
          <If Condition="(base.map.table.ctrl.pointer[i] &amp; 0x80) == 0">
            <!-- Bucket is populated -->
            <Exec>n--</Exec>
            <Item>(($T1*)base.map.table.ctrl.pointer)[-(i + 1)]</Item>
          </If>
          <Exec>i++</Exec>
        </Loop>
      </CustomListItems>
    </Expand>
  </Type>
</AutoVisualizer>
