Earlier quoted context omitted.
I guess the next stage would involve translating common non-idiomatic patterns into idiomatic Rust. Looks like this could be a job for a community-managed database!
On the rust subreddit someone tongue-in-cheek suggested `cargo clippy | rustfix` to be used in conjunction with this tool for better rust code. But that actually could work! Clippy has a ton of lints that make your code more idiomatic, and rustfix basically takes diagnostic output and applies suggestions (still WIP). Clippy is geared towards making human-written unidiomatic code better, so it might not catch some sil…
Corrode: C to Rust translator written in Haskell
41–50 of 127 posts
Re: Corrode: C to Rust translator written in Haskell
#42It's too bad this is written in Haskell. I don't have anything against Haskell, it is just not as popular a language as others.[1] Any ANTLR target language would have been a solid choice.[2] This way more of the community could contribute. This is an invaluable tool if we're truly going to see a shift from C (or C++) to Rust. [1] http://pypl.github.io/PYPL.html [2] http://www.antlr.org/download.html
Re: Corrode: C to Rust translator written in Haskell
#43Earlier quoted context omitted.
Here you go. It didn't like my stdio.h. Apparently enums and unions aren't supported, but: extern int printf(char *, ...); int main(int argc, char argv[]) { printf("Hello, world!\n"); return 0; } Was turned into: extern { fn printf(arg1 : *mut u8, ...) -> i32; } #[no_mangle] pub unsafe fn main(mut argc : i32, mut argv : *mut u8) -> i32 { printf(b"Hello, world!\n\0".as_ptr() as (*mut u8)); 0i32 } edit: Also worth noti…
Shouldn't a C `int` be converted to Rust's `isize`. I think that captures the spirit better.
Re: Corrode: C to Rust translator written in Haskell
#44I was curious about how this worked so I looked into the source a little (even though Haskell isn't exactly my cup of tea), and WOW... This is just amazing. The most important part of the source is highly educative literate haskell: https://github.com/jameysharp/corrode/blob/master/src/Langua...
That's an incredibly brilliant idea, there's some serious craftsmanship going on in that source file. Kinda taking Rust's doc unit tests format to a whole new level.
Re: Corrode: C to Rust translator written in Haskell
#45Earlier quoted context omitted.
On the rust subreddit someone tongue-in-cheek suggested `cargo clippy | rustfix` to be used in conjunction with this tool for better rust code. But that actually could work! Clippy has a ton of lints that make your code more idiomatic, and rustfix basically takes diagnostic output and applies suggestions (still WIP). Clippy is geared towards making human-written unidiomatic code better, so it might not catch some sil…
I haven't used nightly much, what all does Clippy do?
Its a developer tool so you can use rustup to switch to nightly to run clippy (and use stable otherwise) and not impose nightly on the rest of the people who use the project. We have plans for making clippy a tool that you can fetch via rustup without requiring nightly.
Re: Corrode: C to Rust translator written in Haskell
#46Earlier quoted context omitted.
That's an incredibly brilliant idea, there's some serious craftsmanship going on in that source file. Kinda taking Rust's doc unit tests format to a whole new level.
Rust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.
Re: Corrode: C to Rust translator written in Haskell
#47This was my immediate concern. Is there any chance this tool can produce anything close to clean, safe, idiomatic, rust code?
Re: Corrode: C to Rust translator written in Haskell
#48Earlier quoted context omitted.
Rust should have a literate source format - with how much the community focuses on documentation, this would be an excellent addition.
https://github.com/rust-lang/rust/issues/26097
-- foo is a function
foo :: String -> String
It's foo is a function
> foo :: String -> StringRe: Corrode: C to Rust translator written in Haskell
#49Earlier quoted context omitted.
https://github.com/rust-lang/rust/issues/26097
With the literate style, code is prefixed and comments are raw. So, instead of: -- foo is a function foo :: String -> String It's foo is a function > foo :: String -> String
Re: Corrode: C to Rust translator written in Haskell
#50Earlier quoted context omitted.
Here you go. It didn't like my stdio.h. Apparently enums and unions aren't supported, but: extern int printf(char *, ...); int main(int argc, char argv[]) { printf("Hello, world!\n"); return 0; } Was turned into: extern { fn printf(arg1 : *mut u8, ...) -> i32; } #[no_mangle] pub unsafe fn main(mut argc : i32, mut argv : *mut u8) -> i32 { printf(b"Hello, world!\n\0".as_ptr() as (*mut u8)); 0i32 } edit: Also worth noti…
Shouldn't a C `int` be converted to Rust's `isize`. I think that captures the spirit better.