How to implement Enumerable protocol for struct?

Thank you all for the suggestions! @eksperimental this source code was very helpful and @srowley you are right some decisions must be taken about this :__struct__ key.

For example I tried implementing this with DateTime struct – I found this is almost 100% copy of the source code for Map impl. The only change I tried was in the count function but I understand this may be different depends on what you need to do:

defimpl Enumerable, for: DateTime do
  def count(map) do
    {:ok, map |> Map.from_struct() |> map_size()}
  end

  def member?(map, {key, value}) do
    {:ok, match?(%{^key => ^value}, map)}
  end

  def member?(_map, _other) do
    {:ok, false}
  end

  def slice(map) do
    size = map_size(map)
    {:ok, size, &Enumerable.List.slice(:maps.to_list(map), &1, &2, size)}
  end

  def reduce(map, acc, fun) do
    Enumerable.List.reduce(:maps.to_list(map), acc, fun)
  end
end