Derive Unbox, Storable and a bunch of others for Finitary automagically.
Koz Ross fff7ac5b59 Documentation fix | 5 سال پیش | |
---|---|---|
src | 5 سال پیش | |
test | 5 سال پیش | |
.gitignore | 5 سال پیش | |
.vimrc | 5 سال پیش | |
CHANGELOG.md | 5 سال پیش | |
LICENSE.md | 5 سال پیش | |
README.md | 5 سال پیش | |
Setup.hs | 5 سال پیش | |
cabal.project | 5 سال پیش | |
finitary-derive.cabal | 5 سال پیش |
finitary-derive
Have you ever written an Unbox
instance for a user-defined type? I hope not,
because it's a uniquely tedious chore. If your type is more complex, this
can be difficult, fiddly, and frustrating. Storable
is not much better. This
is the kind of 'work' that we as Haskellers ought not to put up with.
Now, you don't have to! As long as your type is Finitary
, you can now
get Unbox
and Storable
(as well as Binary
and Hashable
, because
we could) instances almost automagically:
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
import Data.Finitary
import Data.Finitary.Pack
import Data.Word
import Data.Hashable
import qualified Data.Vector.Unboxed as VU
data Foo = Bar | Baz (Word8, Word8) | Quux Word16
deriving (Eq, Generic, Finitary)
deriving (Storable, Binary, Hashable) via (Pack Foo)
someVector :: VU.Vector (Pack Foo)
someVector = VU.fromList . fmap Pack $ [Bar, Baz 0x0 0xf, Quux 0x134]
If you don't have access to DerivingVia
, you can still get the benefits of
this library -- just use Pack a
instead of a
in all cases where you need
any such instances. As it is a newtype
, you can coerce
through it if you
care about efficiency.
Unbox
too?The short answer is 'role restrictions on unboxed vectors'. If you want a more
detailed explanation, check out the GHC wiki on roles, as well as the
implementation of Data.Vector.Unboxed
. You might also want to check out
stuff about data families, as it ties into this rather aggravating
limitation closely too.
Certainly - we've tested on GHC 8.4.4, 8.6.5 and 8.8.1, on GNU/Linux only. If you would like support for any additional GHC versions, let us know.
If you build and use this library successfully on any other platforms, we'd like to know too - it'd be beneficial even if nothing breaks, and especially if something does.
This library is under the GNU General Public License, version 3 or later (SPDX
code GPL-3.0-or-later
). For more details, see the LICENSE.md
file.