View Source Bunch.ShortRef (Bunch v1.6.1)

A wrapper over Erlang/Elixir references that makes them more readable and visually distinguishable.

Erlang references

When printed, Erlang references are quite long: #Reference<0.133031758.722993155.68472>. Moreover, since they're based on an incremented counter, it's hard to distinguish between a two created within the same period of time, like #Reference<0.133031758.722993155.68512> and #Reference<0.133031758.722993155.68519>.

Bunch.ShortRef

Bunch.ShortRef.t/0 stores a usual reference along with first 4 bytes of its SHA1 hash and when inspected prints only 8 hex digits prepended with #. Thanks to use of the hash function, similar references have totally different string representations - the case from the previous example would be #60e0fd2d and #d4208051.

When to use

Bunch.ShortRef should be used when a reference is to be printed or logged. It should NOT be used when creating lots of references as it adds a significant performance overhead.

Summary

Functions

Creates a short reference.

Types

@type t() :: %Bunch.ShortRef{hash: String.t(), ref: reference()}

Functions

@spec new(reference()) :: t()

Creates a short reference.

iex> IEx.Helpers.ref(0, 1, 2, 3) |> Bunch.ShortRef.new() |> inspect()
"#82c033ef"
iex> <<"#", hash::binary-size(8)>> = Bunch.ShortRef.new() |> inspect()
iex> Base.decode16(hash, case: :lower) |> elem(0)
:ok