nagisa / rust_libloading

Bindings around the platform's dynamic library loading primitives with greatly improved memory safety.
https://docs.rs/libloading
ISC License
1.24k stars 102 forks source link

possibility to read private field handle of library and manual reinitialization #106

Closed bobi6666 closed 2 years ago

bobi6666 commented 2 years ago

Hello, I suggest that a function be added to libloading that would allow me to read the handle of the library and manually read it externally for free, but so that I could then add a new handle there, for example via winapi, because this would help me when reinitializing the library when I use it via lazy static.

nagisa commented 2 years ago

Is https://docs.rs/libloading/latest/libloading/os/windows/struct.Library.html#method.into_raw what you are thinking of?

bobi6666 commented 2 years ago

close is there but close is not what i need because that taking ownership and dropping lib but i would like to free library handle and in to same struct load new library with some external function or load with flags

2022-07-12 21:26 GMT+02:00, Simonas Kazlauskas @.***>:

Is https://docs.rs/libloading/latest/libloading/os/windows/struct.Library.html#method.into_raw what you are thinking of?

-- Reply to this email directly or view it on GitHub: https://github.com/nagisa/rust_libloading/issues/106#issuecomment-1182347762 You are receiving this because you authored the thread.

Message ID: @.***>

nagisa commented 2 years ago

Sorry, I have no idea what you’re trying to achieve from your explanation, nor how close is relevant to your problem. The methods I linked you were into_raw and from_raw. These would give you an ability to convert os::*::Library to and from a raw OS handle, which is what I understood your question to be about. You can convert between the top-level Library and os::*::Library throug their From trait implementations.

If that does not in any way help, then you’ll need to provide some code examples demonstrating your use case, or failing that describe your problem in a different way.

bobi6666 commented 2 years ago

I would need a getter to get the handle of the library inside the lib structure, but then also a setter to be able to change it, because if I wanted a free library now, for example, I wouldn't be able to do that free_current_lib(library.0); due to the fact that this field is private and therefore it is not possible to reload it again via load_with_flags, but it is also not possible to use free without losing the owner ship of the structure

2022-07-12 23:00 GMT+02:00, Simonas Kazlauskas @.***>:

Sorry, I have no idea what you’re trying to achieve from your explanation, nor how close is relevant to your problem. The methods I linked you were into_raw and from_raw. These would give you an ability to convert os::*::Library to and from a raw OS handle, which is what I understood your question to be about. You can convert between the top-level Library and os::*::Library throug their From trait implementations.

If that does not in any way help, then you’ll need to provide some code examples demonstrating your use case, or failing that describe your problem in a different way.

--

Reply to this email directly or view it on GitHub:

https://github.com/nagisa/rust_libloading/issues/106#issuecomment-1182495896

You are receiving this because you authored the thread.

Message ID: @.***>

bobi6666 commented 2 years ago

hope you understood my explanation now but if no i will try to create some pseudo code

2022-07-12 23:07 GMT+02:00, Peter Kubek @.***>:

I would need a getter to get the handle of the library inside the lib structure, but then also a setter to be able to change it, because if I wanted a free library now, for example, I wouldn't be able to do that free_current_lib(library.0); due to the fact that this field is private and therefore it is not possible to reload it again via load_with_flags, but it is also not possible to use free without losing the owner ship of the structure

2022-07-12 23:00 GMT+02:00, Simonas Kazlauskas @.***>:

Sorry, I have no idea what you’re trying to achieve from your explanation, nor how close is relevant to your problem. The methods I linked you were into_raw and from_raw. These would give you an ability to convert os::*::Library to and from a raw OS handle, which is what I understood your question to be about. You can convert between the top-level Library and os::*::Library throug their From trait implementations.

If that does not in any way help, then you’ll need to provide some code examples demonstrating your use case, or failing that describe your problem in a different way.

--

Reply to this email directly or view it on GitHub:

https://github.com/nagisa/rust_libloading/issues/106#issuecomment-1182495896

You are receiving this because you authored the thread.

Message ID: @.***>

nagisa commented 2 years ago

Ah, so what you’re asking for is for Library to internally support nullary states. Doing so is generally frowned upon in the Rust ecosystem, primarily because composition provides a more flexible and elegant mechanism to achieve that already.

Have you considered composing the Library with an Option and potentially some type with interior mutability? Perhaps RefCell<Option<Library>>, or maybe something with a Mutex? In that case a None would represent a “freed” library, and as long as you have a Some(...), you can Option::take out of it and do whatever you want with the library – including closeing it, or converting it to native handles. Once you have a library opened with new flags or whatever, you can Option::replace it to the place of the old Library as well. All without Library needing any special support for nullary states.

bobi6666 commented 2 years ago

do you have some example how to do that? because i tried that with refcell but if i want to free old library and load new static init tells me that i can't move out of static because free calling drop

2022-07-13 15:19 GMT+02:00, Simonas Kazlauskas @.***>:

Ah, so what you’re asking for is for Library to internally support nullary states. Doing so is generally frowned upon in the Rust ecosystem, primarily because composition provides a more flexible and elegant mechanism to achieve that already.

Have you considered composing the Library with an Option and potentially some type with interior mutability? Perhaps RefCell<Option<Library>>, or maybe something with a Mutex? In that case a None would represent a “freed” library, and as long as you have a Some(...), you can Option::take out of it and do whatever you want with the library – including closeing it, or converting it to native handles. Once you have a library opened with new flags or whatever, you can Option::replace it to the place of the old Library as well. All without Library needing any special support for nullary states.

--

Reply to this email directly or view it on GitHub:

https://github.com/nagisa/rust_libloading/issues/106#issuecomment-1183215175

You are receiving this because you authored the thread.

Message ID: @.***>

nagisa commented 2 years ago

Something like this perhaps?

bobi6666 commented 2 years ago

Solved my problem with idea from last message