Skip to content

Commit f428bba

Browse files
authored
Merge pull request #6 from fhunleth/specs
Add specs and dialyzer
2 parents 48c576e + 7570fc2 commit f428bba

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

lib/circular_buffer.ex

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,23 @@ defmodule CircularBuffer do
1414
"""
1515

1616
defstruct [:a, :b, :max_size, :count]
17+
@typedoc "A circular buffer"
18+
@opaque t() :: %__MODULE__{a: list(), b: list(), max_size: pos_integer(), count: non_neg_integer()}
1719

1820
alias __MODULE__, as: CB
1921

2022
@doc """
2123
Creates a new circular buffer with a given size.
2224
"""
25+
@spec new(pos_integer()) :: t()
2326
def new(size) when is_integer(size) and size > 0 do
2427
%CB{a: [], b: [], max_size: size, count: 0}
2528
end
2629

2730
@doc """
2831
Inserts a new item into the next location of the circular buffer
2932
"""
33+
@spec insert(t(), any()) :: t()
3034
def insert(%CB{b: b} = cb, item) when b != [] do
3135
%CB{cb | a: [item | cb.a], b: tl(b)}
3236
end
@@ -44,25 +48,29 @@ defmodule CircularBuffer do
4448
Converts a circular buffer to a list. The list is ordered from oldest to newest
4549
elements based on their insertion order.
4650
"""
51+
@spec to_list(t()) :: list()
4752
def to_list(%CB{} = cb) do
4853
cb.b ++ Enum.reverse(cb.a)
4954
end
5055

5156
@doc """
5257
Returns the newest element in the buffer
5358
"""
59+
@spec newest(t()) :: any()
5460
def newest(%CB{a: [newest | _rest]}), do: newest
5561
def newest(%CB{b: []}), do: nil
5662

5763
@doc """
5864
Returns the oldest element in the buffer
5965
"""
66+
@spec oldest(t()) :: any()
6067
def oldest(%CB{b: [oldest | _rest]}), do: oldest
6168
def oldest(%CB{a: a}), do: List.last(a)
6269

6370
@doc """
6471
Checks the buffer to see if its empty
6572
"""
73+
@spec empty?(t()) :: boolean()
6674
def empty?(%CB{} = cb) do
6775
cb.count == 0
6876
end

mix.exs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ defmodule CircularBuffer.MixProject do
1414
package: package(),
1515
name: "CircularBuffer",
1616
source_url: "https://github.com/keathley/circular_buffer",
17-
docs: docs()
17+
docs: docs(),
18+
dialyzer: dialyzer()
1819
]
1920
end
2021

@@ -29,7 +30,8 @@ defmodule CircularBuffer.MixProject do
2930
defp deps do
3031
[
3132
{:propcheck, "~> 1.2", only: [:dev, :test]},
32-
{:ex_doc, "~> 0.19", only: [:dev, :test]}
33+
{:ex_doc, "~> 0.19", only: [:dev, :test], runtime: false},
34+
{:dialyxir, "~> 1.1.0", only: :dev, runtime: false}
3335
]
3436
end
3537

@@ -54,4 +56,10 @@ defmodule CircularBuffer.MixProject do
5456
main: "CircularBuffer"
5557
]
5658
end
59+
60+
defp dialyzer() do
61+
[
62+
flags: [:race_conditions, :unmatched_returns, :error_handling]
63+
]
64+
end
5765
end

mix.lock

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
%{
2-
"earmark": {:hex, :earmark, "1.4.2", "3aa0bd23bc4c61cf2f1e5d752d1bb470560a6f8539974f767a38923bb20e1d7f", [:mix], [], "hexpm"},
3-
"ex_doc": {:hex, :ex_doc, "0.21.2", "caca5bc28ed7b3bdc0b662f8afe2bee1eedb5c3cf7b322feeeb7c6ebbde089d6", [:mix], [{:earmark, "~> 1.3.3 or ~> 1.4", [hex: :earmark, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}], "hexpm"},
4-
"makeup": {:hex, :makeup, "1.0.0", "671df94cf5a594b739ce03b0d0316aa64312cee2574b6a44becb83cd90fb05dc", [:mix], [{:nimble_parsec, "~> 0.5.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm"},
5-
"makeup_elixir": {:hex, :makeup_elixir, "0.14.0", "cf8b7c66ad1cff4c14679698d532f0b5d45a3968ffbcbfd590339cb57742f1ae", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm"},
6-
"nimble_parsec": {:hex, :nimble_parsec, "0.5.2", "1d71150d5293d703a9c38d4329da57d3935faed2031d64bc19e77b654ef2d177", [:mix], [], "hexpm"},
7-
"propcheck": {:hex, :propcheck, "1.2.0", "e2b84f2f1a4c46b6b2aa22a0f6ddf97696f99d4a5c8f71d45f6519741e727eca", [:mix], [{:proper, "~> 1.3", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm"},
8-
"proper": {:hex, :proper, "1.3.0", "c1acd51c51da17a2fe91d7a6fc6a0c25a6a9849d8dc77093533109d1218d8457", [:make, :mix, :rebar3], [], "hexpm"},
2+
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
3+
"earmark_parser": {:hex, :earmark_parser, "1.4.13", "0c98163e7d04a15feb62000e1a891489feb29f3d10cb57d4f845c405852bbef8", [:mix], [], "hexpm", "d602c26af3a0af43d2f2645613f65841657ad6efc9f0e361c3b6c06b578214ba"},
4+
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
5+
"ex_doc": {:hex, :ex_doc, "0.24.2", "e4c26603830c1a2286dae45f4412a4d1980e1e89dc779fcd0181ed1d5a05c8d9", [:mix], [{:earmark_parser, "~> 1.4.0", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "e134e1d9e821b8d9e4244687fb2ace58d479b67b282de5158333b0d57c6fb7da"},
6+
"libgraph": {:hex, :libgraph, "0.13.3", "20732b7bafb933dcf7351c479e03076ebd14a85fd3202c67a1c197f4f7c2466b", [:mix], [], "hexpm", "78f2576eef615440b46f10060b1de1c86640441422832052686df53dc3c148c6"},
7+
"makeup": {:hex, :makeup, "1.0.5", "d5a830bc42c9800ce07dd97fa94669dfb93d3bf5fcf6ea7a0c67b2e0e4a7f26c", [:mix], [{:nimble_parsec, "~> 0.5 or ~> 1.0", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "cfa158c02d3f5c0c665d0af11512fed3fba0144cf1aadee0f2ce17747fba2ca9"},
8+
"makeup_elixir": {:hex, :makeup_elixir, "0.15.1", "b5888c880d17d1cc3e598f05cdb5b5a91b7b17ac4eaf5f297cb697663a1094dd", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.1", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "db68c173234b07ab2a07f645a5acdc117b9f99d69ebf521821d89690ae6c6ec8"},
9+
"makeup_erlang": {:hex, :makeup_erlang, "0.1.1", "3fcb7f09eb9d98dc4d208f49cc955a34218fc41ff6b84df7c75b3e6e533cc65f", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "174d0809e98a4ef0b3309256cbf97101c6ec01c4ab0b23e926a9e17df2077cbb"},
10+
"nimble_parsec": {:hex, :nimble_parsec, "1.1.0", "3a6fca1550363552e54c216debb6a9e95bd8d32348938e13de5eda962c0d7f89", [:mix], [], "hexpm", "08eb32d66b706e913ff748f11694b17981c0b04a33ef470e33e11b3d3ac8f54b"},
11+
"propcheck": {:hex, :propcheck, "1.4.0", "34ed3f05e7024be6ca4b45d48079cdcb0deb4ae1eed8b8405d3e14ed4b6c2c04", [:mix], [{:libgraph, "~> 0.13", [hex: :libgraph, repo: "hexpm", optional: false]}, {:proper, "~> 1.4", [hex: :proper, repo: "hexpm", optional: false]}], "hexpm", "305566952afba800e971e75a75526fb91e6c167ee3d78ceb67396848d90c6677"},
12+
"proper": {:hex, :proper, "1.4.0", "89a44b8c39d28bb9b4be8e4d715d534905b325470f2e0ec5e004d12484a79434", [:rebar3], [], "hexpm", "18285842185bd33efbda97d134a5cb5a0884384db36119fee0e3cfa488568cbb"},
913
}

0 commit comments

Comments
 (0)