mgsloan / store

Fast binary serialization in Haskell
MIT License
109 stars 35 forks source link

How to get an instance of Store for Data.UUID from the uuid package? #129

Closed donatello closed 6 years ago

donatello commented 6 years ago

I tried something along the lines of:

import Data.Store as Store
import Data.UUID as U

instance Generic U.UUID
instance Store.Store U.UUID

but I get:

Types.hs:242:10: error:
    • No instance for (Store.GStoreSize (Rep U.UUID))
        arising from a use of ‘store-0.4.3.2:Data.Store.Impl.$dmsize’
    • In the expression: store-0.4.3.2:Data.Store.Impl.$dmsize @U.UUID
      In an equation for ‘Store.size’:
          Store.size = store-0.4.3.2:Data.Store.Impl.$dmsize @U.UUID
      In the instance declaration for ‘Store.Store U.UUID’
    |
242 | instance Store.Store U.UUID
    |          ^^^^^^^^^^^^^^^^^^

Types.hs:242:10: error:
    • No instance for (Store.GStorePoke (Rep U.UUID))
        arising from a use of ‘store-0.4.3.2:Data.Store.Impl.$dmpoke’
    • In the expression: store-0.4.3.2:Data.Store.Impl.$dmpoke @U.UUID
      In an equation for ‘Store.poke’:
          Store.poke = store-0.4.3.2:Data.Store.Impl.$dmpoke @U.UUID
      In the instance declaration for ‘Store.Store U.UUID’
    |
242 | instance Store.Store U.UUID
    |          ^^^^^^^^^^^^^^^^^^

Types.hs:242:10: error:
    • No instance for (Store.GStorePeek (Rep U.UUID))
        arising from a use of ‘store-0.4.3.2:Data.Store.Impl.$dmpeek’
    • In the expression: store-0.4.3.2:Data.Store.Impl.$dmpeek @U.UUID
      In an equation for ‘Store.peek’:
          Store.peek = store-0.4.3.2:Data.Store.Impl.$dmpeek @U.UUID
      In the instance declaration for ‘Store.Store U.UUID’
    |
242 | instance Store.Store U.UUID
    |          ^^^^^^^^^^^^^^^^^^
jm4games commented 6 years ago

You are better off just doing this:

instance Store UUID where
  size = ConstSize 16
  poke uuid =
    case toWords uuid of
      (w1, w2, w3, w4) -> poke w1 >> poke w2 >> poke w3 >> poke w4
  peek = fromWords <$> peek <*> peek <*> peek <*> peek
mgsloan commented 6 years ago

Yep, @jm4games 's suggestion is close to what I've used previously. GHC probably inlines the toWords / fromWords, but you can also import Data.UUID.Types.Internal and directly use the UUID constructor.