Add from_library for generated dynamic library structs (#2011)
diff --git a/src/codegen/dyngen.rs b/src/codegen/dyngen.rs
index 94dd574..768fb02 100644
--- a/src/codegen/dyngen.rs
+++ b/src/codegen/dyngen.rs
@@ -90,14 +90,20 @@
path: P
) -> Result<Self, ::libloading::Error>
where P: AsRef<::std::ffi::OsStr> {
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+
+ pub unsafe fn from_library<L>(
+ library: L
+ ) -> Self
+ where L: Into<::libloading::Library> {
+ let __library = library.into();
#( #constructor_inits )*
- Ok(
- #lib_ident {
- __library,
- #( #init_fields ),*
- }
- )
+ #lib_ident {
+ __library,
+ #( #init_fields ),*
+ }
}
#( #struct_implementation )*
diff --git a/tests/expectations/tests/dynamic_loading_simple.rs b/tests/expectations/tests/dynamic_loading_simple.rs
index dae273a..c9c4ba1 100644
--- a/tests/expectations/tests/dynamic_loading_simple.rs
+++ b/tests/expectations/tests/dynamic_loading_simple.rs
@@ -31,16 +31,23 @@
where
P: AsRef<::std::ffi::OsStr>,
{
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+ pub unsafe fn from_library<L>(library: L) -> Self
+ where
+ L: Into<::libloading::Library>,
+ {
+ let __library = library.into();
let foo = __library.get(b"foo\0").map(|sym| *sym);
let bar = __library.get(b"bar\0").map(|sym| *sym);
let baz = __library.get(b"baz\0").map(|sym| *sym);
- Ok(TestLib {
+ TestLib {
__library,
foo,
bar,
baz,
- })
+ }
}
pub unsafe fn foo(
&self,
diff --git a/tests/expectations/tests/dynamic_loading_template.rs b/tests/expectations/tests/dynamic_loading_template.rs
index 4d90c62..a46253c 100644
--- a/tests/expectations/tests/dynamic_loading_template.rs
+++ b/tests/expectations/tests/dynamic_loading_template.rs
@@ -19,14 +19,21 @@
where
P: AsRef<::std::ffi::OsStr>,
{
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+ pub unsafe fn from_library<L>(library: L) -> Self
+ where
+ L: Into<::libloading::Library>,
+ {
+ let __library = library.into();
let foo = __library.get(b"foo\0").map(|sym| *sym);
let foo1 = __library.get(b"foo1\0").map(|sym| *sym);
- Ok(TestLib {
+ TestLib {
__library,
foo,
foo1,
- })
+ }
}
pub unsafe fn foo(
&self,
diff --git a/tests/expectations/tests/dynamic_loading_with_allowlist.rs b/tests/expectations/tests/dynamic_loading_with_allowlist.rs
index 0c1d9df..678cd34 100644
--- a/tests/expectations/tests/dynamic_loading_with_allowlist.rs
+++ b/tests/expectations/tests/dynamic_loading_with_allowlist.rs
@@ -33,16 +33,23 @@
where
P: AsRef<::std::ffi::OsStr>,
{
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+ pub unsafe fn from_library<L>(library: L) -> Self
+ where
+ L: Into<::libloading::Library>,
+ {
+ let __library = library.into();
let foo = __library.get(b"foo\0").map(|sym| *sym);
let baz = __library.get(b"baz\0").map(|sym| *sym);
let bazz = __library.get(b"bazz\0").map(|sym| *sym);
- Ok(TestLib {
+ TestLib {
__library,
foo,
baz,
bazz,
- })
+ }
}
pub unsafe fn foo(
&self,
diff --git a/tests/expectations/tests/dynamic_loading_with_blocklist.rs b/tests/expectations/tests/dynamic_loading_with_blocklist.rs
index 15092f0..930876a 100644
--- a/tests/expectations/tests/dynamic_loading_with_blocklist.rs
+++ b/tests/expectations/tests/dynamic_loading_with_blocklist.rs
@@ -77,14 +77,21 @@
where
P: AsRef<::std::ffi::OsStr>,
{
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+ pub unsafe fn from_library<L>(library: L) -> Self
+ where
+ L: Into<::libloading::Library>,
+ {
+ let __library = library.into();
let foo = __library.get(b"foo\0").map(|sym| *sym);
let bar = __library.get(b"bar\0").map(|sym| *sym);
- Ok(TestLib {
+ TestLib {
__library,
foo,
bar,
- })
+ }
}
pub unsafe fn foo(
&self,
diff --git a/tests/expectations/tests/dynamic_loading_with_class.rs b/tests/expectations/tests/dynamic_loading_with_class.rs
index a402e28..81a045b 100644
--- a/tests/expectations/tests/dynamic_loading_with_class.rs
+++ b/tests/expectations/tests/dynamic_loading_with_class.rs
@@ -72,14 +72,21 @@
where
P: AsRef<::std::ffi::OsStr>,
{
- let __library = ::libloading::Library::new(path)?;
+ let library = ::libloading::Library::new(path)?;
+ Ok(Self::from_library(library))
+ }
+ pub unsafe fn from_library<L>(library: L) -> Self
+ where
+ L: Into<::libloading::Library>,
+ {
+ let __library = library.into();
let foo = __library.get(b"foo\0").map(|sym| *sym);
let bar = __library.get(b"bar\0").map(|sym| *sym);
- Ok(TestLib {
+ TestLib {
__library,
foo,
bar,
- })
+ }
}
pub unsafe fn foo(
&self,