Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

My favorite translation so far is Duff's Device. In old C from Wikipedia modernized it's roughly:

    void send(volatile short *to, short *from, int count)
    {
        int n = (count + 7) / 8;
        switch (count % 8) {
        case 0: do { *to = *from++;
        case 7:      *to = *from++;
        case 6:      *to = *from++;
        case 5:      *to = *from++;
        case 4:      *to = *from++;
        case 3:      *to = *from++;
        case 2:      *to = *from++;
        case 1:      *to = *from++;
                } while (--n > 0);
        }
    }
While the result from c2rust is even less maintainable than original interleaved-loop-switch, I'm impressed that it looks like they have a robust way to handle bizarre control flow. (The output from c2rust is not reproduced here to save space, you can try it on https://c2rust.com/ )


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: