storable-static-array 0.5.0.0 → 0.5.0.1
raw patch · 3 files changed
+54/−10 lines, 3 files
Files
- Foreign/Marshal/StaticArray.hs +35/−5
- Foreign/Marshal/StaticArray/Unpromoted.hs +17/−3
- storable-static-array.cabal +2/−2
Foreign/Marshal/StaticArray.hs view
@@ -46,11 +46,11 @@ -- representation compatible with that of the result array to -- avoid that extra copy. --- -- The following functions provide correct implementations of- -- a minimum complete 'Storable' implementation for- -- 'StaticArray'. The helper function required by 'peek'' is- -- the part necessary for efficient implementations which- -- prevent creation of a fully polymorphic instance.+ -- The following functions provide a minimum complete,+ -- correct 'Storable' implementation for 'StaticArray'. The+ -- helper function required by 'peek'' is the part necessary+ -- for efficient implementations which prevent creation of a+ -- fully polymorphic instance. , sizeOf' , alignment' , poke'@@ -101,12 +101,14 @@ -- elements. This has all the semantic caveats of 'array', except that -- the bounds are as good as those provided by the 'StaticSize' -- instance.+{-# INLINEABLE staticArray #-} staticArray :: (Ix (Bound d), IArray b e, StaticSize d) => [(Bound d, e)] -> StaticArray b d e staticArray ls = let a = StaticArray $ array (extent a) ls in a -- | Create a new 'StaticArray' from a list of elements in index -- order. Implemented in terms of 'listArray'.+{-# INLINEABLE listStaticArray #-} listStaticArray :: (StaticSize d, Ix (Bound d), IArray b e) => [e] -> StaticArray b d e listStaticArray ls = let a = StaticArray $ listArray (extent a) ls in a@@ -115,17 +117,20 @@ -- | Get the size, in bytes, of the native representation of this -- 'StaticArray'.+{-# INLINEABLE sizeOf' #-} sizeOf' :: forall b d e. (StaticSize d, Ix (Bound d), Storable e) => StaticArray b d e -> Int sizeOf' a = sizeOf (undefined :: e) * rangeSize (extent a) -- | Get the alignment, in bytes, of the native representation of this -- 'StaticArray'+{-# INLINEABLE alignment' #-} alignment' :: forall b d e. Storable e => StaticArray b d e -> Int alignment' _ = alignment (undefined :: e) -- | Write the contents of this 'StaticArray' to the given location in -- memory.+{-# INLINEABLE poke' #-} poke' :: forall b d e. (Ix (Bound d), IArray b e, Storable e) => Ptr (StaticArray b d e) -> StaticArray b d e -> IO () poke' dst' arr = do@@ -140,6 +145,7 @@ -- result, then freezes it. The first argument is the -- freezing function. Non-copying implementations of 'unsafeFreeze' -- are safe as this argument, and preferred.+{-# INLINEABLE peek' #-} peek' :: forall b d e m . (StaticSize d, Ix (Bound d), Storable e, IArray b e, MArray m e IO) => (m (Bound d) e -> IO (b (Bound d) e)) ->@@ -160,17 +166,25 @@ instance (StaticSize d, Ix (Bound d), Storable e, IArray UArray e, MArray IOUArray e IO) => Storable (StaticArray UArray d e) where+ {-# INLINEABLE sizeOf#-} sizeOf = sizeOf'+ {-# INLINEABLE alignment #-} alignment = alignment'+ {-# INLINEABLE poke #-} poke = poke'+ {-# INLINEABLE peek #-} peek = peek' (unsafeFreeze :: IOUArray (Bound d) e -> IO (UArray (Bound d) e)) instance (StaticSize d, Ix (Bound d), Storable e) => Storable (StaticArray Array d e) where+ {-# INLINEABLE sizeOf #-} sizeOf = sizeOf'+ {-# INLINEABLE alignment #-} alignment = alignment'+ {-# INLINEABLE poke #-} poke = poke'+ {-# INLINEABLE peek #-} peek = peek' (unsafeFreeze :: IOArray (Bound d) e -> IO (Array (Bound d) e)) @@ -186,6 +200,7 @@ -- automatically fulfilled for all types of kind 'Nat'. Its explicit -- presence in the signature is an artifact of how GHC implements -- dictionary passing and type erasure.+{-# INLINEABLE fromNat #-} fromNat :: forall (proxy :: Nat -> *) (n :: Nat). SingI n => proxy n -> Int fromNat _ = fromInteger $ fromSing (sing :: Sing n) @@ -201,21 +216,25 @@ instance SingI n => StaticSize ('[n] :: [Nat]) where type Bound ('[n]) = Int+ {-# INLINEABLE extent #-} extent _ = (0, fromNat (Proxy :: Proxy n) - 1) instance (SingI n, StaticSize (n2 ': ns)) => StaticSize ((n ': n2 ': ns) :: [Nat]) where type Bound (n ': n2 ': ns) = (Int, Bound (n2 ': ns))+ {-# INLINEABLE extent #-} extent _ = ((0, b0), (fromNat (Proxy :: Proxy n) - 1, bn)) where (b0, bn) = extent (undefined :: StaticArray a (n2 ': ns) b) instance SingI a => StaticSize (a :: Nat) where type Bound a = Int+ {-# INLINEABLE extent #-} extent _ = (0, fromNat (Proxy :: Proxy a) - 1) instance (SingI a, SingI b) => StaticSize ('(a, b) :: (Nat, Nat)) where type Bound '(a, b) = (Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1))@@ -223,6 +242,7 @@ instance (SingI a, SingI b, SingI c) => StaticSize ('(a, b, c) :: (Nat, Nat, Nat)) where type Bound '(a, b, c) = (Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -231,6 +251,7 @@ instance (SingI a, SingI b, SingI c, SingI d) => StaticSize ('(a, b, c, d) :: (Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d) = (Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -240,6 +261,7 @@ instance (SingI a, SingI b, SingI c, SingI d, SingI e) => StaticSize ('(a, b, c, d, e) :: (Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e) = (Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -251,6 +273,7 @@ StaticSize ('(a, b, c, d, e, f) :: (Nat, Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e, f) = (Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -263,6 +286,7 @@ StaticSize ('(a, b, c, d, e, f, g) :: (Nat, Nat, Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e, f, g) = (Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -278,6 +302,7 @@ (Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e, f, g, h) = (Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -294,6 +319,7 @@ (Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e, f, g, h, i) = (Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -311,6 +337,7 @@ (Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat, Nat)) where type Bound '(a, b, c, d, e, f, g, h, i, j) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -330,6 +357,7 @@ where type Bound '(a, b, c, d, e, f, g, h, i, j, k) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -350,6 +378,7 @@ Nat)) where type Bound '(a, b, c, d, e, f, g, h, i, j, k, l) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -371,6 +400,7 @@ Nat, Nat)) where type Bound '(a, b, c, d, e, f, g, h, i, j, k, l, m) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,
Foreign/Marshal/StaticArray/Unpromoted.hs view
@@ -51,21 +51,23 @@ -- ('Int',('Int','Int')) 'Int'@ with bounds @((0,(0,0)),(1,(9,24)))@. -- -- Neither promoted lists nor this approach support creating--- 0-dimensional arrays, because they make no sense with 'Storable'.+-- 0-dimensional arrays, because they make no sense with+-- 'Foreign.Storable.Storable'. data a :. b+infixr 3 :. -- | 'Nil' is the terminator for type-level lists created with ':.' data Nil -infixr 3 :.- instance SingI n => StaticSize ((n :: Nat) :. Nil) where type Bound (n :. Nil) = Int+ {-# INLINEABLE extent #-} extent _ = (0, fromNat (Proxy :: Proxy n) - 1) instance (SingI n, StaticSize (n2 :. ns)) => StaticSize ((n :: Nat) :. n2 :. ns) where type Bound (n :. n2 :. ns) = (Int, Bound (n2 :. ns))+ {-# INLINEABLE extent #-} extent _ = ((0, b0), (fromNat (Proxy :: Proxy n) - 1, bn)) where (b0, bn) = extent (undefined :: StaticArray a (n2 :. ns) b)@@ -75,6 +77,7 @@ data D2 (a :: Nat) (b :: Nat) instance (SingI a, SingI b) => StaticSize (D2 a b) where type Bound (D2 a b) = (Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1))@@ -84,6 +87,7 @@ data D3 (a :: Nat) (b :: Nat) (c :: Nat) instance (SingI a, SingI b, SingI c) => StaticSize (D3 a b c) where type Bound (D3 a b c) = (Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -94,6 +98,7 @@ data D4 (a :: Nat) (b :: Nat) (c :: Nat) (d :: Nat) instance (SingI a, SingI b, SingI c, SingI d) => StaticSize (D4 a b c d) where type Bound (D4 a b c d) = (Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -106,6 +111,7 @@ instance (SingI a, SingI b, SingI c, SingI d, SingI e) => StaticSize (D5 a b c d e) where type Bound (D5 a b c d e) = (Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -119,6 +125,7 @@ instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f) => StaticSize (D6 a b c d e f) where type Bound (D6 a b c d e f) = (Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -134,6 +141,7 @@ instance (SingI a, SingI b, SingI c, SingI d, SingI e, SingI f, SingI g) => StaticSize (D7 a b c d e f g) where type Bound (D7 a b c d e f g) = (Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -151,6 +159,7 @@ SingI h) => StaticSize (D8 a b c d e f g h) where type Bound (D8 a b c d e f g h) = (Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -170,6 +179,7 @@ StaticSize (D9 a b c d e f g h i) where type Bound (D9 a b c d e f g h i) = (Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -190,6 +200,7 @@ StaticSize (D10 a b c d e f g h i j) where type Bound (D10 a b c d e f g h i j) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -211,6 +222,7 @@ StaticSize (D11 a b c d e f g h i j k) where type Bound (D11 a b c d e f g h i j k) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -233,6 +245,7 @@ StaticSize (D12 a b c d e f g h i j k l) where type Bound (D12 a b c d e f g h i j k l) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,@@ -257,6 +270,7 @@ StaticSize (D13 a b c d e f g h i j k l m) where type Bound (D13 a b c d e f g h i j k l m) = (Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int, Int)+ {-# INLINEABLE extent #-} extent _ = ((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), (fromNat (Proxy :: Proxy a) - 1, fromNat (Proxy :: Proxy b) - 1,
storable-static-array.cabal view
@@ -1,5 +1,5 @@ name: storable-static-array-version: 0.5.0.0+version: 0.5.0.1 synopsis: Statically-sized array wrappers with Storable instances for FFI marshaling description: Uses type-level numeric literals to wrap arrays in a type@@ -23,4 +23,4 @@ exposed-modules: Foreign.Marshal.StaticArray, Foreign.Marshal.StaticArray.Unpromoted build-depends: base ==4.6.*, array ==0.4.*, tagged ==0.6.*- ghc-options: -Wall+ ghc-options: -Wall -O2