rust-ffi | generate bindings to a rust foreign function interface | Wrapper library
kandi X-RAY | rust-ffi Summary
kandi X-RAY | rust-ffi Summary
This tool is split into several parts.
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
rust-ffi Key Features
rust-ffi Examples and Code Snippets
Community Discussions
Trending Discussions on rust-ffi
QUESTION
I try to pass a string
as a function argument to a Rust library (cdylib
) as described in the Rust FFI Omnibus.
I tried to however omit the libc
dependency, because I think it should not be necessary anymore.
I am using Rust 1.50.0
and .net 5.0.103
.
From the the documentation it seems to me as if the CStr::from_ptr()
function constructs a CStr
from the pointer by reading all bytes until the null-termination. And that C# strings are automatically marshalled to C compatible strings (and are therefore null-terminated). My problem however is, that I do not get the full string that I supply as the function argument, instead I only get the first character as the string.
This is my lib.rs
:
ANSWER
Answered 2021-Mar-11 at 13:17You need to use CharSet = CharSet.Ansi
which does seem to be the default.
When I replace
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:
QUESTION
I have followed the instructions from here and here
And although the .a
library and the functions work as expected on Debug (simulator and real device), when I Archive and test through TestFlight the Flutter App begins with a grey background (which I read that it means some kind of error).
If I go ahead and remove all calls of the Rust lib then the Testflight opens normally.
Note: I have also added the .a
library as a Linked framework from the XCode and I have included the .h
file to the bridging-header.h of the project.
I have also
- run
flutter build --release
- cleared the derived data
- pods cache
- even re-made the iOS folder from scratch.
Is there something else that I'm missing here?
...ANSWER
Answered 2020-Sep-27 at 19:50I managed to make it work by creating a Flutter plugin flutter create -t plugin
then I imported the .a
file on the /iOS
folder and included all the rust functions on the .h
file inside /iOS/Classes
(these are automatically created).
Then add a sample function for each of the rust functions inside the .Swift
file in /iOS/Classes
And make sure to include
QUESTION
I found this question, but it is 3 years old and since then, crates like cxx
have come into existence. Is it possible now to construct a C++ object from Rust, or do I still have to create a shim?
ANSWER
Answered 2020-Sep-04 at 00:09To the extent that constructors "return" a C++ type by value, they're not translatable to Rust because Rust moves (memcpy) are incompatible with C++ moves (which can require a move constructor to be called). Translating an arbitrary constructor to fn new() -> Self
would not be correct.
You can bind them unsafely with bindgen which assumes moving without a constructor call is okay, or you can use the "shared struct" approach in the readme which is safely movable in either language, or you can include!
a shim which does the construction behind a unique_ptr or similar.
That last approach would look something like:
QUESTION
As part of mapping a C interface to Rust, I want to handle a union that stored a few native types directly and have a pointer to an allocated type for all others.
How can I implement a parameterized wrapper type around the union that can select and use the appropriate field based on the wrapper type parameter?
In this case, I want to add a Rust wrapper that reads the data structure directly and not a solution that converts it to a Rust-native type first. Adding other support types to "trick" the compiler is fine though. The end goal is to be able to write code similar to this:
...ANSWER
Answered 2020-Jun-24 at 19:20I would define my own trait instead of From
to avoid conflicting with the standard library implementations. I'd also define a newtype wrapper / marker type. This removes the possibility of conflict when storing one of the specific types in the generic spot.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rust-ffi
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