News for hackers. Stuff that matters.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by petschge on Thursday November 21, 2019 @04:59PM (#21600166)
I am sure there is good theoretical arguments. But they are hard on the humans. Ideally I would want something like

  constdata keymaps[1,2,3, u16]
That is easy to read, gets rid of all the extra line noise and directly tells me everything I need to know about memory layout and performance.

  1.) it is constant, known and compile time and can be put into a read-only segment (or possibly flash rom on an embedded system).

  2.) it is named keymaps. The name is important and should come early

  3.) it is an array. arrays and primitive datatype have many important differences and programming languages should not try to hide that.

  4.) it has dimensions 1 by 2 by 3 (in that order). Listing the "3" first in Rust when  the first dimension only has extend 1 might have good reasons but is damn hard to read if you have more than 2 dimensions. Especially if you end up with things like 3 by 3 by 4 by 3. Which of the inner two is larger?

  5.) Having the type of the element last makes sense, because in terms of memory layout that just means that we have 2 consecutive bytes. I also makes it easier to which from "a 1 by 2 by 3 array of u16" to "a 1 by 2 array of (three vectors of u16)".

Now you will probably give me reasons why I can't have that. But when I am coding I don't hard how hard it is on the compiler writers (as long as I can express things unambiguously), but want to have it as easy as possible so I have brain cycles to spare to think about data layout and algorithms.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by cdirkx on Thursday November 21, 2019 @05:35PM (#21600493)
You can have anything you want with macros :)

I just wrote a macro `array` [1] that allows you to write

  #[no_mangle]
  static keymaps: array!{ u16[1, 2, 3] } = [
    [
      [1, 2, 3],
      [4, 5, 6],
    ],
  ];
or alternatively using a type alias

  type Keymaps = array!{ u16[1, 2, 3] };

  #[no_mangle]
  static keymaps: Keymaps = ...
On a more serious note, in general Rust favors explicit simple syntax: the only syntax related to arrays you need to learn is `[TYPE; LENGTH]` which is the way to write an array of type TYPE and length LENGTH, pretty straightforward. `[[[usize; 3]; 2]; 1]` is simply a composition of such arrays, as multidimensional arrays are just arrays of arrays.

C has a few more variants: the implicit length of `keymaps[]`, the `[0] = ...` initializer , the alternative `keymaps[1,2,3]` syntax. This is nice syntactic sugar, but you don't technically need it. Although if you really don't like the raw Rust syntax, you can always use macros like shown or a library like multiarray [2].

In a way I would say this makes Rust easier to learn: there are only a few symbols and patterns you need to learn to recognize, and the rest is compositions.

[1] WARNING: macro definitions are very symbol heavy, and thus even more unreadable.

https://play.rust-lang.org/?version=nightly&mode=debug&editi...

[2] https://docs.rs/multiarray/0.1.3/multiarray/

Re:How to make your C codebase Rusty: rewriting keyboard fir... by pcwalton on Thursday November 21, 2019 @07:01PM (#21601143)
The consequences of the lexer hack are hard on humans (typename, most vexing parse, order of declarations being significant, weird function type syntax), not just compiler writers.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by Ar-Curunir on Thursday November 21, 2019 @08:32PM (#21601665)
You can already define a custom type which will allow you to have a nice syntax for multidimensional arrays: `Matrix<1,2,3>`. It solves your issue of nesting brackets, and you can impl arbitrary indexing for it.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by kazagistar on Thursday November 21, 2019 @09:20PM (#21601891)
Unfortunately, rust does not have numeric types outside the special case baked in arrays, so it cannot do that yet afaik. There is a ticket for it, but it needs work.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by steveklabnik on Thursday November 21, 2019 @09:49PM (#21602004)
You can sorta do it kinda today: https://crates.io/crates/typenum

But it will be much nicer and better once const generics lands, it's true.

Re:How to make your C codebase Rusty: rewriting keyboard fir... by Ar-Curunir on Thursday November 21, 2019 @11:52PM (#21602668)
You can do it in nightly already.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by cdirkx on Thursday November 21, 2019 @06:10PM (#21600775)
Also, normally the array in Rust would also be a constant, with the keyword `const` instead of `static`: the reason it is static however, is so the C program can access it.
Re:How to make your C codebase Rusty: rewriting keyboard fir... by petschge on Thursday November 21, 2019 @07:24PM (#21601266)
I don't mind the const vs static. I mind just about everything else.