Skip to content
zig nif assistance
Elixir C Zig HTML
Branch: master
Clone or download
Latest commit 157f037 Oct 12, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
assets guarded function adapters complete Oct 12, 2019
config add in support for different zig release modes Oct 9, 2019
lib Merge pull request #24 from ityonemo/work-on-guards Oct 12, 2019
test
zig guarded function adapters complete Oct 12, 2019
.gitignore refactor for cleaner elixir imports and also test out the new allocator Oct 7, 2019
README.md s/elixir/beam/g Oct 9, 2019
mix.exs refactor for cleaner elixir imports and also test out the new allocator Oct 7, 2019
mix.lock refactor for cleaner elixir imports and also test out the new allocator Oct 7, 2019

README.md

Zigler

Zig Nifs made easy

Wouldn't it be nice if you could make NIFs as easily as you can use the asm keyword in C?

This is now possible, using the magic of Zig.

defmodule ExampleZig do
  use Zigler, app: :my_app

  ~Z"""
  @nif("example_fun")
  fn example_fun(value1: f64, value2: f64) bool {
    return value1 > value2;
  }
  """

end

iex> ExampleZig.example_fun(0.1, 0.4)
false

iex> ExampleZig.example_fun(0.8, -0.8)
true

Zigler will do automatic type marshalling between Elixir code and Zig code. It will also convert trickier types into types you care about, for example:

defmodule ZigCollections do
  use Zigler, app: :my_app
  ~Z"""
  @nif("string_count")
  fn string_count(string: []u8) i64 {
    return @intCast(i64, string.len);
  }

  @nif("list_sum")
  fn list_sum(array: []f64) f64 {
    var sum: f64 = 0.0;
    for(array) | item | {
      sum += item;
    }
    return sum;
  }
  """
end

iex> ZigCollections.string_count("hello zig")
9
iex> ZigCollections.list_sum([1.0, 2.0, 3.0])
6.0

Memory allocation with zigler is easy! A standard BEAM allocator is provided for you, so any zig code you import will play nice with the BEAM.

defmodule Allocations do
  use Zigler, app: :my_app
  ~Z"""
  @nif("double_atom")
  fn double_atom(env: beam.env, string: []u8) beam.atom {
    var double_string = beam.allocator.alloc(u8, string.len * 2)
      catch beam.enomem(env);

    defer beam.allocator.free(double_string);

    for (string) | char, i | {
      double_string[i] = char;
      double_string[i + string.len] = char;
    }

    return beam.make_atom(env, double_string);
  }
  """
end

iex> Allocations.double_atom("foo")
:foofoo

Zigler even has support for zig docstrings.

defmodule AllTheDocs do
  use Zigler, app: :zigler
  ~Z"""
  /// a zero-arity function which returns 47.
  @nif("zeroarity")
  fn zeroarity() i64 {
    return 47;
  }
  """
end

iex> h AllTheDocs.zeroarity

                                def zeroarity()                                 

a zero-arity function which returns 47.

Installation

def deps do
  [
    {:zigler, git: "https://github.com/ityonemo/zigler.git"}
  ]
end

once you have this dependency, you should cache the zig build tools by running the following:

mix zigler.get_zig latest

Future installation

If available in Hex, the package can be installed by adding zigler to your list of dependencies in mix.exs:

def deps do
  [
    {:zigler, "~> 0.1.0"}
  ]
end

You'll also want to grab the latest zig binary and cache it with zigler. You can do this by executing:

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/zigler.

You can’t perform that action at this time.