4 ms·
The OP is likely nothing more than wishful thinking, but I can see why people are intrigued by the prospect. An important thing to note is that it's entirely po
by kibwen 11y ago
The OP is likely nothing more than wishful thinking, but I can see why people are intrigued by the prospect. An important thing to note is that it's entirely possible to perform a slow, gradual integration of Rust code into a C/C++ codebase (such as Mozilla is doing with Firefox), but only if your codebase is sufficiently modular with well-defined interfaces for Rust-based libs to slot into. If all you've got is a big ball of mud, then your first step is to work on the modularization of your codebase (which, IMO, has a good chance of increasing your code quality regardless of whether you eventually decide to introduce any Rust).
- MichaelGG 11y agoSince Rust and C can agree on ABI and memory layout issues, you can move the code over function-by-function if you were so inclined, right? Declare the structs in the headers and auto-gen the Rust versions (is there a tool for the inverse?).
- comex 11y agoYou can, but idiomatic Rust code looks pretty different from idiomatic C code - to give a concrete example, int get_a_string(char **out) // returns error code could be translated literally as fn get_a_string(out: *mut *mut c_char) -> c_int but much preferable would be fn get_a_string() -> Result<String, Errcode> [where Result and String are standard library types; Errcode would be a custom enum.] I suppose you could go from the first to the second incrementally, then refactor to the third. But I'm not sure it'd work well.
- randommodnar 11y agoIs there a solution yet in Rust to this problem of, monadic-ish constructs like Result and Option, but no nice way to chain them together without hideous nesting, like Haskell's "do" or Scala's "for"?
- steveklabnik 11y agoWe don't have HKT and therefore we can't have real do notation. The mdo crate provides a duck-typed macro. .and_then calls themselves aren't too bad, with the right whitespace.
- comex 11y agoIn the special case of Result, the new ? operator is going to subsume try! and offer more flexibility: https://github.com/rust-lang/rfcs/pull/243 https://github.com/rust-lang/rfcs/pull/243 (note that what was actually accepted differs from the text of the RFC, you need to scroll to the bottom) As steveklabnik says, there's nothing resembling generalized monadic do notation for now.
- kibwen 11y agoThis presumes that functions are well-defined boundaries in your C codebase, which may not be true if one is leaning heavily on global state.
- alexnewman 11y agoYou end up having to toss a layer of c around your c++ last time I checked. Does c++ even have an abi yet?
- roblabla 11y agowell, not a standard one. That's what extern "C" is for : if you want a standardized API, C always had one.
- deleted 11y ago[deleted]
- gpderetta 11y agoNeither C nor C++ have a standardized ABI, if by standardized you mean part of their respective ISO standard. Nor they could, as the many of ABI properties (names, calling conventions, layout, object and executable file format) are intimately platform (as in OS and CPU) specific. Most platform officially specify their C ABI, and many also specify their C++ ABI (usually in therm of the C one). Itanium C++ ABI (plus the platform specific psABIs) has become the de facto C++ ABI for many unix and unix-like platforms.
- Manishearth 11y agoYou can call into Rust directly from C++ (with a header file). But yeah, the reverse requires a C layer. rust-bindgen has experimental C++ support which we use for spidermonkey but it isn't perfect.
- sitkack 11y ago> You can call into Rust directly from C++ (with a header file). Can you tell me more about this?
- steveklabnik 11y agoThis is C and I tossed it together VERY quickly but $ cat hello.rs #[no_mangle] pub extern fn plus_one(x: isize) -> isize { x + 1 } $ cat hello.c #include<stdio.h> extern int plus_one(int); int main() { int result = plus_one(5); printf("result is %d\n", result); return 0; } $ rustc hello.rs --crate-type=dylib $ gcc hello.c -lhello -L . $ LD_LIBRARY_PATH=. ./a.out result is 6
- voiper1 11y ago>If all you've got is a big ball of mud, then your first step is to work on the modularization of your codebase (which, IMO, has a good chance of increasing your code quality regardless of whether you eventually decide to introduce any Rust). I wanted to up-vote that bit.