-
Notifications
You must be signed in to change notification settings - Fork 225
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #413 from surferlocal/dynamic-load_data
Add dynamic load data
- Loading branch information
Showing
10 changed files
with
145 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use rustler::Atom; | ||
use std::{ffi::OsStr, fs::read_to_string, path::PathBuf}; | ||
|
||
static mut DATASET: Option<Box<str>> = 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 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); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
some random dataset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |