Piston – A modular game engine written in Rust
piston.rs
piston.rs
Since Rust is so new, perhaps there is no real difference, but the piston example shows some ideas that I'm not sure I like but I also don't know if that is just how things are done in the Rust world.
let window: PistonWindow =
WindowSettings::new("Hello Piston!", [640, 480])
.exit_on_esc(true).build().unwrap();
for e in window {
e.draw_2d(|c, g| {
clear([1.0; 4], g);
rectangle([1.0, 0.0, 0.0, 1.0], // red
[0.0, 0.0, 100.0, 100.0],
c.transform, g);
});
}
That's iterating on a window which seems an odd choice.
The functions clear and rectangle seem to me to be things that should be in traits rather than functions that require a parameter to say what the target of the drawing function is.The `e` that is yielded by iterating the window is the window itself WHAT
Especially because the examples use stuff that is basically impossible to find in the docs (even though the docs are auto-generated from the source!). The modularity makes it really hard to tell how anything works, what anything is, or what functionality anything has.
let s = Some(10u32);
for val in s {
println!("{}", val);
}
So it's not a completely foreign concept... but maybe they went too far with it?Though it wouldn't be super terrible if it was idiomatic. I personally prefer to do `s.map(|val| { ... }), but there's a contingent that argues that `map` shouldn't be used purely for side-effects, and should instead be used for, you know, mapping. We've oft argued that if you want to consume an iterator, it aught to be with a `for` loop, so it might make sense to say the same about an Option? Option is such a trivial and core type that you basically end up with 4 ways to do everything you could think of, because semantically distinct conventions all end up doing the exact same thing:
for x in data {
println!("{}", x)
}
if let Some(x) = data {
println!("{}", x)
}
data.map(|x| {
println!("{}", x)
});
match data {
Some(x) => println!("{}", x),
_ => ()
}
The fact that you can loop over an option is kind of a side-effect of a lot of our APIs taking "thing that is iterable", and evidently it was convenient to provide that for Option (which is after all just a really degenerate collection).You can of course `match` and `if let` an Option because it's an enum, but because there's two cases and one has no state, they end up being completely equivalent (the `_ =>` branch is just an `else`).
Then finally you can `map` over an Option because... It's Option. That's what you do with optional types, dangit!
https://github.com/tinco/super-susano/blob/master/src/main.r...
That calls into the draw loop here:
https://github.com/tinco/super-susano/blob/master/src/engine...
let window: PistonWindow =
WindowSettings::new("Hello Piston!", [640, 480])
.exit_on_esc(true).build().unwrap();
That is one of the ugliest, most "accidental complexity" ways of creating a window I have ever seen.I am certainly not going to use that.