Skip to content

Commit 5a055fd

Browse files
authored
Add from_library for generated dynamic library structs (#2011)
1 parent 2a46e29 commit 5a055fd

6 files changed

+63
-22
lines changed

src/codegen/dyngen.rs

+13-7
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,20 @@ impl DynamicItems {
9090
path: P
9191
) -> Result<Self, ::libloading::Error>
9292
where P: AsRef<::std::ffi::OsStr> {
93-
let __library = ::libloading::Library::new(path)?;
93+
let library = ::libloading::Library::new(path)?;
94+
Ok(Self::from_library(library))
95+
}
96+
97+
pub unsafe fn from_library<L>(
98+
library: L
99+
) -> Self
100+
where L: Into<::libloading::Library> {
101+
let __library = library.into();
94102
#( #constructor_inits )*
95-
Ok(
96-
#lib_ident {
97-
__library,
98-
#( #init_fields ),*
99-
}
100-
)
103+
#lib_ident {
104+
__library,
105+
#( #init_fields ),*
106+
}
101107
}
102108

103109
#( #struct_implementation )*

tests/expectations/tests/dynamic_loading_simple.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,23 @@ impl TestLib {
3131
where
3232
P: AsRef<::std::ffi::OsStr>,
3333
{
34-
let __library = ::libloading::Library::new(path)?;
34+
let library = ::libloading::Library::new(path)?;
35+
Ok(Self::from_library(library))
36+
}
37+
pub unsafe fn from_library<L>(library: L) -> Self
38+
where
39+
L: Into<::libloading::Library>,
40+
{
41+
let __library = library.into();
3542
let foo = __library.get(b"foo\0").map(|sym| *sym);
3643
let bar = __library.get(b"bar\0").map(|sym| *sym);
3744
let baz = __library.get(b"baz\0").map(|sym| *sym);
38-
Ok(TestLib {
45+
TestLib {
3946
__library,
4047
foo,
4148
bar,
4249
baz,
43-
})
50+
}
4451
}
4552
pub unsafe fn foo(
4653
&self,

tests/expectations/tests/dynamic_loading_template.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,21 @@ impl TestLib {
1919
where
2020
P: AsRef<::std::ffi::OsStr>,
2121
{
22-
let __library = ::libloading::Library::new(path)?;
22+
let library = ::libloading::Library::new(path)?;
23+
Ok(Self::from_library(library))
24+
}
25+
pub unsafe fn from_library<L>(library: L) -> Self
26+
where
27+
L: Into<::libloading::Library>,
28+
{
29+
let __library = library.into();
2330
let foo = __library.get(b"foo\0").map(|sym| *sym);
2431
let foo1 = __library.get(b"foo1\0").map(|sym| *sym);
25-
Ok(TestLib {
32+
TestLib {
2633
__library,
2734
foo,
2835
foo1,
29-
})
36+
}
3037
}
3138
pub unsafe fn foo(
3239
&self,

tests/expectations/tests/dynamic_loading_with_allowlist.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,23 @@ impl TestLib {
3333
where
3434
P: AsRef<::std::ffi::OsStr>,
3535
{
36-
let __library = ::libloading::Library::new(path)?;
36+
let library = ::libloading::Library::new(path)?;
37+
Ok(Self::from_library(library))
38+
}
39+
pub unsafe fn from_library<L>(library: L) -> Self
40+
where
41+
L: Into<::libloading::Library>,
42+
{
43+
let __library = library.into();
3744
let foo = __library.get(b"foo\0").map(|sym| *sym);
3845
let baz = __library.get(b"baz\0").map(|sym| *sym);
3946
let bazz = __library.get(b"bazz\0").map(|sym| *sym);
40-
Ok(TestLib {
47+
TestLib {
4148
__library,
4249
foo,
4350
baz,
4451
bazz,
45-
})
52+
}
4653
}
4754
pub unsafe fn foo(
4855
&self,

tests/expectations/tests/dynamic_loading_with_blocklist.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,21 @@ impl TestLib {
7777
where
7878
P: AsRef<::std::ffi::OsStr>,
7979
{
80-
let __library = ::libloading::Library::new(path)?;
80+
let library = ::libloading::Library::new(path)?;
81+
Ok(Self::from_library(library))
82+
}
83+
pub unsafe fn from_library<L>(library: L) -> Self
84+
where
85+
L: Into<::libloading::Library>,
86+
{
87+
let __library = library.into();
8188
let foo = __library.get(b"foo\0").map(|sym| *sym);
8289
let bar = __library.get(b"bar\0").map(|sym| *sym);
83-
Ok(TestLib {
90+
TestLib {
8491
__library,
8592
foo,
8693
bar,
87-
})
94+
}
8895
}
8996
pub unsafe fn foo(
9097
&self,

tests/expectations/tests/dynamic_loading_with_class.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -72,14 +72,21 @@ impl TestLib {
7272
where
7373
P: AsRef<::std::ffi::OsStr>,
7474
{
75-
let __library = ::libloading::Library::new(path)?;
75+
let library = ::libloading::Library::new(path)?;
76+
Ok(Self::from_library(library))
77+
}
78+
pub unsafe fn from_library<L>(library: L) -> Self
79+
where
80+
L: Into<::libloading::Library>,
81+
{
82+
let __library = library.into();
7683
let foo = __library.get(b"foo\0").map(|sym| *sym);
7784
let bar = __library.get(b"bar\0").map(|sym| *sym);
78-
Ok(TestLib {
85+
TestLib {
7986
__library,
8087
foo,
8188
bar,
82-
})
89+
}
8390
}
8491
pub unsafe fn foo(
8592
&self,

0 commit comments

Comments
 (0)