packages feed

streamly-bytestring 0.1.2 → 0.1.3

raw patch · 6 files changed

+46/−43 lines, 6 filesdep ~bytestringdep ~streamlynew-uploader

Dependency ranges changed: bytestring, streamly

Files

Changelog.md view
@@ -1,5 +1,9 @@ # Changelog for streamly-bytestring +## 0.1.3 (Jul 2021)++* Support streamly-0.8.0+ ## 0.1.2  * Fix memory bug in Strict.toArray
README.md view
@@ -2,14 +2,15 @@  Library for streamly and bytestring interoperation. -If you are writing code from scratch, please use `Streamly.Memory.Array` which-is a generalization of `ByteString` and better integrated with streamly.+If you are writing code from scratch, please use `Streamly.Data.Array.Foreign`+which is a generalization of `ByteString` and better integrated with streamly.  This library is to enable interoperation of streamly with existing code that uses `ByteString`.  The package provides APIs to interconvert between strict `Bytestring` and-streamly `Array Word8` and between lazy `Bytestring` and stream of `Array Word8`.+streamly `Array Word8` and between lazy `Bytestring` and stream of `Array+Word8`.  The interconversion in the case of strict `Bytestring` and streamly `Array Word8` has no overhead as the underlying representation of ByteString and Array@@ -20,7 +21,6 @@ This is a dumb program that counts the number of bytes in a file.  ```-import Streamly import qualified Streamly.Prelude as S  import qualified Data.ByteString as BS
src/Streamly/External/ByteString.hs view
@@ -12,7 +12,6 @@ where  import Control.Monad.IO.Class (MonadIO)-import Data.ByteString.Internal (ByteString(..)) import Data.Word (Word8) #if MIN_VERSION_base(4, 10, 0) import Foreign.ForeignPtr (plusForeignPtr)@@ -21,13 +20,15 @@ #endif import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr) import GHC.Ptr (minusPtr, plusPtr)-import Streamly.Internal.Memory.Array.Types (Array(..))-import Streamly.Internal.Data.Unfold.Types (Unfold(..))-import Streamly.Internal.Data.Unfold (lmap)-import Streamly.Internal.Data.Fold.Types (Fold(..))+import Streamly.Data.Unfold (Unfold, lmap)+import Streamly.Data.Fold (Fold) -import qualified Streamly.Internal.Memory.Array as A+-- Internal imports+import Data.ByteString.Internal (ByteString(..))+import Streamly.Internal.Data.Array.Foreign.Type (Array(..)) +import qualified Streamly.Data.Array.Foreign as A+ import Prelude hiding (read)  -- | Convert a 'ByteString' to an array of 'Word8'. This function unwraps the@@ -35,7 +36,7 @@ -- is performed in constant time. {-# INLINE toArray #-} toArray :: ByteString -> Array Word8-toArray (PS fp off len) = Array nfp endPtr endPtr+toArray (PS fp off len) = Array nfp endPtr   where     nfp = fp `plusForeignPtr` off     endPtr = unsafeForeignPtrToPtr nfp `plusPtr` len@@ -60,7 +61,7 @@ -- | Fold a stream of Word8 to a strict ByteString of given size in bytes. {-# INLINE writeN #-} writeN :: MonadIO m => Int -> Fold m Word8 ByteString-writeN i = fromArray <$> (A.writeN i)+writeN i = fromArray <$> A.writeN i  -- | Fold a stream of Word8 to a strict ByteString of appropriate size. {-# INLINE write #-}
src/Streamly/External/ByteString/Lazy.hs view
@@ -10,22 +10,19 @@   ) where -import Data.ByteString.Lazy.Internal (ByteString(..)) import Data.Word (Word8)-import Streamly.Internal.Memory.Array.Types (Array(..))-import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))-import Streamly.Internal.Data.Unfold (concat)-import Streamly.Internal.Data.Unfold.Types (Unfold(..))--import qualified Streamly.External.ByteString as Strict-import qualified Streamly.Internal.Memory.Array as A-+import Streamly.Data.Unfold (many)+import Streamly.Data.Array.Foreign (Array) import System.IO.Unsafe (unsafeInterleaveIO) -import Streamly-import qualified Streamly.Internal.Prelude as S+-- Internal imports+import Data.ByteString.Lazy.Internal (ByteString(..), chunk)+import Streamly.Internal.Data.Stream.StreamD.Type (Step(..))+import Streamly.Internal.Data.Unfold.Type (Unfold(..)) -import qualified Data.ByteString.Lazy.Internal as BSLI+import qualified Streamly.External.ByteString as Strict+import qualified Streamly.Data.Array.Foreign as A+import qualified Streamly.Prelude as S  import Prelude hiding (concat, read) @@ -36,16 +33,16 @@   where     seed = return     step (Chunk bs bl) = return $ Yield (Strict.toArray bs) bl-    step Empty = return $ Stop+    step Empty = return Stop  -- | Unfold a lazy ByteString to a stream of Word8 {-# INLINE read #-} read :: Monad m => Unfold m ByteString Word8-read = concat readChunks A.read+read = many readChunks A.read  -- | Convert a lazy 'ByteString' to a serial stream of 'Array' 'Word8'. {-# INLINE toChunks #-}-toChunks :: Monad m => ByteString -> SerialT m (Array Word8)+toChunks :: Monad m => ByteString -> S.SerialT m (Array Word8) toChunks = S.unfold readChunks  {-@@ -89,17 +86,17 @@ -- fromChunksIO str = runLazy (fromChunks (S.hoist liftToLazy str)) -- @ {-# INLINE fromChunks #-}-fromChunks :: Monad m => SerialT m (Array Word8) -> m ByteString-fromChunks = S.foldr BSLI.chunk Empty . S.map Strict.fromArray+fromChunks :: Monad m => S.SerialT m (Array Word8) -> m ByteString+fromChunks = S.foldr chunk Empty . S.map Strict.fromArray  -- | Convert a serial stream of 'Array' 'Word8' to a lazy 'ByteString' in the -- /IO/ monad. {-# INLINE fromChunksIO #-}-fromChunksIO :: SerialT IO (Array Word8) -> IO ByteString+fromChunksIO :: S.SerialT IO (Array Word8) -> IO ByteString fromChunksIO = -- Although the /IO/ monad is strict in nature we emulate laziness using -- 'unsafeInterleaveIO'.     S.foldrM-        (\x b -> unsafeInterleaveIO b >>= pure . BSLI.chunk x)-        (pure BSLI.Empty) .+        (\x b -> unsafeInterleaveIO b >>= pure . chunk x)+        (pure Empty) .     S.map Strict.fromArray
streamly-bytestring.cabal view
@@ -1,13 +1,13 @@ cabal-version: 1.12 --- This file has been generated from package.yaml by hpack version 0.31.2.+-- This file has been generated from package.yaml by hpack version 0.33.1. -- -- see: https://github.com/sol/hpack ----- hash: af662f24c97fa6b630e84ef660885ad0de001dc9face27ef4698f5361e4e5484+-- hash: ec5d4099c0c82b1487393616056b1f4f77ac9bd91e3764f38fd227abf5fe6dfd  name:           streamly-bytestring-version:        0.1.2+version:        0.1.3 synopsis:       Library for streamly and bytestring interoperation. description:    Please see the README on GitHub at <https://github.com/psibi/streamly-bytestring#readme> category:       Streamly, Stream, ByteString@@ -38,8 +38,8 @@   ghc-options: -Wall -O2   build-depends:       base >=4.7 && <5-    , bytestring >=0.10.0 && <0.11-    , streamly >=0.7.0 && <0.8+    , bytestring >=0.10.0 && <=0.11.1.0+    , streamly >=0.8.0 && <=0.8.0   if impl(ghc < 8.1)     build-depends:         base-compat >=0.11
test/Main.hs view
@@ -8,8 +8,6 @@ import GHC.IO.Handle (Handle) import GHC.Ptr (minusPtr) import System.Random (randomIO)-import Streamly-import Streamly.Internal.Memory.Array.Types (Array(..)) import Streamly.FileSystem.Handle (readChunks) import System.IO (openFile, IOMode(ReadMode)) import System.IO.Temp (withSystemTempFile)@@ -17,13 +15,16 @@ import Test.Hspec.QuickCheck import Test.QuickCheck.Instances.ByteString () +-- Internal imports+import Streamly.Internal.Data.Array.Foreign.Type (Array(..))+ import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BSL import qualified Streamly.External.ByteString as Strict import qualified Streamly.External.ByteString.Lazy as Lazy import qualified Streamly.Prelude as S -streamToByteString :: MonadAsync m => SerialT m (Array Word8) -> m ByteString+streamToByteString :: S.MonadAsync m => S.SerialT m (Array Word8) -> m ByteString streamToByteString stream = S.foldl' (<>) mempty $ S.map Strict.fromArray stream  checkFileContent :: FilePath -> Handle -> IO ()@@ -75,14 +76,14 @@  propFromChunks :: Spec propFromChunks =-    prop ("Lazy.fromChunks = BSL.fromChunks") $ \aL -> do+    prop "Lazy.fromChunks = BSL.fromChunks" $ \aL -> do         x1 <- Lazy.fromChunks $ S.map Strict.toArray $ S.fromList aL         let x2 = BSL.fromChunks aL         x1 `shouldBe` x2  propFromChunksIO :: Spec propFromChunksIO =-    prop ("Lazy.fromChunks = BSL.fromChunks") $ \aL -> do+    prop "Lazy.fromChunks = BSL.fromChunks" $ \aL -> do         x1 <- Lazy.fromChunksIO $ S.map Strict.toArray $ S.fromList aL         let x2 = BSL.fromChunks aL         x1 `shouldBe` x2@@ -110,7 +111,7 @@                 in bs' `shouldBe` Strict.fromArray (Strict.toArray bs')             prop "toArray never produces negative length" $ \bs ->                 -- 'BS.drop 5' to trigger non-zero offset-                let (Array nfp endPtr _) = Strict.toArray (BS.drop 5 bs)+                let (Array nfp endPtr) = Strict.toArray (BS.drop 5 bs)                 in (endPtr `minusPtr` unsafeForeignPtrToPtr nfp) >= 0 `shouldBe` True             prop "Lazy Identity" $ \bs -> do                 bs2 <- Lazy.fromChunks . Lazy.toChunks $ bs