[go: up one dir, main page]

support from_num as a const fn? (or workaround)

With the latest 1.46.0 update, loop, match and other logical operators have been stabilized to be used in const functions.

Something I often find myself doing is storing const as integers, and then doing something like let my_const = I48F16::from_num(MY_CONST), and then using my_const separately.

Correct me if I'm wrong, but something as trivial as computing a half currently requires us to do something like let halved = my_value / I46F16::from_num(2). It might not sound like a lot, but when you have tons of constants it does start to take some space in your editor.

It might also be computational heavy, although I'm not sure about that since I have not done any benchmark regarding how fast from_num is.

It's currently not possible to my knowledge to have const fn in traits, but perhaps there can be a workaround we could use somehow? It looks like there is a heavy use of macros in this crate, maybe we could have a const fn from_num clone for every Fixed type, that would then be used from the trait itself? People like me who would like to use Fixed constants could using this workaround, while still keeping the trait behavior.

Obviously it's just a suggestion, but I would love to have your input on this.