From 5a055fde1d4b8937396914c4089a3f9883380786 Mon Sep 17 00:00:00 2001 From: "Wang, Chi" Date: Tue, 23 Mar 2021 03:04:59 +0800 Subject: [PATCH] Add from_library for generated dynamic library structs (#2011) --- src/codegen/dyngen.rs | 20 ++++++++++++------- .../tests/dynamic_loading_simple.rs | 13 +++++++++--- .../tests/dynamic_loading_template.rs | 13 +++++++++--- .../tests/dynamic_loading_with_allowlist.rs | 13 +++++++++--- .../tests/dynamic_loading_with_blocklist.rs | 13 +++++++++--- .../tests/dynamic_loading_with_class.rs | 13 +++++++++--- 6 files changed, 63 insertions(+), 22 deletions(-) diff --git a/src/codegen/dyngen.rs b/src/codegen/dyngen.rs index 94dd574b..768fb02e 100644 --- a/src/codegen/dyngen.rs +++ b/src/codegen/dyngen.rs @@ -90,14 +90,20 @@ impl DynamicItems { path: P ) -> Result 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( + 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 dae273a6..c9c4ba15 100644 --- a/tests/expectations/tests/dynamic_loading_simple.rs +++ b/tests/expectations/tests/dynamic_loading_simple.rs @@ -31,16 +31,23 @@ impl TestLib { 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(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 4d90c62f..a46253c6 100644 --- a/tests/expectations/tests/dynamic_loading_template.rs +++ b/tests/expectations/tests/dynamic_loading_template.rs @@ -19,14 +19,21 @@ impl TestLib { 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(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 0c1d9df2..678cd340 100644 --- a/tests/expectations/tests/dynamic_loading_with_allowlist.rs +++ b/tests/expectations/tests/dynamic_loading_with_allowlist.rs @@ -33,16 +33,23 @@ impl TestLib { 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(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 15092f06..930876a4 100644 --- a/tests/expectations/tests/dynamic_loading_with_blocklist.rs +++ b/tests/expectations/tests/dynamic_loading_with_blocklist.rs @@ -77,14 +77,21 @@ impl TestLib { 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(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 a402e288..81a045b5 100644 --- a/tests/expectations/tests/dynamic_loading_with_class.rs +++ b/tests/expectations/tests/dynamic_loading_with_class.rs @@ -72,14 +72,21 @@ impl TestLib { 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(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,