dkuku
How to handle rust crate features in rustler libraries?
I was curious if rustler can support dynamic crate features and I came up with something like this:
- config.exs, or Mix.install:
config :ex_text_splitter,
features: ["markdown", "tiktoken-rs"]
- Native module
@features Application.compile_env(:ex_text_splitter, :features, []) |> List.wrap()
def text_splitter(_arg1, _arg2), do: err()
if "tiktoken-rs" in @features do
def tokenizer_text_splitter(_arg1, _arg2), do: err()
end
if "markdown" in @features do
def markdown_splitter(_arg1, _arg2), do: err()
end
- But then the hacky part is in rust:
#[cfg(all(feature = "tiktoken-rs", not(feature = "markdown")))]
rustler::init!(
"Elixir.ExTextSplitter.Native",
[text_splitter, tokenizer_text_splitter]
);
#[cfg(all(not(feature = "tiktoken-rs"), feature = "markdown"))]
rustler::init!(
"Elixir.ExTextSplitter.Native",
[markdown_splitter, text_splitter]
);
#[cfg(all(not(feature = "tiktoken-rs"), not(feature = "markdown")))]
rustler::init!("Elixir.ExTextSplitter.Native", [text_splitter]);
I tried to use something like
match (cfg!(markdown), cfg!(feature = "tiktoken-rs")) {
(false, false) => [functions_to_export],
...
}
But the compiler complains because rustler::init! is a macro and it looks for ‘[’. Do you have any suggestions how this could be refactored?
Marked As Solved
filmor
If we merge this Discover NIFs at startup by filmor · Pull Request #613 · rusterlium/rustler · GitHub, you should be able to just #[cfg...] the respective NIFs.
1
Popular in Questions
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
Hi all,
Trying to get some more clarity over utc_datetime and naive_datetime for Ecto:
https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum.
...
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Hi! May someone helps me, please!
I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
Phoenix 1.4.0 released
Phoenix 1.4 is out! This release ships with exciting new features, most notably
with HTTP2 support, improved deve...
New
can someone please explain to me how Enum.reduce works with maps
New
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode.
The solution seems to be, in a hyphena...
New
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Using vs code and installed ElixirLS: support and debugger.
And I got an error popped up on start up says
Failed to run ‘elixir’ comma...
New
Hey,
Just curious what are the main benefits of Elixir compared to Clojure?
When is Elixir more useful than Clojure and vice versa?
Th...
New
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors:
[WARN] - (starship::utils): Executing command ...
New







