optional icon indicating copy to clipboard operation
optional copied to clipboard

Use const generics

Open llogiq opened this issue 3 years ago • 3 comments

Rust has const generics now, so we may want to use them to make the Noned trait generic over the none value (although this will likely require a workaround for floats; perhaps we need to split the types between integers and floats).

llogiq avatar Apr 29 '21 20:04 llogiq

I've been using optional for parsing and storing weather data that can have many interleaved missing values. The file format I most commonly parse uses -9999.0 as the missing value flag. So this wouldn't be very helpful for my use case until we can use floats in const generics. I like the idea though of being able to encode the "missing value" into the type.

It would also be good then to have From and Into implementations that convert between types with the same inner type (e.g. float) but different none values. For instance, when I currently parse a -9999.0, I convert it to a NaN and wrap it in Optioned. It would be nice to just use From for that.

rnleach avatar Apr 30 '21 14:04 rnleach

I think we should be able to create a more generic Noned trait that could have custom impls of .is_none() (for example matching all values above 9999.0), and then have a type that implements that trait for a const usize.

llogiq avatar Apr 30 '21 16:04 llogiq

I like the idea, but after looking over the code, I have no idea how to do it with const generics. As it is now, I can wrap anything in a newtype and implement Noned with any logic I want.

rnleach avatar May 02 '21 04:05 rnleach