[Haskell-cafe] ANNOUNCE: storable-endian

Henning Thielemann lemming at henning-thielemann.de
Thu Dec 23 00:58:04 CET 2010


On Thu, 23 Dec 2010, Henning Thielemann wrote:

> On Wed, 22 Dec 2010, Eugene Kirpichov wrote:
>
>> It defines types like {{Int,Word}{16,32,64},Double,Float}{LE,BE} (for
>> example Int32BE) etc. with a corresponding Storable instance.
>
> How about type constructors LittleEndian and BigEndian?
>
> newtype LittleEndian a = LittleEndian a
>
> Maybe using some type classes you can even get rid of Template Haskell and 
> get plain Haskell 98?

Yes, I think you could have (given a module Data.Storable.LittleEndian as 
LE)

instance LE.Storable a => Storable (LittleEndian a) where
    sizeOf (LittleEndian a) = sizeOf a
    alignment (LittleEndian a) = alignment a
    peek p = fmap LittleEndian $ LE.peek p
    poke p (LittleEndian a) = LE.poke p a

class LE.Storable a where
    LE.peek :: Ptr a -> IO a
    LE.poke :: Ptr a -> a -> IO ()

instance LE.Storable Word16 where
    LE.peek p = getWord16le (castPtr p)
    LE.poke p = putWord16le (castPtr p)

...

I find this much cleaner and simpler to extend to other types.



More information about the Haskell-Cafe mailing list