News for hackers. Stuff that matters.
Re:Things Rust shipped without by merlincorey on Friday July 03, 2015 @04:36PM (#9827809)
How does one accomplish loop-unrolling as in Duff's Device in rust, if case statements do not fall through?
Re:Things Rust shipped without by steveklabnik on Friday July 03, 2015 @04:48PM (#9827874)
Well, you generally don't, because Duff's Device isn't generally regarded as a good idea anymore, in my understanding:

  > It turns out that with branch predictions and the relative speed of CPU
  > vs. memory changing over the past decade, loop unrolling is pretty much
  > pointless.
http://lkml.iu.edu/hypermail/linux/kernel/0008.2/0171.html
Re:Things Rust shipped without by veddan on Friday July 03, 2015 @05:29PM (#9828055)
Still, there are cases where fall-through can be useful:

    int remaining = length % 4;
    switch (remaining) {
        case 3: h ^= (data[(length & ~3) + 2] & 0xff) << 16;
        case 2: h ^= (data[(length & ~3) + 1] & 0xff) << 8;
        case 1: h ^= (data[length & ~3] & 0xff);
                h *= m;
    }
Re:Things Rust shipped without by steveklabnik on Friday July 03, 2015 @05:37PM (#9828097)
https://github.com/pythonesque/fallthrough can emulate the fallthrough style if you really, really want to have it. It seems like it hasn't been updated in a while, though.
Re:Things Rust shipped without by Jweb_Guru on Friday July 03, 2015 @10:12PM (#9828863)
In the post-1.0 world, a repository not having been updated for a few months doesn't automatically mean it no longer works :P
Re:Things Rust shipped without by steveklabnik on Friday July 03, 2015 @11:40PM (#9829070)
That's true but January 11 is in that weird grey area...
Re:Things Rust shipped without by Jweb_Guru on Saturday July 04, 2015 @12:01AM (#9829134)
Let me be clearer: that repository works fine and doesn't rely on the standard library at all.
Re:Things Rust shipped without by Sanddancer on Saturday July 04, 2015 @03:32AM (#9829553)
It's interesting, but a loop makes for smaller code, by a couple dozen bytes or so:

    int remaining = len % 4;

    if (remaining)
    {
        do
        {
            remaining--;
            h ^= (data[(length & ~3) + remaining] & 0xff) <<  (remaining * 8);
      } 
      while(remaining);

      h *= m;
    }
Fall-through's interesting, but at the same time, as architectures have changed, has become less useful. Self-modifying code at one time was near vital, but has fallen by the wayside, fall-through is doing much the same.
Re:Things Rust shipped without by pcwalton on Friday July 03, 2015 @07:21PM (#9828433)
Usually I would just factor out the guts of the expression into a little closure in that case. (let f = |x| h ^= ... x ...) LLVM should inline it just fine, and it'll save you typing.
Re:Things Rust shipped without by yazaddaruvala on Friday July 03, 2015 @10:38PM (#9828907)
If loops can have specialized control-flow keywords (break; continue), why not let match have one (fallthrough)?

    match x {
        0 => { /* do some stuff */; fall_through; },
        1 => true,
        _ => false
    }
However, I don't know yet how useful it would be. I can't remember ever really needing it, so it would probably need a few practical examples before it became a reality but its an idea.
Re:Things Rust shipped without by pcwalton on Friday July 03, 2015 @10:44PM (#9828922)
If we really need it I'd rather have C#-style goto-label instead of explicit fall-through, which is strictly less general. (But I'd almost rather it be a tail-duplicating macro to begin with, since the feature is so rarely needed.)
Re:Things Rust shipped without by detrino on Friday July 03, 2015 @09:09PM (#9828729)
I don't see how this helps here, unless you intend to call the closure several times per case.
Re:Things Rust shipped without by detrino on Friday July 03, 2015 @09:21PM (#9828759)
I wonder if this is really faster than just writing it as a loop.
Re:Things Rust shipped without by arcticbull on Friday July 03, 2015 @04:59PM (#9827921)
Generally doing anything crafty like this in real-world or production code is just going to cause the next guy who shows up to be very, very confused. Loop unrolling optimizations and the like should really be left to the compiler.

If you're truly sure you're better than the compiler, I'd imagine an assembly language implementation would be easier to understand than nested switch/while fall through madness.

That said. There's an implementation of Duff's device in the Wikipedia entry which (IMO is much easier to understand and maintain) that doesn't use fall-through that should be just fine to write in Rust.

https://en.wikipedia.org/wiki/Duff's_device

Re:Things Rust shipped without by jcranmer on Friday July 03, 2015 @04:49PM (#9827876)
Duff's Device is now considered an anti-pattern... X found their code speed actually improved when they dropped it. <http://lkml.iu.edu/hypermail/linux/kernel/0008.2/0171.html>.