ExCodecs.NIF (ex_codecs v0.2.3)

Copy Markdown View Source

Shared NIF result wrapping and the library-wide decompress size ceiling.

Compression codecs call safe_call/2 around ExCodecs.Native functions so callers always see {:ok, binary()} or {:error, %ExCodecs.Error{}}, never raw :erlang.nif_error/1 exceptions on the public API.

Safety policy

Decompression defaults to a 256 MiB output ceiling (default_max_output_size/0). Override per call with max_output_size: (bytes). Exceeding the limit returns :output_limit_exceeded.

Panic classification

safe_call/2 maps :nif_not_loaded precisely. Panic-like ErlangError payloads are logged at warning and returned as :compression_failed with message "Native codec crashed".

Summary

Functions

Default decompress ceiling in bytes (256 MiB).

Reads :max_output_size from opts, defaulting to default_max_output_size/0.

Invokes a zero-arity NIF function and wraps ErlangError/nif_not_loaded.

Wraps raw NIF {:ok, binary} / {:error, atom} results into ExCodecs.Error.

Functions

default_max_output_size()

@spec default_max_output_size() :: pos_integer()

Default decompress ceiling in bytes (256 MiB).

max_output_size(opts)

@spec max_output_size(keyword()) ::
  {:ok, pos_integer()} | {:error, ExCodecs.Error.t()}

Reads :max_output_size from opts, defaulting to default_max_output_size/0.

safe_call(codec, fun)

@spec safe_call(atom(), (-> term())) :: {:ok, binary()} | {:error, ExCodecs.Error.t()}

Invokes a zero-arity NIF function and wraps ErlangError/nif_not_loaded.

wrap(codec, other)

@spec wrap(atom(), {:ok, binary()} | {:error, atom()} | term()) ::
  {:ok, binary()} | {:error, ExCodecs.Error.t()}

Wraps raw NIF {:ok, binary} / {:error, atom} results into ExCodecs.Error.