Compiler should compile this away, what's the context of this? This feels like way too specific of a piece of code not to warrant specific circumstances
I'm asking what you think is wrong with Rust as a language, not what you think is wrong about code posted to r/badcode. Like, sure, this particular operation is convoluted. You're doing casts and array copies. The C version doesn't look much better
unsigned long long int = module.index;
StivaleModule *module = module.module;
char stivale2_mod_name[32];
memcpy(stivale2_mod_name, module->string, 32);
Like, I can't imagine what problem there is with something like fibonacci, unless you're just put off by the mut keyword
fn fib(n: usize) -> usize {
let mut a = 0;
let mut b = 1;
for _i in 0..n {
let c = a + b;
a = b;
b = c;
}
return a;
}
I actually like Rust. But the syntax reminds me of Assembly Language which I haven't coded in over 30 years (and don't want to code in it again). Coding with a 4K RAM limit wasn't a lot of fun.
Not really about syntax (although, I don't like Rust syntax either), but the worst thing I find about Rust programs is that programmers are told to use nested anonymous functions.
It doesn't matter how many times in different environments people discover the same problem with this approach, they call it different names (eg. "callback hell", "Angular"), but keep doing it regardless.
And, of course, you don't have to do it in Rust, but, for some reason, this is the popular and well-accepted style.
And, when it comes to syntax, Rust's macros are... just lame. It's like they knew they aren't going to work well even before they started, but did it poorly anyways. The major problem is not distinguishing between syntax macros and other kinds of macros. it would've been a lot better if regular macros used the same syntax rules as the rest of the language, and this is something that has been known for at least 40 years if not more...
If you're talking about the problem of colored functions, I don't see why you think the problem is specific to Rust. This is also the standard in C#. Unlike in JavaScript, Rust has ways to block on an async function from a sync function. And the entire Rust standard library is synchronous. I might be misunderstanding though. Maybe you could give an example which is more clear.
I also don't understand the problem with macros. Maybe that's because I don't know what you mean by "regular vs syntax" macros. Calling a macro certainly doesn't seem to be that difficult (unless you're specifically urked by an exclamation point).
Maybe that's because I don't know what you mean by "regular vs syntax" macros.
This is not about difficultly. The taxonomy comes from Lisp languages. Syntax macros define new syntax elements, in context of Common Lisp, for example, they define new rules for the syntax table, and allow parsing code that otherwise wouldn't have been valid CL. Regular macros don't touch the syntax table, if you use them, you produce something that's still syntactically valid CL, but it will result in a meaningless program (i.e. a macro will be mistaken for a function).
There are more kinds of macros, like symbol macros, it's when something that in Rust would've been an identifier, for example, could be interpreted differently etc.
So, I think, it's bad to mix the regular macros and the syntax macros.
If something is a standard in C#, it's a good indication you shouldn't do it...
And no, the problem isn't performance. The problem is that when shit hits the fan, you have no idea where it came from. You just get some nondescript stack traces, from which you cannot recreate the chain of events that led to the failure.
I don't think I mentioned performance. It seems like the issue you're talking about is more a problem of the chaos that occurs with concurrency than with the language. What alternative are you thinking of that would solve the issue?
It's kind of really simple: just name the functions you are using. You can absolutely write like that in Rust, just don't declare and use at the same time, and you will have better ways of tracing back the cause of a failure.
Also, Rust doesn't have a good middle ground for concurrency-related code. It's either you use system threads, and it's both inefficient and dangerous, or you use Tokyo, which has too many things going on. It also kind of encourages using anonymous functions.
I didn't even realize you were talking about anonymous async functions. I don't think that's the norm. I've never used them. The Rust async book uses named functions. I'm sure you can find examples of people using async closures, but that doesn't necessarily mean it's the norm. I've never seen it actively recommended. Although, for what it's worth, I can't imagine it causes that much of an issue. It tells you the line number of the error if the thread panics.
If Tokio's too large, then you could use the futures crate on its own. I'm pretty sure the executor uses a thread pool under the hood.
Line number inside the anonymous function is pretty much worthless. What happens is that you have the stack trace of this format: general code leading to submitting callback to Tokyo, a bunch of Tokyo internal stuff, then the code of the callback. And you have not a single clue of what caused that callback to be fired. You need to reconstruct possible events chains that might have led to this callback to be fired in your head, and then try to examine all possibilities. Sometimes the number possibilities is manageable, but often times it gets into hundreds if not thousands of possible scenarios, and then you just sigh and say "well, maybe it will fail again, but in a better way", and maybe add a few logs here and there, hoping to understand what happened.
Using the futures on its own isn't a solution. Not to the problem I'm describing anyways. Look at Go or Erlang: they have simple ways to implement >90% of anything you need from parallelism, and they have like 5 primitives for that. Rust doesn't have a good built-in parallelism mechanism. You could, in principle, reconstruct something like Go or Erlang using Tokyo, but it's both not properly integrated, and it's just built in a way that instead of optimizing a handful of primitives, you get a shitload of primitives which you need to use based on specifics of your case, making it a lot more difficult and error prone to do.
That transmute is wild. In theory converting a u8 to c_char is just a simple primitive cast. I wonder how well rustc would optimize a loop that does this.
ah idk if possible to collect into a fixed size array, but if zip the two iterators together then it should just be a simple copy between the two variables (possibly vectorised, too)
Not real bad code, but transmute is incredibly unsafe, and due to how C and Rust types work I think there might be safe or at least safer way to do it.
When you haven't done some Rust before the syntax can looks a bit weird but once you are familiar with it I swear you think why it is the only language to have adopted it
Dude, are you rust hater and haskell praiser or something? I see you under every rust post on this sub and you're every time comparing rust and haskell for no particular reason
Do I praise Haskell? Yes, Haskell is a good language and it serves as a testing ground for features that get adopted by mainstream languages and other non-mainstrean languages like Rust.
17
u/Atsch Feb 28 '22
surely this is being done for some good reason?
33
u/O_X_E_Y Feb 28 '22
Compiler should compile this away, what's the context of this? This feels like way too specific of a piece of code not to warrant specific circumstances
11
u/seeroflights Feb 28 '22
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
2
u/Kikiyoshima Mar 01 '22
From which hobby kernel have you got this from?
2
u/minus_uu_ee Mar 01 '22
I guess I will take your word for it.
edit: Btw, colorscheme?
2
u/ahos-adanos Mar 01 '22
This isn't bad, honestly. I'd only change first 2 lines to:
2
u/minercreep Mar 01 '22
What theme you are using
41
u/SleeplessSloth79 Mar 01 '22 edited Mar 01 '22
That doesn't look that bad though. All it does is clone a slice and transmute a u8 array to a C char one
18
u/[deleted] Mar 01 '22
that code really isn't all that bad. This is why unsafe exists. Thought comments are desperately needed here.
6
u/FloweyTheFlower420 sadistic Mar 01 '22
Kernel dev in rust moment. Anyway still mad respect for using limine and stivale2
10
u/HinaCh4n Feb 28 '22
That transmute is wild. In theory converting a u8 to c_char is just a simple primitive cast. I wonder how well rustc would optimize a loop that does this.
2
u/linlin110 Mar 01 '22
Why is this on r/badcode? Because of
transmute?1
u/weflown Mar 01 '22
Not real bad code, but transmute is incredibly unsafe, and due to how C and Rust types work I think there might be safe or at least safer way to do it.
1
u/bruhred Apr 26 '22
you could have used copy_from_slice which is usually faster(?)
-11
u/isunktheship Mar 01 '22
I don't know Rust, so I'll assume the joke here is that it's simply written in Rust
-7
u/[deleted] Mar 01 '22
You know, I'd like to learn rust because it has so many good features. But why is its syntax so God damn retarded ?
0
u/[deleted] Feb 28 '22
[deleted] — view deleted comment