primitive-checked 0.6.4.2 → 0.7.0.0
raw patch · 6 files changed
+34/−190 lines, 6 filesdep ~primitivePVP ok
version bump matches the API change (PVP)
Dependency ranges changed: primitive
API changes (from Hackage documentation)
- Data.Primitive.UnliftedArray: MutableUnliftedArray :: MutableArrayArray# s -> MutableUnliftedArray s e
- Data.Primitive.UnliftedArray: UnliftedArray :: ArrayArray# -> UnliftedArray e
- Data.Primitive.UnliftedArray: class PrimUnlifted a
- Data.Primitive.UnliftedArray: cloneMutableUnliftedArray :: (HasCallStack, PrimMonad m) => MutableUnliftedArray (PrimState m) a -> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
- Data.Primitive.UnliftedArray: cloneUnliftedArray :: HasCallStack => UnliftedArray a -> Int -> Int -> UnliftedArray a
- Data.Primitive.UnliftedArray: copyMutableUnliftedArray :: (HasCallStack, PrimMonad m) => MutableUnliftedArray (PrimState m) a -> Int -> MutableUnliftedArray (PrimState m) a -> Int -> Int -> m ()
- Data.Primitive.UnliftedArray: copyUnliftedArray :: (HasCallStack, PrimMonad m) => MutableUnliftedArray (PrimState m) a -> Int -> UnliftedArray a -> Int -> Int -> m ()
- Data.Primitive.UnliftedArray: data MutableUnliftedArray s e
- Data.Primitive.UnliftedArray: data UnliftedArray e
- Data.Primitive.UnliftedArray: foldlUnliftedArray :: PrimUnlifted a => (b -> a -> b) -> b -> UnliftedArray a -> b
- Data.Primitive.UnliftedArray: foldlUnliftedArray' :: PrimUnlifted a => (b -> a -> b) -> b -> UnliftedArray a -> b
- Data.Primitive.UnliftedArray: foldrUnliftedArray :: PrimUnlifted a => (a -> b -> b) -> b -> UnliftedArray a -> b
- Data.Primitive.UnliftedArray: foldrUnliftedArray' :: PrimUnlifted a => (a -> b -> b) -> b -> UnliftedArray a -> b
- Data.Primitive.UnliftedArray: freezeUnliftedArray :: (HasCallStack, PrimMonad m) => MutableUnliftedArray (PrimState m) a -> Int -> Int -> m (UnliftedArray a)
- Data.Primitive.UnliftedArray: fromArrayArray# :: PrimUnlifted a => ArrayArray# -> a
- Data.Primitive.UnliftedArray: indexUnliftedArray :: (HasCallStack, PrimUnlifted a) => UnliftedArray a -> Int -> a
- Data.Primitive.UnliftedArray: indexUnliftedArrayM :: (HasCallStack, Monad m, PrimUnlifted a) => UnliftedArray a -> Int -> m a
- Data.Primitive.UnliftedArray: mapUnliftedArray :: (PrimUnlifted a, PrimUnlifted b) => (a -> b) -> UnliftedArray a -> UnliftedArray b
- Data.Primitive.UnliftedArray: newUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => Int -> a -> m (MutableUnliftedArray (PrimState m) a)
- Data.Primitive.UnliftedArray: readUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> Int -> m a
- Data.Primitive.UnliftedArray: sameMutableUnliftedArray :: () => MutableUnliftedArray s a -> MutableUnliftedArray s a -> Bool
- Data.Primitive.UnliftedArray: setUnliftedArray :: (PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> a -> m ()
- Data.Primitive.UnliftedArray: sizeofMutableUnliftedArray :: () => MutableUnliftedArray s e -> Int
- Data.Primitive.UnliftedArray: sizeofUnliftedArray :: () => UnliftedArray e -> Int
- Data.Primitive.UnliftedArray: thawUnliftedArray :: (HasCallStack, PrimMonad m) => UnliftedArray a -> Int -> Int -> m (MutableUnliftedArray (PrimState m) a)
- Data.Primitive.UnliftedArray: toArrayArray# :: PrimUnlifted a => a -> ArrayArray#
- Data.Primitive.UnliftedArray: unliftedArrayFromList :: PrimUnlifted a => [a] -> UnliftedArray a
- Data.Primitive.UnliftedArray: unsafeFreezeUnliftedArray :: PrimMonad m => MutableUnliftedArray (PrimState m) a -> m (UnliftedArray a)
- Data.Primitive.UnliftedArray: unsafeNewUnliftedArray :: (HasCallStack, PrimMonad m) => Int -> m (MutableUnliftedArray (PrimState m) a)
- Data.Primitive.UnliftedArray: writeUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()
+ Data.Primitive.ByteArray: copyMutableByteArrayToAddr :: PrimMonad m => Ptr Word8 -> MutableByteArray (PrimState m) -> Int -> Int -> m ()
- Data.Primitive.ByteArray: byteArrayContents :: ByteArray -> Addr
+ Data.Primitive.ByteArray: byteArrayContents :: ByteArray -> Ptr Word8
- Data.Primitive.ByteArray: mutableByteArrayContents :: () => MutableByteArray s -> Addr
+ Data.Primitive.ByteArray: mutableByteArrayContents :: () => MutableByteArray s -> Ptr Word8
Files
- primitive-checked.cabal +5/−6
- src/Data/Primitive.hs +0/−4
- src/Data/Primitive/ByteArray.hs +8/−12
- src/Data/Primitive/PrimArray.hs +7/−2
- src/Data/Primitive/SmallArray.hs +14/−4
- src/Data/Primitive/UnliftedArray.hs +0/−162
primitive-checked.cabal view
@@ -1,13 +1,13 @@-cabal-version: 2.0+cabal-version: 2.2 name: primitive-checked-version: 0.6.4.2+version: 0.7.0.0 synopsis: primitive functions with bounds-checking homepage: https://github.com/andrewthad/primitive-checked#readme bug-reports: https://github.com/andrewthad/primitive-checked/issues author: Andrew Martin maintainer: andrew.thaddeus@gmail.com copyright: 2018 Andrew Martin-license: BSD3+license: BSD-3-Clause license-file: LICENSE build-type: Simple category: Array@@ -41,21 +41,20 @@ src build-depends: base >=4.9.1.0 && <5- , primitive == 0.6.4.*+ , primitive == 0.7.0.* exposed-modules: Data.Primitive Data.Primitive.Array Data.Primitive.SmallArray Data.Primitive.ByteArray Data.Primitive.PrimArray- Data.Primitive.UnliftedArray reexported-modules: Control.Monad.Primitive- , Data.Primitive.Addr , Data.Primitive.MachDeps , Data.Primitive.MutVar , Data.Primitive.MVar , Data.Primitive.Ptr , Data.Primitive.Types default-language: Haskell2010+ ghc-options: -Wall
src/Data/Primitive.hs view
@@ -2,18 +2,14 @@ module Data.Primitive.Types, module Data.Primitive.Array, module Data.Primitive.ByteArray,- module Data.Primitive.Addr, module Data.Primitive.PrimArray, module Data.Primitive.SmallArray,- module Data.Primitive.UnliftedArray, module Data.Primitive.MutVar ) where -import Data.Primitive.Addr import Data.Primitive.Array import Data.Primitive.ByteArray import Data.Primitive.MutVar import Data.Primitive.PrimArray import Data.Primitive.SmallArray import Data.Primitive.Types-import Data.Primitive.UnliftedArray
src/Data/Primitive/ByteArray.hs view
@@ -29,6 +29,7 @@ , moveByteArray , setByteArray , fillByteArray+ , A.copyMutableByteArrayToAddr -- * Information , A.sizeofByteArray , A.sizeofMutableByteArray@@ -56,19 +57,14 @@ elementSizeofByteArray :: forall a. Prim a => Proxy a -> ByteArray -> Int elementSizeofByteArray _ arr = div (A.sizeofByteArray arr) (sizeOf (undefined :: a)) -elementSizeofByteArrayPermissive :: forall a. Prim a => Proxy a -> ByteArray -> Int-elementSizeofByteArrayPermissive _ arr =- div (A.sizeofByteArray arr) (sizeOf (undefined :: a)) + - ( if rem (A.sizeofByteArray arr) (sizeOf (undefined :: a)) == 0- then 0- else 1- ) - elementSizeofMutableByteArray :: forall s a. Prim a => Proxy a -> MutableByteArray s -> Int elementSizeofMutableByteArray _ arr = div (A.sizeofMutableByteArray arr) (sizeOf (undefined :: a)) newByteArray :: (HasCallStack, PrimMonad m) => Int -> m (MutableByteArray (PrimState m))-newByteArray n = check "newByteArray: negative size" (n>=0) (A.newByteArray n)+newByteArray n =+ check "newByteArray: negative size" (n>=0)+ $ check ("newByteArray: reqeusted " ++ show n ++ " bytes") (n<1024*1024*1024)+ $ (A.newByteArray n) newPinnedByteArray :: (HasCallStack, PrimMonad m) => Int -> m (MutableByteArray (PrimState m)) newPinnedByteArray n = check "newPinnedByteArray: negative size" (n>=0) (A.newPinnedByteArray n)@@ -94,7 +90,7 @@ -- the last machine word of the byte array. indexByteArray :: forall a. (HasCallStack, Prim a) => ByteArray -> Int -> a indexByteArray arr i = check "indexByteArray: index of out bounds"- (i>=0 && i< elementSizeofByteArrayPermissive (Proxy :: Proxy a) arr)+ (i>=0 && i< elementSizeofByteArray (Proxy :: Proxy a) arr) (A.indexByteArray arr i) copyByteArray :: forall m. (HasCallStack, PrimMonad m)@@ -153,8 +149,8 @@ -> Int -- ^ number of values to fill -> a -- ^ value to fill with -> m ()-setByteArray dst doff sz x = - check "copyMutableByteArray: index range of out bounds"+setByteArray dst doff sz x =+ check "setByteArray: index range of out bounds" (doff>=0 && (doff+sz)<=elementSizeofMutableByteArray (Proxy :: Proxy a) dst) (A.setByteArray dst doff sz x)
src/Data/Primitive/PrimArray.hs view
@@ -87,7 +87,12 @@ check errMsg False _ = throw (IndexOutOfBounds $ "Data.Primitive.PrimArray." ++ errMsg ++ "\n" ++ prettyCallStack callStack) newPrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a) => Int -> m (MutablePrimArray (PrimState m) a)-newPrimArray n = check "newPrimArray: negative size" (n>=0) (A.newPrimArray n)+newPrimArray n =+ check "newPrimArray: negative size" (n>=0)+ $ check ("newPrimArray: requested " ++ show n ++ " elements of size " ++ show elemSz) (n * elemSz < 1024*1024*1024)+ $ A.newPrimArray n+ where+ elemSz = sizeOf (undefined :: a) -- | After a call to resizeMutablePrimArray, the original reference to -- the mutable array should not be used again. This cannot truly be enforced@@ -106,7 +111,7 @@ A.setPrimArray (A.MutablePrimArray x) 0 (sz * sizeOf (undefined :: a)) (0xFF :: Word8) return marr' --- | This corrupts the contents of the mutable argument array.+-- | This corrupts the contents of the argument array. unsafeFreezePrimArray :: forall m a. (HasCallStack, PrimMonad m, Prim a) => MutablePrimArray (PrimState m) a -> m (PrimArray a)
src/Data/Primitive/SmallArray.hs view
@@ -26,12 +26,15 @@ , A.smallArrayFromListN ) where -import Control.Monad.Primitive (PrimMonad,PrimState)-import Control.Exception (throw, ArrayException(..), Exception, toException)+import "primitive" Data.Primitive (sizeOf) import "primitive" Data.Primitive.SmallArray (SmallArray,SmallMutableArray)-import qualified "primitive" Data.Primitive.SmallArray as A++import Control.Exception (throw, ArrayException(..), Exception, toException)+import Control.Monad.Primitive (PrimMonad,PrimState) import GHC.Exts (raise#) import GHC.Stack++import qualified "primitive" Data.Primitive.SmallArray as A import qualified Data.List as L check :: HasCallStack => String -> Bool -> a -> a@@ -46,7 +49,12 @@ throwUnary e = raise# (toException e) newSmallArray :: (HasCallStack, PrimMonad m) => Int -> a -> m (SmallMutableArray (PrimState m) a)-newSmallArray n x = check "newSmallArray: negative size" (n>=0) (A.newSmallArray n x)+newSmallArray n x =+ check "newSmallArray: negative size" (n>=0)+ $ check ("newSmallArray: requested " ++ show n ++ " elements") (n * ptrSz < 1024*1024*1024)+ $ A.newSmallArray n x+ where+ ptrSz = sizeOf (undefined :: Int) readSmallArray :: (HasCallStack, PrimMonad m) => SmallMutableArray (PrimState m) a -> Int -> m a readSmallArray marr i = do@@ -100,6 +108,8 @@ errorUnsafeFreeze = error "Data.Primitive.Array.Checked.unsafeFreeze:\nAttempted to read from an array after unsafely freezing it." +-- | This installs error thunks in the argument array so that+-- any attempt to use it after an unsafeFreeze will fail. unsafeFreezeSmallArray :: (HasCallStack, PrimMonad m) => SmallMutableArray (PrimState m) a -> m (SmallArray a)
− src/Data/Primitive/UnliftedArray.hs
@@ -1,162 +0,0 @@-{-# LANGUAGE PackageImports #-}--module Data.Primitive.UnliftedArray- ( UnliftedArray(..)- , MutableUnliftedArray(..)- , PrimUnlifted(..)- , unsafeNewUnliftedArray- , newUnliftedArray- , A.setUnliftedArray- , A.sizeofUnliftedArray- , A.sizeofMutableUnliftedArray- , readUnliftedArray- , writeUnliftedArray- , indexUnliftedArray- , indexUnliftedArrayM- , A.unsafeFreezeUnliftedArray- , freezeUnliftedArray- , thawUnliftedArray- , A.sameMutableUnliftedArray- , copyUnliftedArray- , copyMutableUnliftedArray- , cloneUnliftedArray- , cloneMutableUnliftedArray- , A.mapUnliftedArray- , A.foldrUnliftedArray- , A.foldlUnliftedArray- , A.foldrUnliftedArray'- , A.foldlUnliftedArray'- , A.unliftedArrayFromList- ) where--import Control.Monad.Primitive (PrimMonad,PrimState)-import Control.Exception (throw, ArrayException(..))-import "primitive" Data.Primitive.UnliftedArray (UnliftedArray,MutableUnliftedArray,PrimUnlifted)-import qualified "primitive" Data.Primitive.UnliftedArray as A-import qualified Data.List as L-import GHC.Stack--check :: HasCallStack => String -> Bool -> a -> a-check _ True x = x-check errMsg False _ = throw (IndexOutOfBounds $ "Data.Primitive.UnliftedArray.Checked." ++ errMsg ++ "\n" ++ prettyCallStack callStack)--newUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => Int -> a -> m (MutableUnliftedArray (PrimState m) a)-newUnliftedArray n x = check "newUnliftedArray: negative size" (n>=0) (A.newUnliftedArray n x)--unsafeNewUnliftedArray :: (HasCallStack, PrimMonad m) => Int -> m (MutableUnliftedArray (PrimState m) a)-unsafeNewUnliftedArray n = check "unsafeNewUnliftedArray: negative size" (n>=0) (A.unsafeNewUnliftedArray n)--readUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> Int -> m a-readUnliftedArray marr i = do- let siz = A.sizeofMutableUnliftedArray marr- explain = L.concat- [ "[size: "- , show siz- , ", index: "- , show i- , "]"- ]- check ("readUnliftedArray: index of out bounds " ++ explain) (i>=0 && i<siz) (A.readUnliftedArray marr i)--writeUnliftedArray :: (HasCallStack, PrimMonad m, PrimUnlifted a) => MutableUnliftedArray (PrimState m) a -> Int -> a -> m ()-writeUnliftedArray marr i x = do- let siz = A.sizeofMutableUnliftedArray marr- explain = L.concat- [ "[size: "- , show siz- , ", index: "- , show i- , "]"- ]- check ("writeUnliftedArray: index of out bounds " ++ explain) (i>=0 && i<siz) (A.writeUnliftedArray marr i x)--indexUnliftedArray :: (HasCallStack, PrimUnlifted a) => UnliftedArray a -> Int -> a-indexUnliftedArray arr i =- let sz = A.sizeofUnliftedArray arr- explain = L.concat- [ "[size: "- , show sz- , ", index: "- , show i- , "]"- ]- in check ("indexUnliftedArray: index of out bounds " ++ explain)- (i>=0 && i<sz)- (A.indexUnliftedArray arr i)--indexUnliftedArrayM :: (HasCallStack, Monad m, PrimUnlifted a) => UnliftedArray a -> Int -> m a-indexUnliftedArrayM arr i = check "indexUnliftedArrayM: index of out bounds"- (i>=0 && i<A.sizeofUnliftedArray arr)- (A.indexUnliftedArrayM arr i)--freezeUnliftedArray- :: (HasCallStack, PrimMonad m)- => MutableUnliftedArray (PrimState m) a -- ^ source- -> Int -- ^ offset- -> Int -- ^ length- -> m (UnliftedArray a)-freezeUnliftedArray marr s l = do- let siz = A.sizeofMutableUnliftedArray marr- check "freezeUnliftedArray: index range of out bounds"- (s>=0 && l>=0 && (s+l)<=siz)- (A.freezeUnliftedArray marr s l)--thawUnliftedArray- :: (HasCallStack, PrimMonad m)- => UnliftedArray a -- ^ source- -> Int -- ^ offset- -> Int -- ^ length- -> m (MutableUnliftedArray (PrimState m) a)-thawUnliftedArray arr s l = check "thawArr: index range of out bounds"- (s>=0 && l>=0 && (s+l)<=A.sizeofUnliftedArray arr)- (A.thawUnliftedArray arr s l)--copyUnliftedArray :: (HasCallStack, PrimMonad m)- => MutableUnliftedArray (PrimState m) a -- ^ destination array- -> Int -- ^ offset into destination array- -> UnliftedArray a -- ^ source array- -> Int -- ^ offset into source array- -> Int -- ^ number of elements to copy- -> m ()-copyUnliftedArray marr s1 arr s2 l = do- let siz = A.sizeofMutableUnliftedArray marr- check "copyUnliftedArray: index range of out bounds"- (s1>=0 && s2>=0 && l>=0 && (s2+l)<=A.sizeofUnliftedArray arr && (s1+l)<=siz)- (A.copyUnliftedArray marr s1 arr s2 l)---copyMutableUnliftedArray :: (HasCallStack, PrimMonad m)- => MutableUnliftedArray (PrimState m) a -- ^ destination array- -> Int -- ^ offset into destination array- -> MutableUnliftedArray (PrimState m) a -- ^ source array- -> Int -- ^ offset into source array- -> Int -- ^ number of elements to copy- -> m ()-copyMutableUnliftedArray marr1 s1 marr2 s2 l = do- let siz1 = A.sizeofMutableUnliftedArray marr1- let siz2 = A.sizeofMutableUnliftedArray marr2- check "copyMutableUnliftedArray: index range of out bounds"- (s1>=0 && s2>=0 && l>=0 && (s2+l)<=siz2 && (s1+l)<=siz1)- (A.copyMutableUnliftedArray marr1 s1 marr2 s2 l)---cloneUnliftedArray :: HasCallStack- => UnliftedArray a -- ^ source array- -> Int -- ^ offset into destination array- -> Int -- ^ number of elements to copy- -> UnliftedArray a-cloneUnliftedArray arr s l = check "cloneUnliftedArray: index range of out bounds"- (s>=0 && l>=0 && (s+l)<=A.sizeofUnliftedArray arr)- (A.cloneUnliftedArray arr s l)--cloneMutableUnliftedArray :: (HasCallStack, PrimMonad m)- => MutableUnliftedArray (PrimState m) a -- ^ source array- -> Int -- ^ offset into destination array- -> Int -- ^ number of elements to copy- -> m (MutableUnliftedArray (PrimState m) a)-cloneMutableUnliftedArray marr s l = do- let siz = A.sizeofMutableUnliftedArray marr- check "cloneMutableUnliftedArray: index range of out bounds"- (s>=0 && l>=0 && (s+l)<=siz)- (A.cloneMutableUnliftedArray marr s l)-