Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating Rust collator resource references usable from Elixir #13

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/cldr_collation/nif.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ defmodule Cldr.Collation.Nif do
otp_app: :ex_cldr_collation

def sort(_locale, _list, _opts), do: :erlang.nif_error(:nif_not_loaded)
def sort_using_collator(_collator, _list), do: :erlang.nif_error(:nif_not_loaded)
def create_collator(_locale, _opts), do: :erlang.nif_error(:nif_not_loaded)
end
1 change: 1 addition & 0 deletions native/ex_cldr_collation/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions native/ex_cldr_collation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ crate-type = ["cdylib"]
[dependencies]
icu_collator = "1.2.0"
icu_locid = "1.2.0"
icu_provider = { version = "1.2.0", features = ["sync"] }
icu_testdata = "1.2.0"
rustler = "0.28.0"
17 changes: 17 additions & 0 deletions native/ex_cldr_collation/src/collator.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use icu_collator::Collator;
use icu_locid::Locale;
use rustler::NifResult;

fn parse_locale(locale_tag: &str) -> NifResult<Locale> {
locale_tag.parse().map_err(|_| rustler::Error::BadArg)
}

pub fn new(
locale_tag: &str,
opts: impl Into<icu_collator::CollatorOptions>,
) -> NifResult<Collator> {
let locale: Locale = parse_locale(locale_tag)?;
let collator =
Collator::try_new_unstable(&icu_testdata::unstable(), &locale.into(), opts.into()).unwrap();
Ok(collator)
}
2 changes: 0 additions & 2 deletions native/ex_cldr_collation/src/collator_opts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
/// on its fields being stable.
/// Thus we need to create our own version of CollatorOptions that we can use in our NIF API, and can be easily converted to icu_collator::ComparisonOptions.
/// This also allows us the flexibily to adjust the NIF API if we so choose, while still being compatible with icu_collator.

use rustler::{NifMap, NifUnitEnum};

#[derive(NifUnitEnum)]
Expand Down Expand Up @@ -149,4 +148,3 @@ impl From<CollatorOptions> for icu_collator::CollatorOptions {
collator_options
}
}

36 changes: 29 additions & 7 deletions native/ex_cldr_collation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,45 @@
mod collator;
mod collator_opts;
mod resource;

use icu_collator::*;
use icu_locid::Locale;
use collator_opts::CollatorOptions;
use rustler::NifResult;
use resource::{CollatorResource, CollatorResourceArc};
use rustler::{Env, NifResult};

#[rustler::nif]
fn create_collator(locale_tag: &str, opts: CollatorOptions) -> NifResult<CollatorResourceArc> {
let collator = collator::new(locale_tag, opts)?;
Ok(CollatorResource::new_arc(collator))
}

#[rustler::nif]
fn sort<'a>(
locale_tag: &str,
list: Vec<&'a str>,
opts: CollatorOptions,
) -> NifResult<Vec<&'a str>> {
let locale: Locale = locale_tag.parse().map_err(|_| rustler::Error::BadArg)?;
let collator =
Collator::try_new_unstable(&icu_testdata::unstable(), &locale.into(), opts.into()).unwrap();
let collator = collator::new(locale_tag, opts)?;

let mut list = list;
list.sort_by(|first, second| collator.compare(first, second));
Ok(list)
}

rustler::init!("Elixir.Cldr.Collation.Nif", [sort]);
#[rustler::nif]
fn sort_using_collator(collator_arc: CollatorResourceArc, list: Vec<&str>) -> Vec<&str> {
let collator = collator_arc.collator().lock().unwrap();
let mut list = list;
list.sort_by(|first, second| collator.compare(first, second));
list
}

fn load(env: Env, _info: rustler::Term) -> bool {
rustler::resource!(CollatorResource, env);
true
}

rustler::init!(
"Elixir.Cldr.Collation.Nif",
[sort, create_collator, sort_using_collator],
load = load
);
25 changes: 25 additions & 0 deletions native/ex_cldr_collation/src/resource.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use icu_collator::Collator;
use rustler::ResourceArc;
use std::sync::Mutex;

pub struct CollatorResource {
collator: Mutex<Collator>,
}

impl CollatorResource {
pub fn new(collator: Collator) -> Self {
Self {
collator: Mutex::new(collator),
}
}

pub fn new_arc(collator: Collator) -> ResourceArc<Self> {
ResourceArc::new(Self::new(collator))
}

pub fn collator(&self) -> &Mutex<Collator> {
&self.collator
}
}

pub type CollatorResourceArc = ResourceArc<CollatorResource>;