Skip to content

Commit cca50ff

Browse files
committed
first commit
0 parents  commit cca50ff

File tree

13 files changed

+237
-0
lines changed

13 files changed

+237
-0
lines changed

.formatter.exs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Used by "mix format"
2+
[
3+
inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"]
4+
]

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# The directory Mix will write compiled artifacts to.
2+
/_build/
3+
4+
# If you run "mix test --cover", coverage assets end up here.
5+
/cover/
6+
7+
# The directory Mix downloads your dependencies sources to.
8+
/deps/
9+
10+
# Where 3rd-party dependencies like ExDoc output generated docs.
11+
/doc/
12+
13+
# Ignore .fetch files in case you like to edit your project deps locally.
14+
/.fetch
15+
16+
# If the VM crashes, it generates a dump, let's ignore it too.
17+
erl_crash.dump
18+
19+
# Also ignore archive artifacts (built via "mix archive.build").
20+
*.ez
21+
22+
# Ignore package tarball (built via "mix hex.build").
23+
rust_elixir-*.tar
24+
25+
26+
/native/niftest/Cargo.lock
27+
/priv/native/*.so
28+
/native/niftest/target
29+

README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# RustElixir
2+
3+
**TODO: Add description**
4+
5+
## Installation
6+
7+
If [available in Hex](https://hex.pm/docs/publish), the package can be installed
8+
by adding `rust_elixir` to your list of dependencies in `mix.exs`:
9+
10+
```elixir
11+
def deps do
12+
[
13+
{:rust_elixir, "~> 0.1.0"}
14+
]
15+
end
16+
```
17+
18+
Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
19+
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
20+
be found at [https://hexdocs.pm/rust_elixir](https://hexdocs.pm/rust_elixir).
21+

config/config.exs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# This file is responsible for configuring your application
2+
# and its dependencies with the aid of the Mix.Config module.
3+
use Mix.Config
4+
5+
# This configuration is loaded before any dependency and is restricted
6+
# to this project. If another project depends on this project, this
7+
# file won't be loaded nor affect the parent project. For this reason,
8+
# if you want to provide default values for your application for
9+
# 3rd-party users, it should be done in your "mix.exs" file.
10+
11+
# You can configure your application as:
12+
#
13+
# config :rust_elixir, key: :value
14+
#
15+
# and access this configuration in your application as:
16+
#
17+
# Application.get_env(:rust_elixir, :key)
18+
#
19+
# You can also configure a 3rd-party app:
20+
#
21+
# config :logger, level: :info
22+
#
23+
24+
# It is also possible to import configuration files, relative to this
25+
# directory. For example, you can emulate configuration per environment
26+
# by uncommenting the line below and defining dev.exs, test.exs and such.
27+
# Configuration from the imported file will override the ones defined
28+
# here (which is why it is important to import them last).
29+
#
30+
# import_config "#{Mix.env}.exs"

lib/rust_elixir.ex

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
defmodule RustElixir do
2+
@moduledoc """
3+
Documentation for RustElixir.
4+
"""
5+
6+
@doc """
7+
Hello world.
8+
9+
## Examples
10+
11+
iex> RustElixir.hello
12+
:world
13+
14+
"""
15+
def hello do
16+
:world
17+
end
18+
19+
def add(x,y) do
20+
NifTest.add(x,y)
21+
end
22+
end

lib/rust_elixir/native.ex

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
defmodule NifNotLoadedError do
2+
defexception message: "nif not loaded"
3+
end
4+
5+
defmodule NifTest do
6+
use Rustler, otp_app: :rust_elixir, crate: "niftest"
7+
8+
def add(_x,_y), do: err()
9+
10+
defp err() do
11+
throw(NifNotLoadedError)
12+
end
13+
end

mix.exs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
defmodule RustElixir.MixProject do
2+
use Mix.Project
3+
4+
def project do
5+
[
6+
app: :rust_elixir,
7+
version: "0.1.0",
8+
elixir: "~> 1.6",
9+
compilers: [:rustler] ++ Mix.compilers,
10+
start_permanent: Mix.env() == :prod,
11+
rustler_crates: rustler_crates(),
12+
deps: deps()
13+
]
14+
end
15+
16+
# Run "mix help compile.app" to learn about applications.
17+
def application do
18+
[
19+
extra_applications: [:logger]
20+
]
21+
end
22+
23+
# Run "mix help deps" to learn about dependencies.
24+
defp deps do
25+
[
26+
{:rustler, "~> 0.16.0"}
27+
# {:dep_from_hexpm, "~> 0.3.0"},
28+
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"},
29+
]
30+
end
31+
32+
defp rustler_crates do
33+
[niftest: [
34+
path: "native/niftest",
35+
mode: rustc_mode(Mix.env)
36+
]]
37+
end
38+
defp rustc_mode(:prod), do: :release
39+
defp rustc_mode(_), do: :debug
40+
end

mix.lock

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
%{
2+
"rustler": {:hex, :rustler, "0.16.0", "9b04237d2e7b30fcae40a28edb56f59aad8f4e3c8790f4996b5f200f649964be", [:mix], [], "hexpm"},
3+
}

native/niftest/Cargo.toml

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "niftest"
3+
version = "0.1.0"
4+
authors = []
5+
6+
[lib]
7+
name = "niftest"
8+
path = "src/lib.rs"
9+
crate-type = ["dylib"]
10+
11+
[dependencies]
12+
rustler = "0.16.0"
13+
rustler_codegen = "0.16.0"
14+
lazy_static = "0.2"

native/niftest/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Nif for Elixir.NifTest
2+
3+
## To build the NIF module:
4+
5+
* Make sure your projects `mix.exs` has the `:rustler` compiler listed in the `project` function: `compilers: [:rustler] ++ Mix.compilers` If there already is a `:compilers` list, you should append `:rustler` to it.
6+
* Add your crate to the `rustler_crates` attribute in the `project function. [See here](https://hexdocs.pm/rustler/basics.html#crate-configuration).
7+
* Your NIF will now build along with your project.
8+
9+
## To load the NIF:
10+
11+
```elixir
12+
defmodule NifTest do
13+
use Rustler, otp_app: [otp app], crate: "niftest"
14+
15+
# When your NIF is loaded, it will override this function.
16+
def add(_a, _b), do: :erlang.nif_error(:nif_not_loaded)
17+
end
18+
```
19+
20+
## Examples
21+
[This](https://github.com/hansihe/NifIo) is a complete example of a NIF written in Rust.

native/niftest/src/lib.rs

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#[macro_use] extern crate rustler;
2+
#[macro_use] extern crate rustler_codegen;
3+
#[macro_use] extern crate lazy_static;
4+
5+
use rustler::{NifEnv, NifTerm, NifResult, NifEncoder};
6+
7+
mod atoms {
8+
rustler_atoms! {
9+
atom ok;
10+
//atom error;
11+
//atom __true__ = "true";
12+
//atom __false__ = "false";
13+
}
14+
}
15+
16+
rustler_export_nifs! {
17+
"Elixir.NifTest",
18+
[("add", 2, add)],
19+
None
20+
}
21+
22+
fn add<'a>(env: NifEnv<'a>, args: &[NifTerm<'a>]) -> NifResult<NifTerm<'a>> {
23+
let num1: i64 = try!(args[0].decode());
24+
let num2: i64 = try!(args[1].decode());
25+
26+
Ok((atoms::ok(), num1 + num2).encode(env))
27+
}

test/rust_elixir_test.exs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
defmodule RustElixirTest do
2+
use ExUnit.Case
3+
doctest RustElixir
4+
5+
test "greets the world" do
6+
assert RustElixir.hello() == :world
7+
end
8+
9+
test "native call" do
10+
assert RustElixir.add(1, 2) == {:ok, 3}
11+
end
12+
end

test/test_helper.exs

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ExUnit.start()

0 commit comments

Comments
 (0)