From 9860b8f2efe52fe749c3526e980017debe5ac3c4 Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Mon, 10 Nov 2025 19:33:36 +0200 Subject: [PATCH] feat(dyngen): preconvert path to `OsStr` when opening the library In libloading 0.9 the argument bound changed from `AsRef` to `libloading::AsFilename`. This was necessary in order to enable compilation of libloading for usecases that cannot depend on libstd. `libloading::AsFilename` supports most of the same types, but cannot be implemented generally for any `T: AsRef` due to the orphan rule. This makes the generated code support both 0.8 and 0.9 versions of libloading transparently, and at the same time it also reduces the number of different instantiations of `Library::new`. When bindgen is adjusted to work with libloading 0.9 only it will be worthwhile to adjust the generation of `from_library` to use `c"string"` literals over `b"string\0"` ones as those can forgo any runtime checking of the argument(s). However we can't do it right while libloading 0.8 is still supported. --- .../tests/expectations/tests/dynamic_loading_attributes.rs | 1 + .../tests/expectations/tests/dynamic_loading_required.rs | 1 + bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs | 1 + .../tests/expectations/tests/dynamic_loading_template.rs | 1 + .../expectations/tests/dynamic_loading_variable_required.rs | 1 + .../tests/expectations/tests/dynamic_loading_variable_simple.rs | 1 + .../tests/dynamic_loading_variable_with_allowlist.rs | 1 + .../tests/expectations/tests/dynamic_loading_with_allowlist.rs | 1 + .../tests/expectations/tests/dynamic_loading_with_blocklist.rs | 1 + .../tests/expectations/tests/dynamic_loading_with_class.rs | 1 + .../expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs | 1 + bindgen/codegen/dyngen.rs | 1 + 12 files changed, 12 insertions(+) diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs index a1fb1b0cc1..36be3d4266 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_attributes.rs @@ -12,6 +12,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs index 5c929ffc3d..654a6d5dfe 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_required.rs @@ -15,6 +15,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs index 9a4cfaf570..aa50d2381e 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_simple.rs @@ -22,6 +22,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs index 1f63a7893f..b72ee4f6ef 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_template.rs @@ -12,6 +12,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_required.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_required.rs index a96efbe546..ab8f65aa3a 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_required.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_required.rs @@ -9,6 +9,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_simple.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_simple.rs index ced70dbdba..1853e24fdf 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_simple.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_simple.rs @@ -9,6 +9,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_with_allowlist.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_with_allowlist.rs index 3f29e73814..6df4404faa 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_with_allowlist.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_variable_with_allowlist.rs @@ -9,6 +9,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs index 34ebe8d9a5..4e0a548374 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_allowlist.rs @@ -19,6 +19,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs index 8c86674f7a..5b8a8b07ae 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_blocklist.rs @@ -54,6 +54,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs index 65ff2b2f72..64a35ceac9 100644 --- a/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs +++ b/bindgen-tests/tests/expectations/tests/dynamic_loading_with_class.rs @@ -51,6 +51,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = ::libloading::Library::new(path)?; Self::from_library(library) } diff --git a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs index 05be5d9944..f03e7b488b 100644 --- a/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs +++ b/bindgen-tests/tests/expectations/tests/wrap_unsafe_ops_dynamic_loading_simple.rs @@ -23,6 +23,7 @@ impl TestLib { where P: AsRef<::std::ffi::OsStr>, { + let path = path.as_ref(); let library = unsafe { ::libloading::Library::new(path) }?; unsafe { Self::from_library(library) } } diff --git a/bindgen/codegen/dyngen.rs b/bindgen/codegen/dyngen.rs index 76f3805795..59168187c2 100644 --- a/bindgen/codegen/dyngen.rs +++ b/bindgen/codegen/dyngen.rs @@ -106,6 +106,7 @@ impl DynamicItems { path: P ) -> Result where P: AsRef<::std::ffi::OsStr> { + let path = path.as_ref(); let library = #library_new?; #from_library }