From 85f384ae1fbf1818e2b0e43695139bd7d79fc420 Mon Sep 17 00:00:00 2001 From: kaaboyae Date: Thu, 30 Dec 2021 00:03:42 +0100 Subject: [PATCH 01/13] add load_data_fun config option --- rustler_mix/lib/rustler.ex | 50 +++++++++++++++++++++- rustler_mix/lib/rustler/compiler/config.ex | 1 + 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/rustler_mix/lib/rustler.ex b/rustler_mix/lib/rustler.ex index 4c1b5f08..dd734cd0 100644 --- a/rustler_mix/lib/rustler.ex +++ b/rustler_mix/lib/rustler.ex @@ -43,6 +43,24 @@ defmodule Rustler do * `:load_data` - Any valid term. This value is passed into the NIF when it is loaded (default: `0`) + * `:load_data_fun` - `{Module, :function}` to dynamically generate `load_data`. + Default value: `nil`. + + This parameter is mutually exclusive with `load_data` + which means that `load_data` has to be set to it's default value. + + Example + + defmodule NIF do + use Rustler, load_data_fun: {Deployment, :nif_data} + end + + defmodule Deployment do + def nif_data do + :code.priv_dir(:otp_app) |> IO.iodata_to_binary() + end + end + * `:load_from` - This option allows control over where the final artifact should be loaded from at runtime. By default the compiled artifact is loaded from the owning `:otp_app`'s `priv/native` directory. This option comes in handy in @@ -86,6 +104,7 @@ defmodule Rustler do if config.lib do @load_from config.load_from @load_data config.load_data + @load_data_fun config.load_data_fun @before_compile Rustler end @@ -109,11 +128,40 @@ defmodule Rustler do |> Application.app_dir(path) |> to_charlist() - :erlang.load_nif(load_path, @load_data) + load_data = Rustler.construct_load_data(@load_data, @load_data_fun) + + :erlang.load_nif(load_path, load_data) end end end + def construct_load_data(load_data, load_data_fun) do + default_load_data_value = %Rustler.Compiler.Config{}.load_data + default_fun_value = %Rustler.Compiler.Config{}.load_data_fun + + case {load_data, load_data_fun} do + {load_data, ^default_fun_value} -> + load_data + + {^default_load_data_value, {module, function}} + when is_atom(module) and is_atom(function) -> + apply(module, function, []) + + {^default_load_data_value, provided_value} -> + raise """ + `load_data` has to be `{Module, :function}`. + Instead received: #{inspect(provided_value)} + """ + + {load_data, load_data_fun} -> + raise """ + Only `load_data` or `load_data_fun` can be provided. Instead received: + >>> load_data: #{inspect(load_data)} + >>> load_data_fun: #{inspect(load_data_fun)} + """ + end + end + @doc false def rustler_version, do: "0.26.0" diff --git a/rustler_mix/lib/rustler/compiler/config.ex b/rustler_mix/lib/rustler/compiler/config.ex index 0c9f8ed9..40ed1325 100644 --- a/rustler_mix/lib/rustler/compiler/config.ex +++ b/rustler_mix/lib/rustler/compiler/config.ex @@ -19,6 +19,7 @@ defmodule Rustler.Compiler.Config do features: [], lib: true, load_data: 0, + load_data_fun: nil, load_from: nil, mode: :release, otp_app: nil, From 22c60423326f2404d3d1c15d4bd68571d02e4b72 Mon Sep 17 00:00:00 2001 From: Mieszko Wawrzyniak Date: Wed, 9 Nov 2022 18:18:16 +0100 Subject: [PATCH 02/13] add dynamic load test --- Cargo.toml | 1 + rustler_tests/lib/dynamic_data.ex | 14 +++++++ rustler_tests/native/dynamic_load/Cargo.toml | 13 +++++++ rustler_tests/native/dynamic_load/src/lib.rs | 40 ++++++++++++++++++++ rustler_tests/priv/.gitkeep | 0 rustler_tests/priv/demo_dataset.txt | 1 + rustler_tests/test/dynamic_data_test.exs | 10 +++++ 7 files changed, 79 insertions(+) create mode 100644 rustler_tests/lib/dynamic_data.ex create mode 100644 rustler_tests/native/dynamic_load/Cargo.toml create mode 100644 rustler_tests/native/dynamic_load/src/lib.rs delete mode 100644 rustler_tests/priv/.gitkeep create mode 100644 rustler_tests/priv/demo_dataset.txt create mode 100644 rustler_tests/test/dynamic_data_test.exs diff --git a/Cargo.toml b/Cargo.toml index 352e5703..4bfa21ad 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,7 @@ members = [ "rustler_tests/native/rustler_test", "rustler_tests/native/rustler_bigint_test", "rustler_tests/native/deprecated_macros", + "rustler_tests/native/dynamic_load", "rustler_tests/native/rustler_compile_tests", "rustler_benchmarks/native/benchmark", ] diff --git a/rustler_tests/lib/dynamic_data.ex b/rustler_tests/lib/dynamic_data.ex new file mode 100644 index 00000000..f61bd912 --- /dev/null +++ b/rustler_tests/lib/dynamic_data.ex @@ -0,0 +1,14 @@ +defmodule DynamicData.Config do + def nif_data do + %{priv_path: :code.priv_dir(:rustler_test) |> IO.iodata_to_binary()} + end +end + +defmodule DynamicData do + use Rustler, + otp_app: :rustler_test, + crate: :dynamic_load, + load_data_fun: {DynamicData.Config, :nif_data} + + def get_dataset, do: :erlang.nif_error(:nif_not_loaded) +end diff --git a/rustler_tests/native/dynamic_load/Cargo.toml b/rustler_tests/native/dynamic_load/Cargo.toml new file mode 100644 index 00000000..469596ea --- /dev/null +++ b/rustler_tests/native/dynamic_load/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "dynamic_load" +version = "0.1.0" +edition = "2021" + +[lib] +name = "dynamic_load" +path = "src/lib.rs" +crate-type = ["cdylib"] + +[dependencies] +rustler_bigint = { path = "../../../rustler_bigint" } +rustler = { path = "../../../rustler" } \ No newline at end of file diff --git a/rustler_tests/native/dynamic_load/src/lib.rs b/rustler_tests/native/dynamic_load/src/lib.rs new file mode 100644 index 00000000..a8f07faf --- /dev/null +++ b/rustler_tests/native/dynamic_load/src/lib.rs @@ -0,0 +1,40 @@ +use rustler::Atom; +use std::{ffi::OsStr, fs::read_to_string, os::unix::prelude::OsStrExt, path::PathBuf}; + +static mut DATASET: Option> = None; + +fn initialize_dataset(mut asset_path: PathBuf) { + asset_path.push("demo_dataset.txt"); + + // https://github.com/elixir-lsp/elixir-ls/issues/604 + eprintln!("Loading dataset from {:?}.", &asset_path); + + let data = read_to_string(asset_path).unwrap().into_boxed_str(); + let data = Some(data); + + // Safety: assumes that this function is being called once when + // dynamically loading this library. + // `load()` is being called exactly once and OTP will not allow any other function call + // before this function returns + unsafe { DATASET = data }; +} + +#[rustler::nif] +fn get_dataset() -> &'static str { + // Safety: see `initialize_dataset()` + unsafe { DATASET.as_ref() }.expect("Dataset is not initialized") +} + +fn load<'a>(env: rustler::Env<'a>, args: rustler::Term<'a>) -> bool { + let key = Atom::from_str(env, "priv_path").unwrap().to_term(env); + let priv_path = args.map_get(key).unwrap(); + let priv_path = priv_path.into_binary().unwrap().as_slice(); + let priv_path = OsStr::from_bytes(priv_path); + let asset_path = PathBuf::from(priv_path); + + initialize_dataset(asset_path); + + true +} + +rustler::init!("Elixir.DynamicData", [get_dataset], load = load); diff --git a/rustler_tests/priv/.gitkeep b/rustler_tests/priv/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/rustler_tests/priv/demo_dataset.txt b/rustler_tests/priv/demo_dataset.txt new file mode 100644 index 00000000..c7eaa228 --- /dev/null +++ b/rustler_tests/priv/demo_dataset.txt @@ -0,0 +1 @@ +some random dataset diff --git a/rustler_tests/test/dynamic_data_test.exs b/rustler_tests/test/dynamic_data_test.exs new file mode 100644 index 00000000..cf33812d --- /dev/null +++ b/rustler_tests/test/dynamic_data_test.exs @@ -0,0 +1,10 @@ +defmodule RustlerTest.DynamicDataTest do + use ExUnit.Case, async: true + + test "rust has access to demo_dataset.txt via dynamic priv path" do + %{priv_path: path} = DynamicData.Config.nif_data() + path = Path.join(path, "demo_dataset.txt") + + assert File.read!(path) == DynamicData.get_dataset() + end +end From ad09cfc49febbcd969fe3404129023ab85c17363 Mon Sep 17 00:00:00 2001 From: Tatsuyuki Ishi Date: Wed, 30 Nov 2022 12:23:22 +0900 Subject: [PATCH 03/13] rustler_codegen: Fix stale doc example for init `Some(load)` has been replaced with `load = load`. Also add a stub for showing how `load` should look like. --- rustler_codegen/src/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/rustler_codegen/src/lib.rs b/rustler_codegen/src/lib.rs index 185e189c..06df98bc 100644 --- a/rustler_codegen/src/lib.rs +++ b/rustler_codegen/src/lib.rs @@ -46,7 +46,11 @@ enum RustlerAttr { /// a / b /// } /// -/// rustler::init!("Elixir.Math", [add, sub, mul, div], Some(load)); +/// fn load(env: Env, _term: Term) -> bool { +/// true +/// } +/// +/// rustler::init!("Elixir.Math", [add, sub, mul, div], load = load); /// ``` #[proc_macro] pub fn init(input: TokenStream) -> TokenStream { From 81ec9125e2232e2620b07761dd57e2165a989b05 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 30 Nov 2022 08:33:23 +0100 Subject: [PATCH 04/13] ci: Add Elixir v1.14 and fix rustler_mix_test job --- .github/workflows/main.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 816873e3..72a1817b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -73,7 +73,7 @@ jobs: - name: Install Erlang/Elixir uses: erlef/setup-beam@v1 with: - otp-version: "24.1.7" + otp-version: "24" elixir-version: 1.13 - name: Test rustler_mix @@ -86,6 +86,7 @@ jobs: strategy: matrix: pair: + - { erlang: "25", elixir: "1.14" } - { erlang: "25", elixir: "1.13" } - { erlang: "24", elixir: "1.13" } - { erlang: "24", elixir: "1.12" } From a63ca292fa7e43dd7c17a105362be5b877e1d8f8 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 30 Nov 2022 08:47:31 +0100 Subject: [PATCH 05/13] ci: Use ubuntu-20.04 for tests as that is required for OTP 23 --- .github/workflows/main.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 72a1817b..fe4c7e3b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -98,7 +98,10 @@ jobs: - nightly os: - windows - - ubuntu + # TODO change this to ubuntu after OTP 23 is deprecated, see + # https://github.com/erlef/setup-beam#compatibility-between-operating-system-and-erlangotp + # for compatibility + - ubuntu-20.04 steps: - name: Checkout sources uses: actions/checkout@v1 From 766670f622867c44987b9e2358e8450d0d122ba7 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 4 Jan 2023 21:40:07 +0100 Subject: [PATCH 06/13] Define MSRV According to https://github.com/foresterre/cargo-msrv, the current MSRV is 1.56.1. Clippy needs to obey this, as new lints may not be available to old versions of Rust. --- .clippy.toml | 1 + README.md | 5 +++++ 2 files changed, 6 insertions(+) create mode 100644 .clippy.toml diff --git a/.clippy.toml b/.clippy.toml new file mode 100644 index 00000000..56ce04e4 --- /dev/null +++ b/.clippy.toml @@ -0,0 +1 @@ +msrv = "1.56.1" diff --git a/README.md b/README.md index 295d9b61..07243fe2 100644 --- a/README.md +++ b/README.md @@ -46,6 +46,11 @@ fn add(a: i64, b: i64) -> i64 { rustler::init!("Elixir.Math", [add]); ``` +#### Minimal Supported Rust Version (MSRV) + +Rustler currently has a minimal supported Rust version (MSRV) of 1.56.1. This +is the configured version in `.clippy.toml`. + #### Supported OTP and Elixir Versions Rustler aims to support the newest three major OTP versions as well as newest three minor Elixir versions. From 9ad80d2c3ff4b43f1999c0c10195e904468d8c2f Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 4 Jan 2023 21:28:51 +0100 Subject: [PATCH 07/13] clippy: unnecessary_cast --- rustler_tests/native/rustler_test/src/test_thread.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustler_tests/native/rustler_test/src/test_thread.rs b/rustler_tests/native/rustler_test/src/test_thread.rs index 2cf853a2..634fb791 100644 --- a/rustler_tests/native/rustler_test/src/test_thread.rs +++ b/rustler_tests/native/rustler_test/src/test_thread.rs @@ -24,7 +24,7 @@ pub fn threaded_sleep(env: Env, msec: u64) -> Atom { let q = msec / 1000; let r = (msec % 1000) as u32; thread::spawn::(env, move |thread_env| { - std::thread::sleep(std::time::Duration::new(q as u64, r * 1_000_000)); + std::thread::sleep(std::time::Duration::new(q, r * 1_000_000)); msec.encode(thread_env) }); From d8a716308cf9e89dd8d4217b554d1288308bd6e6 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Wed, 4 Jan 2023 21:43:33 +0100 Subject: [PATCH 08/13] clippy: needless_borrow --- rustler/build.rs | 2 +- rustler_codegen/src/ex_struct.rs | 2 ++ rustler_codegen/src/map.rs | 2 ++ rustler_sys/build.rs | 2 +- rustler_sys/tests/struct_size.rs | 2 +- 5 files changed, 7 insertions(+), 3 deletions(-) diff --git a/rustler/build.rs b/rustler/build.rs index f9672e6c..bcdadaa9 100644 --- a/rustler/build.rs +++ b/rustler/build.rs @@ -40,7 +40,7 @@ fn get_version_from_erl() -> Option { r#"io:format("~s~n", [erlang:system_info(nif_version)]), init:stop()."#, ]; - let version = Command::new("erl").args(&args).output().ok()?.stdout; + let version = Command::new("erl").args(args).output().ok()?.stdout; let version = String::from_utf8(version).ok()?; diff --git a/rustler_codegen/src/ex_struct.rs b/rustler_codegen/src/ex_struct.rs index d8c01092..e489ca7f 100644 --- a/rustler_codegen/src/ex_struct.rs +++ b/rustler_codegen/src/ex_struct.rs @@ -58,6 +58,8 @@ pub fn transcoder_decorator(ast: &syn::DeriveInput, add_exception: bool) -> Toke } #decoder + + #[allow(clippy::needless_borrow)] #encoder }; diff --git a/rustler_codegen/src/map.rs b/rustler_codegen/src/map.rs index fc82b2cc..8584484e 100644 --- a/rustler_codegen/src/map.rs +++ b/rustler_codegen/src/map.rs @@ -42,6 +42,8 @@ pub fn transcoder_decorator(ast: &syn::DeriveInput) -> TokenStream { } #decoder + + #[allow(clippy::needless_borrow)] #encoder }; diff --git a/rustler_sys/build.rs b/rustler_sys/build.rs index 4c0aa030..0b5aeeb8 100644 --- a/rustler_sys/build.rs +++ b/rustler_sys/build.rs @@ -888,7 +888,7 @@ fn get_version_from_erl() -> Option { r#"io:format("~s~n", [erlang:system_info(nif_version)]), init:stop()."#, ]; - let version = Command::new("erl").args(&args).output().ok()?.stdout; + let version = Command::new("erl").args(args).output().ok()?.stdout; let version = String::from_utf8(version).ok()?; diff --git a/rustler_sys/tests/struct_size.rs b/rustler_sys/tests/struct_size.rs index 279e5250..8fd577e1 100644 --- a/rustler_sys/tests/struct_size.rs +++ b/rustler_sys/tests/struct_size.rs @@ -38,7 +38,7 @@ fn test1() { .arg("-o") .arg(&exe) .arg("-I") - .arg(&erts_include) + .arg(erts_include) .arg("tests/struct_size.c") .status() .map_err(|_| "Can't find c compiler (cc or value of environment CC)") From dcda8571998fa6698c76ce6d4a996fde1de7433d Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Tue, 10 Jan 2023 16:55:55 +0200 Subject: [PATCH 09/13] update rust edition --- rustler_mix/priv/templates/basic/Cargo.toml.eex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustler_mix/priv/templates/basic/Cargo.toml.eex b/rustler_mix/priv/templates/basic/Cargo.toml.eex index 08056579..fd271cfe 100644 --- a/rustler_mix/priv/templates/basic/Cargo.toml.eex +++ b/rustler_mix/priv/templates/basic/Cargo.toml.eex @@ -2,7 +2,7 @@ name = "<%= library_name %>" version = "0.1.0" authors = [] -edition = "2018" +edition = "2021" [lib] name = "<%= library_name %>" From bebce0f729a49b56fff552560aa4684207a9fc99 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Tue, 17 Jan 2023 10:10:28 +0100 Subject: [PATCH 10/13] Fix target for rustler_compile_test The target was copied from rustler_test, but should be different. Fix ed1dff9e. --- rustler_tests/native/rustler_compile_tests/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rustler_tests/native/rustler_compile_tests/Cargo.toml b/rustler_tests/native/rustler_compile_tests/Cargo.toml index d7bec0fc..7dda8044 100644 --- a/rustler_tests/native/rustler_compile_tests/Cargo.toml +++ b/rustler_tests/native/rustler_compile_tests/Cargo.toml @@ -5,7 +5,7 @@ authors = [] edition = "2018" [lib] -name = "rustler_test" +name = "rustler_compile_test" path = "src/lib.rs" crate-type = ["cdylib"] From 1a2ab343446e08569c3c985a50f0d9ea95bc6269 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Tue, 17 Jan 2023 10:03:38 +0100 Subject: [PATCH 11/13] Prepare release v0.27.0 --- CHANGELOG.md | 19 ++++++++++++++++++- UPGRADE.md | 6 ++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a859ddd2..f6bed2cc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,10 +9,27 @@ See [`UPGRADE.md`](./UPGRADE.md) for additional help when upgrading to newer ver ## [unreleased] +## [0.27.0] - 2023-01-17 + ### BREAKING * `MIX_ENV` is no longer considered for determining the build profile. Now, the - profile defaults to `:release`. Use the `:mode` option to pick another profile explicitly. + profile defaults to `:release`. Use the `:mode` option to pick another + profile explicitly. (#496) + +### Added + +* `ResourceArc::make_binary` for safe use of `enif_make_resource_binary` (#487) +* `OwnedBinary` is now `Sync` (#493) +* Specified MSRV to be 1.56.1. + +### Fixed + +* Documentation for `load` (#501, thanks @ishitatsuyuki) + +### Changed + +* Edition 2021 for the rustler mix template (#512, thanks @ayrat555) ## [0.26.0] - 2022-09-02 diff --git a/UPGRADE.md b/UPGRADE.md index e394f645..1cfd27b0 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -2,6 +2,12 @@ This document is intended to simplify upgrading to newer versions by extending the changelog. +## 0.26 -> 0.27 + +`MIX_ENV` is no longer considered for determining the build profile. Now, the +profile defaults to `:release`. Use the `:mode` option to pick another profile +explicitly. See #496. + ## 0.21 -> 0.22 0.22 changes how to define NIFs. Users upgrading to 0.22 should to do these things: From a101c937ff12cb951420ee61e28adc4952908108 Mon Sep 17 00:00:00 2001 From: Magnus Ottenklinger Date: Tue, 17 Jan 2023 10:11:53 +0100 Subject: [PATCH 12/13] (release) 0.27.0 --- rustler/Cargo.toml | 4 ++-- rustler_bigint/Cargo.toml | 2 +- rustler_codegen/Cargo.toml | 2 +- rustler_mix/README.md | 2 +- rustler_mix/lib/rustler.ex | 2 +- rustler_mix/mix.exs | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/rustler/Cargo.toml b/rustler/Cargo.toml index 53386df9..38d6e9f1 100644 --- a/rustler/Cargo.toml +++ b/rustler/Cargo.toml @@ -2,7 +2,7 @@ name = "rustler" description = "Safe Rust wrappers for creating Erlang NIF functions" repository = "https://github.com/rusterlium/rustler" -version = "0.26.0" # rustler version +version = "0.27.0" # rustler version authors = ["Hansihe "] license = "MIT/Apache-2.0" readme = "../README.md" @@ -15,7 +15,7 @@ alternative_nif_init_name = [] [dependencies] lazy_static = "1.4" -rustler_codegen = { path = "../rustler_codegen", version = "0.26.0", optional = true} +rustler_codegen = { path = "../rustler_codegen", version = "0.27.0", optional = true} rustler_sys = { path = "../rustler_sys", version = "~2.2" } [package.metadata.release] diff --git a/rustler_bigint/Cargo.toml b/rustler_bigint/Cargo.toml index ae9cb7d2..489df5fe 100644 --- a/rustler_bigint/Cargo.toml +++ b/rustler_bigint/Cargo.toml @@ -11,4 +11,4 @@ keywords = ["bigint", "Erlang", "Elixir"] [dependencies] num-bigint = {version = "0.4"} -rustler = {path = "../rustler", version = "0.26.0"} +rustler = {path = "../rustler", version = "0.27.0"} diff --git a/rustler_codegen/Cargo.toml b/rustler_codegen/Cargo.toml index e6731ddd..3982bd2b 100644 --- a/rustler_codegen/Cargo.toml +++ b/rustler_codegen/Cargo.toml @@ -2,7 +2,7 @@ name = "rustler_codegen" description = "Compiler plugin for Rustler" repository = "https://github.com/rusterlium/rustler/tree/master/rustler_codegen" -version = "0.26.0" # rustler_codegen version +version = "0.27.0" # rustler_codegen version authors = ["Hansihe "] license = "MIT/Apache-2.0" readme = "../README.md" diff --git a/rustler_mix/README.md b/rustler_mix/README.md index e90ebc63..bde8dac3 100644 --- a/rustler_mix/README.md +++ b/rustler_mix/README.md @@ -15,7 +15,7 @@ This package is available on [Hex.pm](https://hex.pm/packages/rustler). To insta ```elixir def deps do [ - {:rustler, "~> 0.26.0"} + {:rustler, "~> 0.27.0"} ] end ``` diff --git a/rustler_mix/lib/rustler.ex b/rustler_mix/lib/rustler.ex index dd734cd0..758c0683 100644 --- a/rustler_mix/lib/rustler.ex +++ b/rustler_mix/lib/rustler.ex @@ -163,7 +163,7 @@ defmodule Rustler do end @doc false - def rustler_version, do: "0.26.0" + def rustler_version, do: "0.27.0" @doc """ Supported NIF API versions. diff --git a/rustler_mix/mix.exs b/rustler_mix/mix.exs index d4c3d8bc..127c9f29 100644 --- a/rustler_mix/mix.exs +++ b/rustler_mix/mix.exs @@ -2,7 +2,7 @@ defmodule Rustler.Mixfile do use Mix.Project @source_url "https://github.com/rusterlium/rustler" - @version "0.26.0" + @version "0.27.0" def project do [ From 51bf30ef2fee33f472e127946a50e3e12222d881 Mon Sep 17 00:00:00 2001 From: Mieszko Wawrzyniak Date: Sun, 19 Mar 2023 18:15:49 +0100 Subject: [PATCH 13/13] add windows support --- rustler_tests/native/dynamic_load/src/lib.rs | 21 +++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/rustler_tests/native/dynamic_load/src/lib.rs b/rustler_tests/native/dynamic_load/src/lib.rs index a8f07faf..bd5c9ce6 100644 --- a/rustler_tests/native/dynamic_load/src/lib.rs +++ b/rustler_tests/native/dynamic_load/src/lib.rs @@ -1,5 +1,5 @@ use rustler::Atom; -use std::{ffi::OsStr, fs::read_to_string, os::unix::prelude::OsStrExt, path::PathBuf}; +use std::{ffi::OsStr, fs::read_to_string, path::PathBuf}; static mut DATASET: Option> = None; @@ -29,12 +29,27 @@ fn load<'a>(env: rustler::Env<'a>, args: rustler::Term<'a>) -> bool { let key = Atom::from_str(env, "priv_path").unwrap().to_term(env); let priv_path = args.map_get(key).unwrap(); let priv_path = priv_path.into_binary().unwrap().as_slice(); - let priv_path = OsStr::from_bytes(priv_path); - let asset_path = PathBuf::from(priv_path); + + let asset_path = build_path_buf(priv_path); initialize_dataset(asset_path); true } +#[cfg(unix)] +fn build_path_buf(priv_path: &[u8]) -> PathBuf { + use std::os::unix::prelude::OsStrExt; + + let priv_path = OsStr::from_bytes(priv_path); + PathBuf::from(priv_path) +} + +#[cfg(windows)] +fn build_path_buf(priv_path: &[u8]) -> PathBuf { + let string_slice = std::str::from_utf8(priv_path).expect("Data is not valid UTF-8, we could decode it without valid UTF-8 requirements but lets not do that for now because its easier this way"); + let priv_path = OsStr::new(string_slice); + PathBuf::from(priv_path) +} + rustler::init!("Elixir.DynamicData", [get_dataset], load = load);