Migrating from C to Rust - Part 1: Calling Rust Code from C

preview_player
Показать описание
Rust v1.0 was released approximately a decade ago, and support for using Rust as a replacement for C and C++ has been steadily growing. However, this transition requires migrating from C to Rust, which requires the ability to invoke Rust code from within the existing C codebase.
---

#garyexplains
Рекомендации по теме
Комментарии
Автор

Directly to the point. Thanks!
EDIT: You can always write the bulk of the functionality in safe Rust, and then wrap it in a thin unsafe Rust adaptor, that does very little and just allows the (possibly well tested) C code to remain the same.

ernestuz
Автор

Very good succinct video. If your C code is stable, reliable and readable then it ain't broke - so don't fix it. But for anyone with leaking spaghetti C code then this video definitely gives an excellent clear introduction on a possible way to move forward. Remember that for businesses maintenance is currently the primary long term issue with Rust - more so than C.

stephenevans
Автор

Thank you very much! I got to learn a lot from this introductory C to Rust migration tutorial. Looking forward to the complete series.

Djzaamir
Автор

A video on making portable C code would be neat.

nomadic_shadow
Автор

11:54, I don't know if there's caveats to it (in an ffi context), but pretty sure you can still use globals by wrapping in a type that allows for interior mutability (RefCell for single threaded contexts), that way you don't have to use an OOP style API if you don't want to, and don't need unsafe either. I'd just define
```
thread_local!{
static X: RefCell<u64> = Default::default();
}
```
I do this all the time, since I typically don't like singletons, I usually use structs when there's more than one, or when there's a lot of related data to bundle (and that data is needed as context for a few different functions). Then `X.with_borrow(|x| dbg!(x))` or `X.with_borrow_mut(|x| x+=1)`...

For the other direction I've briefly messed around with libloading (e.g. allowing for a c plugin in a rust program).

johanngambolputty
Автор

I like Rust but I think it’s a Java language! What I mean is that Javas JIT compiler was a real breakthrough at the time, and made Java extremely popular. However, the design of the language syntax itself has a bunch of flaws, and it is being slowly replaced by better languages with the same JIT technology.
The same idea with Rust, the core technology of the borrow checker is fantastic, but the language has some issues which make it a real pain. I’m sure there’s gonna be a better version of borrow checker technology that will come along and replace it. For me, Rust is just not worth the pain as it is.

alst
Автор

Some suggestions
1) program behaviour when unsafe condition occurs
2) c vs rust program size
3) c vs rust speed
4) c vs rust tools
5) c vs rust libraries
6) c vs rust i/f to other languages
7) c vs rust lowlevel support eg embedded systems

theokramer_za
Автор

Even though I make like the usage of a language, for it me, its about the syntax. I do not like the syntax of rust, and dont get me started with python... but know, I really want to use python more for small utils, but the whole indenting throws me. I have written a little rust code, but I am just not a fan of the syntax. even in c, i use int8_t and int16_t, to me it tells me a lot when reading.

portblock
Автор

Yesterday, I called C code from some my Rust crate. It worked great.

kamertonaudiophileplayer
Автор

what about binary compatibility? you can't build lib and executable with different compilers, it's a bad practice

bubobubo
Автор

It is possible to have a mutable global variable without the use of unsafe code:

```rust
use std::sync::RwLock;

static X: RwLock<u64> = RwLock::new(123456789u64);

#[no_mangle]
pub extern "C" fn vrandom() -> u64 {
let mut x = X.write().unwrap();
*x =
*x
}

#[no_mangle]
pub extern "C" fn vseed(seed: u64) {
let mut x = X.write().unwrap();
*x = seed;
}
```

No need for structs nor any changes to the original C main() function.

adrianbool
Автор

Six months pasted... Still waiting for the next part in the series...
The obvious next item is calling C from Rust, and then - wrapping "unsafe" C functions so that the wrappers can be called from safe Rust

alexanderandreyev
Автор

I’m really enjoying Rust. I really like how it simultaneously feels like a high level and low level language. Iterators, slices and closures are awesome. Destructuring and match expressions are very powerful. Macros are like crawling inside the complier and casting magical spells. Getting started in Rust was easy because that part is simple. Getting proficient took some time and frustration. I’m still not an expert, but have been building useful projects. The more I work with it the more powerful I become.

This was a great example. I’m looking forward to seeing more Rust content.

Perspectologist
Автор

Looks like Rust has gone down that all to common route with languages where it's later realised that _mistakes were made_ <cough>C++<cough>.

No inital planning for how people would link to C, a syntax which is quite different to what it replaces but for no obvious benefit and perhaps worst of all, it's overly verbose without adding much in the way of readability benefits.

Good video though.

loc
Автор

java and c99 together, c99 in opencl, instead of opengl

gsestream
Автор

Part 2: Calling rust routines from python. Manipulating (python) strings. Interacting with numpy

LA-MJ
join shbcf.ru