Support conversion from (UArray i Word8) to ShortByteString?

Viktor Dukhovni ietf-dane at dukhovni.org
Tue Nov 14 06:10:03 UTC 2023


On Mon, Nov 13, 2023 at 09:35:06PM -0500, Matthew Craven wrote:

> Your proposed `arrayToByteArray` seems plausible.

Thanks, that could be handy, but see below.

> Your proposed `byteArrayToShort` is just the newtype-constructor
> `ShortByteString` which is exposed from Data.ByteString.Short since
> bytestring-0.12.0.0.

Thanks.  I did not notice these are now exposed without having to import
unstable "Internal" interfaces:

    GHCi, version 9.8.1: ...
    λ> import Data.Array.Byte
    λ> import Data.ByteString.Short
    λ> :t ShortByteString
    ShortByteString :: Data.Array.Byte.ByteArray -> ShortByteString
    λ> :t SBS
    SBS :: GHC.Prim.ByteArray# -> ShortByteString
    λ> :t ByteArray
    ByteArray :: GHC.Prim.ByteArray# -> ByteArray

So as of GHC 9.8.1 and "bytestring" 12, I have all the missing glue.

> > An alternative is to add a tailored version of the UArray and STUArray
> > APIs to 'MutableByteArray' by extending the rather limited API of
> > 'Data.Array.Byte':
> 
> How does this compare to the interface provided by
> primitive:Data.Primitive.ByteArray? Their `runByteArray` is your
> `runMutableByteArray`.

I was unaware that the "primitive" packages already provides what I was
looking for.  Unlike the case with "array", there's no duplication of
bounds checks for performing separate read/write at the same index
(because there are no bounds checks), so the "missing" will not be
missed.

Looks like I'm all set.  Just need to use 'SBS' in place of
'ShortByteString' while working with GHC 9.[246].*.

For my use case, I don't need the additional safety (bounds checks) of
"array", but it is perhaps reasonable to consider adding the proposed
bridge (from UArray i Word8), for users who want a bit more safety than
one gets with "primitive".

-- 
    Viktor.


More information about the ghc-devs mailing list