rust-bindgen | Automatically generates Rust FFI bindings to C | Wrapper library
kandi X-RAY | rust-bindgen Summary
kandi X-RAY | rust-bindgen Summary
For example, given the C header doggo.h:.
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-bindgen
rust-bindgen Key Features
rust-bindgen Examples and Code Snippets
Community Discussions
Trending Discussions on rust-bindgen
QUESTION
I am trying to read the contents from the ISO image using Rust. I have installed libiso9660-dev
and libcdio-dev
on my Linux machine to get access to the header files. Libiso9660 is a library to work with ISO9660
filesystems (development files). I used bindgen to generate bindings to the library. Until this point, everything was pretty straightforward.
Here is the part of the bindings relevant to my code:
...ANSWER
Answered 2021-Dec-17 at 09:28Notice the type of the p_psz_app_id
parameter: *mut *mut cdio_utf8_t
. It is a double pointer. Here is what the C example code is doing:
QUESTION
I have a wrapper around a C-API:
...ANSWER
Answered 2021-Jun-07 at 22:24Short answer: just cast it to *mut T
and pass it to C.
Long answer:
It's best to first understand why casting *const T
to *mut T
is prone to undefined behaviour.
Rust's memory model ensures that a &mut T
will not alias with anything else, so the compiler is free to, say, clobber T entirely and then restore its content, and the programmer could not observe that behaviour. If a &mut T
and &T
co-exists and point to the same location, undefined behaviour arises because what will happen if you read from &T
while compiler clobbers &mut T
? Similarly, if you have &T
, the compiler assumes no one will modify it (excluding interior mutability through UnsafeCell
), and undefined behaviour arise if the memory it points to is modified.
With the background, it's easy to see why *const T
to *mut T
is dangerous -- you cannot dereference the resulting pointer. If you ever dereference the *mut T
, you've obtained a &mut T
, and it'll be UB. However, the casting operation itself is safe, and you can safely cast the *mut T
back to *const T
and dereference it.
This is Rust semantics; on the C-side, the guarantee about T*
is very weak. If you hold a T*
, the compiler cannot assume there are no sharers. In fact, the compiler cannot even assert that it points to valid address (it could be null or past-the-end pointer). C compiler cannot generate store instructions to the memory location unless the code write to the pointer explicitly.
The weaker meaning of T*
in C-side means that it won't violate Rust's assumption about semantics of &T
. You can safely cast &T
to *mut T
and pass it to C, provided that C-side never modifies the memory pointed by the pointer.
Note that you can instruct the C compiler that the pointer won't alias with anything else with T * restrict
, but as the C code you mentioned is not strict with const
-correctness, it probably does not use restrict
as well.
QUESTION
I'm using rust-bindgen to access a C library from Rust. Some functions return nullable pointers to structs, which bindgen represents as
...ANSWER
Answered 2021-Mar-05 at 17:28QUESTION
I have a library (both as source and compiled) and I'm writing a program (not linked to this library) that needs to know if some type in the library is e.g. trivially copyable or not.
I could make my program write the following into file.cpp:
...ANSWER
Answered 2020-Dec-03 at 21:56You are going to need to compile the code, at least to IR. This is because the triviality of a C++ class can depend on an arbitrarily complicated computation whose inputs may include platform attributes, available headers, preprocessor defines, compiler options etc., which can thus only be carried out by a C++ compiler.
If you are invoking clang as a binary the option to emit IR is clang -S -emit-llvm
and you will then want to parse the LLVM IR output; for example for
QUESTION
According to the Rust bindgen tutorial at https://rust-lang.github.io/rust-bindgen/tutorial-3.html, I am trying to generate the FFI for existing C header files. However, I am having trouble linking the header files.
The repository at https://github.com/studersi/rust-test-apache holds all the files necessary to reproduce the issue including a README file containing the necessary commands. The most relevant files are listed at the end of the question.
I am using a Docker container to build the project (see Dockerfile below). The C header files are downloaded from the Internet and placed in the subdirectory downloads/
.
The includes for the linker (see build.rs below) are based on the includes the apxs
tool uses to build a similar C example according to the Apache httpd module development guide on which my project is based.
ANSWER
Answered 2020-Oct-16 at 17:32Found the answer shortly after posting the question here.
The problem lies with the .clang_arg()
call. There must be only one include per call to that function but multiple calls to the function are allowed.
The working build.rs
would then include the following calls:
QUESTION
I have the following simple OpenCL kernel, that simply copies all entries pointed at a to b
...ANSWER
Answered 2020-Apr-20 at 07:40Before I dig in, I'll point out that I'm a relative beginner with Rust and I'm not particularly familiar with what bindgen produces, but I know OpenCL quite well. So please bear with me if my Rust syntax is off.
The most obvious thing that sticks out for me is that passing the buffer to clSetKernelArg
using buffer as *const c_void
looks suspicious. My understanding is that your code is roughly equivalent to this C:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install rust-bindgen
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