Permalink
Browse files

BUG 361: Add -spec for public API

  • Loading branch information...
1 parent f2156b3 commit f44e237bebd15baffb6eadcbec90e23535e5ca9b Scott Fritchie committed Jul 8, 2010
Showing with 65 additions and 12 deletions.
  1. +65 −12 src/ebloom.erl
View
@@ -39,6 +39,20 @@
-include_lib("eunit/include/eunit.hrl").
-endif.
+-spec init() -> ok | {error, any()}.
+-spec new(integer(), float(), integer()) -> {ok, reference()}.
+-spec insert(reference(), binary()) -> ok.
+-spec contains(reference(), binary()) -> true | false.
+-spec clear(reference()) -> ok.
+-spec size(reference()) -> integer().
+-spec elements(reference()) -> integer().
+-spec effective_fpp(reference()) -> float().
+-spec intersect(reference(), reference()) -> ok.
+-spec union(reference(), reference()) -> ok.
+-spec difference(reference(), reference()) -> ok.
+-spec serialize(reference()) -> binary().
+-spec deserialize(binary()) -> {ok, reference()}.
+
init() ->
case code:priv_dir(ebloom) of
{error, bad_name} ->
@@ -49,40 +63,79 @@ init() ->
erlang:load_nif(SoName, 0).
new(_Count, _FalseProb, _Seed) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> {ok, make_ref()};
+ _ -> exit("NIF library not loaded")
+ end.
insert(_Ref, _Bin) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
contains(_Ref, _Bin) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> true;
+ 667 -> false;
+ _ -> exit("NIF library not loaded")
+ end.
clear(_Ref) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
size(_Ref) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> random:uniform(4242);
+ 667 -> 0;
+ _ -> exit("NIF library not loaded")
+ end.
elements(_Ref) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> random:uniform(4242);
+ 667 -> 0;
+ _ -> exit("NIF library not loaded")
+ end.
effective_fpp(_Ref) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> random:uniform(4242) / 42.42;
+ _ -> exit("NIF library not loaded")
+ end.
intersect(_Ref, _OtherRef) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
union(_Ref, _OtherRef) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
difference(_Ref, _OtherRef) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> ok;
+ _ -> exit("NIF library not loaded")
+ end.
serialize(_Ref) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> list_to_binary(lists:duplicate(random:uniform(255), random:uniform(4242)));
+ _ -> exit("NIF library not loaded")
+ end.
deserialize(_Bin) ->
- "NIF library not loaded".
+ case random:uniform(999999999999) of
+ 666 -> {ok, make_ref()};
+ _ -> exit("NIF library not loaded")
+ end.
%% ===================================================================
%% EUnit tests

6 comments on commit f44e237

I understand the value of random:uniform(999999999999) will never be 666 or 667,
regarding the resolution of the random:uniform/1 function.
Are the case statements with the random:uniform/1 expressions some sort of
placeholders to write the -spec directives for each NIF?
(I am curious because I've never imagined this sort of random:uniform/1 usage.)

Contributor

slfritchie replied Oct 6, 2012

This use of random:uniform() is only to fool the Dialyzer, since it couldn't peek into the C shared library's implementation to get type information. There's almost certainly a better way, but I haven't researched what it is, alas.

There's the new-ish erlang:nif_error BIF, but I don't think that would resolve the typing issue.

@slfritchie: points taken. I think it won't do much harm even if those case statement choices are executed accidentally since the probability is very very small, and the execution will not happen if the NIF library is successfully loaded.

Contributor

dizzyd replied Oct 7, 2012

@dizzyd Why didn't you just add | {error, not_loaded} to -spec declarations and simply do not set methods body to {error, not_loader}? It's perfectly fine with dyalizer.

Please sign in to comment.