Rust Code in the Wild
Timon Gehr
timon.gehr at gmx.ch
Thu Sep 5 06:50:58 PDT 2013
On 09/05/2013 05:22 AM, Meta wrote:
> While browsing Hacker News I came across this announcement of a physics
> engine written in Rust. Just browsing through the code, I noticed that
> it looks extremely arcane. I guess after awhile it's something you could
> get used to, but it seems somewhat ironic that the author says that C++
> will die of ugliness, when Rust already has syntactic monstrosities such
> as these:
>
> pub struct BodyWorld<N, LV, AV, M, II, CM> {
> world: World<N, Body<N, LV, AV, M, II>, Constraint<N, LV, AV, M, II>>,
> forces: @mut BodyForceGenerator<N, LV, AV, M, II>,
> integrator: @mut BodySmpEulerIntegrator<N, LV, AV, M, II>,
> detector: @mut BodiesBodies<N, LV, AV, M, II, BF<N, LV, AV, M, II>>,
> sleep: @mut IslandActivationManager<N, LV, AV, M, II>,
> ccd: @mut SweptBallMotionClamping<N, LV, AV, M, II, BF<N, LV, AV,
> M, II>>,
> joints: @mut JointManager<N, LV, AV, M, II>,
> solver: @mut AccumulatedImpulseSolver<N, LV, AV, M, II, CM>
> }
>
> This stuff is downright arcane.
Why? I cannot spot any syntax I wouldn't get used to quickly in this
example. (AFAIK the @ are going away in favour of a library solution
with more verbose syntax though.)
In D something similar would look like this:
struct BodyWorld(N, LV, AV, M, II, CM){
World!(N, Body!(N, LV, AV, M, II), Constraint!(N, LV, AV, M, II))
world;
BodyForceGenerator!(N, LV, AV, M, II)* forces;
BodySmpEulerIntegrator!(N, LV, AV, M, II)* integrator;
BodiesBodies!(N, LV, AV, M, II, BF!(N, LV, AV, M, II))* detector;
IslandActivationManager!(N, LV, AV, M, II)* sleep;
SweptBallMotionClamping!(N, LV, AV, M, II, BF!(N, LV, AV, M, II))* ccd;
JointManager!(N, LV, AV, M, II)* joints;
AccumulatedImpulseSolver!(N, LV, AV, M, II, CM)* solver;
}
Afaics there is not much of a difference. Arguably, having the
identifiers conveniently aligned to the left is actually a slight
advantage of rust's syntax.
Of course, the code contains a certain amount of duplication I wouldn't
really want to write down, and this could be improved in D:
struct BodyWorld(N, LV, AV, M, II, CM){
private alias Args = Seq!(N, LV, AV, M, II);
World!(N, Body!Args, Constraint!Args) world;
BodyForceGenerator!Args* forces;
BodySmpEulerIntegrator!Args* integrator;
BodiesBodies!(Args, BF!Args)* detector;
IslandActivationManager!Args* sleep;
SweptBallMotionClamping!(Args, BF!Args)* ccd;
JointManager!Args* joints;
AccumulatedImpulseSolver!(Args, CM)* solver;
}
More information about the Digitalmars-d
mailing list