Protocol.Consolidation

Module responsible for consolidating protocols and helpers for extracting protocols and implementations from code paths for consolidation.

Source

Summary

apply_to(protocol, types)

Receives a protocol and a list of implementations and consolidates the given protocol. Consolidation happens by changing the protocol impl_for in the abstract format to have fast lookup rules

extract_impls(protocol, paths)

Extract all types implemented for the given protocol from the given paths

extract_protocols(paths)

Extract all protocols from the given paths

Functions

apply_to(protocol, types)

Specs:

  • apply_to(module, [module]) :: {:ok, binary} | {:error, :not_a_protocol} | {:error, :no_beam_info}

Receives a protocol and a list of implementations and consolidates the given protocol. Consolidation happens by changing the protocol impl_for in the abstract format to have fast lookup rules.

It returns the updated version of the protocol bytecode. A given bytecode or protocol implementation can be checked to be consolidated or not by analyzing the protocol attribute:

Enumerable.__info__(:attributes)[:protocol]

If the first element of the tuple is true, it means the protocol was consolidated.

This function does not load the protocol at any point nor loads the new bytecode for the compiled module.

Source
extract_impls(protocol, paths)

Specs:

  • extract_impls(module, [char_list | String.t]) :: [atom]

Extract all types implemented for the given protocol from the given paths.

The paths can be either a char list or a string. Internally they are worked on as char lists, so passing them as lists avoid extra conversion.

Examples

# Get Elixir's ebin and retrieve all protocols
iex> path = :code.lib_dir(:elixir, :ebin)
iex> mods = Protocol.Consolidation.extract_impls(Enumerable, [path])
iex> List in mods
true
Source
extract_protocols(paths)

Specs:

  • extract_protocols([char_list | String.t]) :: [atom]

Extract all protocols from the given paths.

The paths can be either a char list or a string. Internally they are worked on as char lists, so passing them as lists avoid extra conversion.

Examples

# Get Elixir's ebin and retrieve all protocols
iex> path = :code.lib_dir(:elixir, :ebin)
iex> mods = Protocol.Consolidation.extract_protocols([path])
iex> Enumerable in mods
true
Source