Skip to content

Commit

Permalink
Minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
qryxip committed Jun 30, 2024
1 parent 82c348b commit 2ab092e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 36 deletions.
11 changes: 4 additions & 7 deletions ort-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -392,9 +392,9 @@ fn prepare_libort_dir() -> (PathBuf, bool) {
copy_libraries(&lib_dir.join("lib"), &out_dir);
}

let our_version = include_str!("./VERSION_NUMBER");
const OUR_VERSION: &str = include_str!("./VERSION_NUMBER");
let their_version = fs::read_to_string(lib_dir.join("VERSION_NUMBER")).unwrap_or_else(|e| panic!("`VERSION_NUMBER`を読めませんでした: {e}"));
assert_eq!(our_version.trim_end(), their_version.trim_end(), "`VERSION_NUMBER`が異なります");
assert_eq!(OUR_VERSION.trim_end(), their_version.trim_end(), "`VERSION_NUMBER`が異なります");

(lib_dir, true)
}
Expand Down Expand Up @@ -427,11 +427,8 @@ fn real_main(link: bool) {
fn main() {
if cfg!(feature = "download-binaries") {
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
fs::write(
out_dir.join("downloaded_version.rs"),
format!("#[macro_export] macro_rules! downloaded_version(() => ({:?}));", include_str!("./VERSION_NUMBER").trim_end())
)
.unwrap();
let version = include_str!("./VERSION_NUMBER").trim_end();
fs::write(out_dir.join("downloaded_version.rs"), format!("#[macro_export] macro_rules! downloaded_version(() => ({version:?}));")).unwrap();
}

if env::var("DOCS_RS").is_ok() {
Expand Down
43 changes: 14 additions & 29 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,36 +75,24 @@ pub use self::value::{
ValueRefMut, ValueType, ValueTypeMarker
};

/// このクレートの`load-dynamic`が有効化されていなければコンパイルエラー
/// このクレートのフィーチャが指定された状態になっていなければコンパイルエラー
#[cfg(feature = "load-dynamic")]
#[macro_export]
macro_rules! assert_load_dynamic_is_enabled {
($_:literal $(,)?) => {};
}

/// このクレートの`load-dynamic`が有効化されていなければコンパイルエラー。
#[cfg(not(feature = "load-dynamic"))]
#[macro_export]
macro_rules! assert_load_dynamic_is_enabled {
($msg:literal $(,)?) => {
macro_rules! assert_feature {
(cfg(feature = "load-dynamic"), $msg:literal $(,)?) => {};
(cfg(not(feature = "load-dynamic")), $msg:literal $(,)?) => {
::std::compile_error!($msg);
};
}

/// このクレートの`load-dynamic`が無効化されていなければコンパイルエラー
#[cfg(feature = "load-dynamic")]
/// このクレートのフィーチャが指定された状態になっていなければコンパイルエラー
#[cfg(not(feature = "load-dynamic"))]
#[macro_export]
macro_rules! assert_load_dynamic_is_disabled {
($msg:literal $(,)?) => {
macro_rules! assert_feature {
(cfg(feature = "load-dynamic"), $msg:literal $(,)?) => {
::std::compile_error!($msg);
};
}

/// このクレートの`load-dynamic`が無効化されていなければコンパイルエラー。
#[cfg(not(feature = "load-dynamic"))]
#[macro_export]
macro_rules! assert_load_dynamic_is_disabled {
($_:literal $(,)?) => {};
(cfg(not(feature = "load-dynamic")), $msg:literal $(,)?) => {};
}

#[cfg(not(all(target_arch = "x86", target_os = "windows")))]
Expand Down Expand Up @@ -184,7 +172,7 @@ static G_ENV_FOR_VOICEVOX: once_cell::sync::OnceCell<EnvHandle> = once_cell::syn

#[cfg(feature = "__init-for-voicevox")]
thread_local! {
static G_ORT_API_FOR_ENV_BUILD: std::cell::Cell<Option<AssertSendSync<NonNull<ort_sys::OrtApi>>>> = const { std::cell::Cell::new(None) };
static G_ORT_API_FOR_ENV_BUILD: std::cell::Cell<Option<NonNull<ort_sys::OrtApi>>> = const { std::cell::Cell::new(None) };
}

#[cfg(feature = "__init-for-voicevox")]
Expand Down Expand Up @@ -269,7 +257,7 @@ pub fn try_init_from(filename: &std::ffi::OsStr, tp_options: Option<EnvironmentG
};
let api = AssertSendSync(NonNull::new(api.cast_mut()).unwrap_or_else(|| panic!("`GetApi({ORT_API_VERSION})`が失敗しました")));

let _env = create_env(api, tp_options)?;
let _env = create_env(api.0, tp_options)?;

Ok(EnvHandle { _env, api, dylib })
})
Expand All @@ -295,17 +283,14 @@ pub fn try_init(tp_options: Option<EnvironmentGlobalThreadPoolOptions>) -> anyho
.unwrap_or_else(|| panic!("`GetApi({ORT_API_VERSION})`が失敗しました。おそらく1.{MINOR_VERSION}より古いものがリンクされています"));
let api = AssertSendSync(api);

let _env = create_env(api, tp_options)?;
let _env = create_env(api.0, tp_options)?;

Ok(EnvHandle { _env, api })
})
}

#[cfg(feature = "__init-for-voicevox")]
fn create_env(
api: AssertSendSync<NonNull<ort_sys::OrtApi>>,
tp_options: Option<EnvironmentGlobalThreadPoolOptions>
) -> anyhow::Result<std::sync::Arc<Environment>> {
fn create_env(api: NonNull<ort_sys::OrtApi>, tp_options: Option<EnvironmentGlobalThreadPoolOptions>) -> anyhow::Result<std::sync::Arc<Environment>> {
G_ORT_API_FOR_ENV_BUILD.set(Some(api));
let _unset_api = UnsetOrtApi;

Expand Down Expand Up @@ -340,7 +325,7 @@ pub fn api() -> NonNull<ort_sys::OrtApi> {
return G_ENV_FOR_VOICEVOX
.get()
.map(|&EnvHandle { api: AssertSendSync(api), .. }| api)
.or_else(|| G_ORT_API_FOR_ENV_BUILD.get().map(|AssertSendSync(api)| api))
.or_else(|| G_ORT_API_FOR_ENV_BUILD.get())
.expect("`try_init_from`または`try_init`で初期化されていなくてはなりません");
}
unsafe {
Expand Down

0 comments on commit 2ab092e

Please sign in to comment.