|
| 1 | +{-# LANGUAGE DerivingVia #-} |
| 2 | +{-# LANGUAGE FlexibleInstances #-} |
| 3 | +{-# LANGUAGE GeneralizedNewtypeDeriving #-} |
| 4 | +{-# LANGUAGE MagicHash #-} |
| 5 | +{-# LANGUAGE MultiParamTypeClasses #-} |
| 6 | +{-# LANGUAGE StandaloneDeriving #-} |
| 7 | +{-# LANGUAGE TypeApplications #-} |
| 8 | +{-# LANGUAGE TypeFamilies #-} |
| 9 | +{-# LANGUAGE TypeOperators #-} |
| 10 | +{-# LANGUAGE UnboxedTuples #-} |
| 11 | +-- | |
| 12 | +-- These tests make sure that derived Unbox instances actually works. |
| 13 | +-- It's distressingly easy to forget to export some constructor and |
| 14 | +-- make seemingly fine code noncompilable. |
| 15 | +-- |
| 16 | +-- We're only interested in checking whether examples compiling. |
| 17 | +-- Doctests aren't reliable in ensuring that! |
| 18 | +module Tests.Deriving () where |
| 19 | + |
| 20 | +import Control.DeepSeq |
| 21 | +import qualified Data.Vector.Generic as VG |
| 22 | +import qualified Data.Vector.Generic.Mutable as VGM |
| 23 | +import qualified Data.Vector.Storable as VS |
| 24 | +import qualified Data.Vector.Primitive as VP |
| 25 | +import qualified Data.Vector.Unboxed as VU |
| 26 | + |
| 27 | + |
| 28 | +---------------------------------------------------------------- |
| 29 | +-- Primitive |
| 30 | + |
| 31 | +newtype FooP1 = FooP1 Int deriving VP.Prim |
| 32 | + |
| 33 | +newtype instance VU.MVector s FooP1 = MV_FooP1 (VP.MVector s FooP1) |
| 34 | +newtype instance VU.Vector FooP1 = V_FooP1 (VP.Vector FooP1) |
| 35 | +deriving via (VU.UnboxViaPrim FooP1) instance VGM.MVector VU.MVector FooP1 |
| 36 | +deriving via (VU.UnboxViaPrim FooP1) instance VG.Vector VU.Vector FooP1 |
| 37 | +instance VU.Unbox FooP1 |
| 38 | + |
| 39 | + |
| 40 | + |
| 41 | +newtype FooP2 = FooP2 Int |
| 42 | + |
| 43 | +newtype instance VU.MVector s FooP2 = MV_FooP2 (VP.MVector s Int) |
| 44 | +newtype instance VU.Vector FooP2 = V_FooP2 (VP.Vector Int) |
| 45 | +deriving via (VU.UnboxViaPrim Int) instance VGM.MVector VU.MVector FooP2 |
| 46 | +deriving via (VU.UnboxViaPrim Int) instance VG.Vector VU.Vector FooP2 |
| 47 | +instance VU.Unbox FooP2 |
| 48 | + |
| 49 | + |
| 50 | +---------------------------------------------------------------- |
| 51 | +-- Storable |
| 52 | + |
| 53 | +newtype FooS1 = FooS1 Int deriving VS.Storable |
| 54 | + |
| 55 | +newtype instance VU.MVector s FooS1 = MV_FooS1 (VS.MVector s FooS1) |
| 56 | +newtype instance VU.Vector FooS1 = V_FooS1 (VS.Vector FooS1) |
| 57 | +deriving via (VU.UnboxViaStorable FooS1) instance VGM.MVector VU.MVector FooS1 |
| 58 | +deriving via (VU.UnboxViaStorable FooS1) instance VG.Vector VU.Vector FooS1 |
| 59 | +instance VU.Unbox FooS1 |
| 60 | + |
| 61 | + |
| 62 | +newtype FooS2 = FooS2 Int |
| 63 | + |
| 64 | +newtype instance VU.MVector s FooS2 = MV_FooS2 (VS.MVector s Int) |
| 65 | +newtype instance VU.Vector FooS2 = V_FooS2 (VS.Vector Int) |
| 66 | +deriving via (VU.UnboxViaStorable Int) instance VGM.MVector VU.MVector FooS2 |
| 67 | +deriving via (VU.UnboxViaStorable Int) instance VG.Vector VU.Vector FooS2 |
| 68 | +instance VU.Unbox FooS2 |
| 69 | + |
| 70 | + |
| 71 | +---------------------------------------------------------------- |
| 72 | +-- Boxed variants |
| 73 | + |
| 74 | + |
| 75 | +data FooLazy a = FooLazy Int a |
| 76 | + deriving (Eq, Ord, Show) |
| 77 | + |
| 78 | +instance VU.IsoUnbox (FooLazy a) (Int, VU.DoNotUnboxLazy a) where |
| 79 | + toURepr (FooLazy i a) = (i, VU.DoNotUnboxLazy a) |
| 80 | + fromURepr (i, VU.DoNotUnboxLazy a) = FooLazy i a |
| 81 | + {-# INLINE toURepr #-} |
| 82 | + {-# INLINE fromURepr #-} |
| 83 | + |
| 84 | +newtype instance VU.MVector s (FooLazy a) = MV_FooLazy (VU.MVector s (Int, VU.DoNotUnboxLazy a)) |
| 85 | +newtype instance VU.Vector (FooLazy a) = V_FooLazy (VU.Vector (Int, VU.DoNotUnboxLazy a)) |
| 86 | +deriving via (FooLazy a `VU.As` (Int, VU.DoNotUnboxLazy a)) instance VGM.MVector VU.MVector (FooLazy a) |
| 87 | +deriving via (FooLazy a `VU.As` (Int, VU.DoNotUnboxLazy a)) instance VG.Vector VU.Vector (FooLazy a) |
| 88 | +instance VU.Unbox (FooLazy a) |
| 89 | + |
| 90 | + |
| 91 | + |
| 92 | +data FooStrict a = FooStrict Int a |
| 93 | + deriving (Eq, Ord, Show) |
| 94 | + |
| 95 | +instance VU.IsoUnbox (FooStrict a) (Int, VU.DoNotUnboxStrict a) where |
| 96 | + toURepr (FooStrict i a) = (i, VU.DoNotUnboxStrict a) |
| 97 | + fromURepr (i, VU.DoNotUnboxStrict a) = FooStrict i a |
| 98 | + {-# INLINE toURepr #-} |
| 99 | + {-# INLINE fromURepr #-} |
| 100 | + |
| 101 | +newtype instance VU.MVector s (FooStrict a) = MV_FooStrict (VU.MVector s (Int, VU.DoNotUnboxStrict a)) |
| 102 | +newtype instance VU.Vector (FooStrict a) = V_FooStrict (VU.Vector (Int, VU.DoNotUnboxStrict a)) |
| 103 | +deriving via (FooStrict a `VU.As` (Int, VU.DoNotUnboxStrict a)) instance VGM.MVector VU.MVector (FooStrict a) |
| 104 | +deriving via (FooStrict a `VU.As` (Int, VU.DoNotUnboxStrict a)) instance VG.Vector VU.Vector (FooStrict a) |
| 105 | +instance VU.Unbox (FooStrict a) |
| 106 | + |
| 107 | + |
| 108 | +data FooNormalForm a = FooNormalForm Int a |
| 109 | + deriving (Eq, Ord, Show) |
| 110 | + |
| 111 | +instance VU.IsoUnbox (FooNormalForm a) (Int, VU.DoNotUnboxNormalForm a) where |
| 112 | + toURepr (FooNormalForm i a) = (i, VU.DoNotUnboxNormalForm a) |
| 113 | + fromURepr (i, VU.DoNotUnboxNormalForm a) = FooNormalForm i a |
| 114 | + {-# INLINE toURepr #-} |
| 115 | + {-# INLINE fromURepr #-} |
| 116 | + |
| 117 | +newtype instance VU.MVector s (FooNormalForm a) = MV_FooNormalForm (VU.MVector s (Int, VU.DoNotUnboxNormalForm a)) |
| 118 | +newtype instance VU.Vector (FooNormalForm a) = V_FooNormalForm (VU.Vector (Int, VU.DoNotUnboxNormalForm a)) |
| 119 | +deriving via (FooNormalForm a `VU.As` (Int, VU.DoNotUnboxNormalForm a)) |
| 120 | + instance NFData a => VGM.MVector VU.MVector (FooNormalForm a) |
| 121 | +deriving via (FooNormalForm a `VU.As` (Int, VU.DoNotUnboxNormalForm a)) |
| 122 | + instance NFData a => VG.Vector VU.Vector (FooNormalForm a) |
| 123 | +instance NFData a => VU.Unbox (FooNormalForm a) |
| 124 | + |
| 125 | + |
| 126 | + |
| 127 | +---------------------------------------------------------------- |
| 128 | +-- Unboxed |
| 129 | + |
| 130 | + |
| 131 | +data FooAs a = FooAs Int a |
| 132 | + deriving Show |
| 133 | + |
| 134 | +instance VU.IsoUnbox (FooAs a) (Int,a) where |
| 135 | + toURepr (FooAs i a) = (i,a) |
| 136 | + fromURepr (i,a) = FooAs i a |
| 137 | + {-# INLINE toURepr #-} |
| 138 | + {-# INLINE fromURepr #-} |
| 139 | + |
| 140 | +newtype instance VU.MVector s (FooAs a) = MV_FooAs (VU.MVector s (Int, a)) |
| 141 | +newtype instance VU.Vector (FooAs a) = V_FooAs (VU.Vector (Int, a)) |
| 142 | +deriving via (FooAs a `VU.As` (Int, a)) instance VU.Unbox a => VGM.MVector VU.MVector (FooAs a) |
| 143 | +deriving via (FooAs a `VU.As` (Int, a)) instance VU.Unbox a => VG.Vector VU.Vector (FooAs a) |
| 144 | +instance VU.Unbox a => VU.Unbox (FooAs a) |
0 commit comments