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);