rust-ffi-guide | A guide for doing FFI using Rust | Wrapper library
kandi X-RAY | rust-ffi-guide Summary
kandi X-RAY | rust-ffi-guide Summary
A guide for doing FFI using Rust
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of rust-ffi-guide
rust-ffi-guide Key Features
rust-ffi-guide Examples and Code Snippets
Community Discussions
Trending Discussions on rust-ffi-guide
QUESTION
I was following Michael-F-Bryan's Dynamic Loading & Plugins chapter from his Rust FFI guide, but I am storing the plugins in a HashMap
(HashMap<&'static str, Box
) instead of a Vec
so that I can call the functions of a Plugin
individually.
I would like the plugins to define a asynchronous function with it's own loop that communicates with the main part of the application using channels (std
or tokio
).
Working around the fact that you can't have async
functions in traits was easy thanks to the async_trait crate, but the issue that I am now facing is, that I cannot spawn a new thread with the module because the new thread might outlive the PluginManager
.
I tried to recreate this in a rust playground without the dynamic modules and I'm facing the same error here (note: this does not include any kind of channel communication)
Unlike the first error, I was unable to recreate a rust playground for the second one (as it happens at runtime). Instead of spawning a new thread, I was using tokio's event loop to handle the async
functions. This works in the sandbox, but not when using a shared library as plugin. At runtime it throws:
ANSWER
Answered 2021-Jan-08 at 00:03You've already recognized the problem: a reference to an object owned by the PluginManager
is being moved into a future that is being spawned, and thus may run on another thread than the one which owns the PluginManager
. The compiler cannot know that the future will not outlive the PluginManager
.
There are a number of possible solutions to this, for example:
Store the plugin instances inside
Arc>
, so that they are reference counted in runtime. Then even if the plugin manager did not live as long as the futures you are spawning, it would not matterMake the
PluginManager
an immutable static singleton
In this case I suspect you want the PluginManager
to be a singleton. You can do that by replacing the new
constructor with a get
method that lazily constructs the instance and returns a reference to it:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rust-ffi-guide
Rust is installed and managed by the rustup tool. Rust has a 6-week rapid release process and supports a great number of platforms, so there are many builds of Rust available at any time. Please refer rust-lang.org for more information.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page