icu4x icon indicating copy to clipboard operation
icu4x copied to clipboard

Add CharULE::from_array<N>(chars: [char; N]) -> [CharULE; N]

Open hsivonen opened this issue 1 year ago • 4 comments

Add const-evaluable CharULE::from_array<N>(chars: [char; N]) -> [CharULE; N]. (I'm not sure if const evaluation is powerful enough to do this.)

hsivonen avatar Aug 31 '22 05:08 hsivonen

It is whenever https://doc.rust-lang.org/stable/std/primitive.array.html#method.map stabilizes for const, but not before. We're not really doing to/from ULE conversions over const since traits can't be invoked in const anyway.

I'd prefer we don't do this: it's impossible to do until [T; N]::map() is stable const; and once that method is stable my recommendation would be to just use it.

Manishearth avatar Sep 01 '22 15:09 Manishearth

This isn't true. We already have pub const fn RawBytesULE::from_array<const N: usize>(arr: [u16; N]) -> [Self; N]

https://unicode-org.github.io/icu4x-docs/doc/zerovec/ule/struct.RawBytesULE.html#method.from_array

It doesn't use map. It just uses plain index operations, which work in const.

sffc avatar Sep 01 '22 15:09 sffc

I'd prefer we don't do this: it's impossible to do until [T; N]::map() is stable const; and once that method is stable my recommendation would be to just use it.

When such a function is added, we still can't avoid the necessity of type-specific conversion functions, since it seems unlikely that we'll be able to call trait functions in const in the near term. In other words, we will need one of the following for each ULE type:

  1. U::from_array<N>(arr: [T; N]) -> [U; N]
  2. U::as_ule_const(t: T) -> U

sffc avatar Sep 01 '22 16:09 sffc

Related: https://github.com/unicode-org/icu4x/issues/1935

sffc avatar Sep 01 '22 16:09 sffc