streamly-bytestring 0.1.3 → 0.1.4
raw patch · 5 files changed
+28/−29 lines, 5 filesdep ~bytestringdep ~streamly
Dependency ranges changed: bytestring, streamly
Files
- Changelog.md +4/−0
- src/Streamly/External/ByteString.hs +18/−13
- src/Streamly/External/ByteString/Lazy.hs +1/−3
- streamly-bytestring.cabal +3/−10
- test/Main.hs +2/−3
Changelog.md view
@@ -1,5 +1,9 @@ # Changelog for streamly-bytestring +## 0.1.4 (Dec 2021)++* Support streamly-0.8.1+ ## 0.1.3 (Jul 2021) * Support streamly-0.8.0
src/Streamly/External/ByteString.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE RecordWildCards #-}+{-# LANGUAGE MagicHash #-} module Streamly.External.ByteString ( toArray@@ -13,33 +14,38 @@ import Control.Monad.IO.Class (MonadIO) import Data.Word (Word8)-#if MIN_VERSION_base(4, 10, 0)-import Foreign.ForeignPtr (plusForeignPtr)-#else-import Foreign.ForeignPtr.Compat (plusForeignPtr)-#endif-import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)-import GHC.Ptr (minusPtr, plusPtr)+import GHC.ForeignPtr (ForeignPtr(..))+import GHC.Ptr (Ptr(..), minusPtr, nullPtr, plusPtr) import Streamly.Data.Unfold (Unfold, lmap) import Streamly.Data.Fold (Fold) -- Internal imports import Data.ByteString.Internal (ByteString(..)) import Streamly.Internal.Data.Array.Foreign.Type (Array(..))+import Streamly.Internal.Data.Array.Foreign.Mut.Type+ (ArrayContents, arrayToFptrContents, fptrToArrayContents, nilArrayContents) import qualified Streamly.Data.Array.Foreign as A import Prelude hiding (read) +-- | Helper function that creates a ForeignPtr+makeForeignPtr :: ArrayContents -> Ptr a -> ForeignPtr a+makeForeignPtr contents (Ptr addr#) =+ ForeignPtr addr# (arrayToFptrContents contents)+ -- | Convert a 'ByteString' to an array of 'Word8'. This function unwraps the -- 'ByteString' and wraps it with 'Array' constructors and hence the operation -- is performed in constant time. {-# INLINE toArray #-} toArray :: ByteString -> Array Word8-toArray (PS fp off len) = Array nfp endPtr+toArray (PS (ForeignPtr addr# _) _ _)+ | Ptr addr# == nullPtr = Array nilArrayContents nullPtr nullPtr+toArray (PS (ForeignPtr addr# fpcontents) off len) =+ Array (fptrToArrayContents fpcontents) startPtr endPtr where- nfp = fp `plusForeignPtr` off- endPtr = unsafeForeignPtrToPtr nfp `plusPtr` len+ startPtr = Ptr addr# `plusPtr` off+ endPtr = startPtr `plusPtr` len -- | Convert an array of 'Word8' to a 'ByteString'. This function unwraps the -- 'Array' and wraps it with 'ByteString' constructors and hence the operation@@ -48,10 +54,9 @@ fromArray :: Array Word8 -> ByteString fromArray Array {..} | aLen == 0 = mempty- | otherwise = PS aStart 0 aLen+ | otherwise = PS (makeForeignPtr arrContents arrStart) 0 aLen where- aStartPtr = unsafeForeignPtrToPtr aStart- aLen = aEnd `minusPtr` aStartPtr+ aLen = aEnd `minusPtr` arrStart -- | Unfold a strict ByteString to a stream of Word8. {-# INLINE read #-}
src/Streamly/External/ByteString/Lazy.hs view
@@ -1,5 +1,3 @@-{-# LANGUAGE GeneralizedNewtypeDeriving #-}- module Streamly.External.ByteString.Lazy ( readChunks , read@@ -97,6 +95,6 @@ -- Although the /IO/ monad is strict in nature we emulate laziness using -- 'unsafeInterleaveIO'. S.foldrM- (\x b -> unsafeInterleaveIO b >>= pure . chunk x)+ (\x b -> chunk x <$> unsafeInterleaveIO b) (pure Empty) . S.map Strict.fromArray
streamly-bytestring.cabal view
@@ -1,13 +1,6 @@-cabal-version: 1.12---- This file has been generated from package.yaml by hpack version 0.33.1.------ see: https://github.com/sol/hpack------ hash: ec5d4099c0c82b1487393616056b1f4f77ac9bd91e3764f38fd227abf5fe6dfd-+cabal-version: 1.12 name: streamly-bytestring-version: 0.1.3+version: 0.1.4 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@@ -39,7 +32,7 @@ build-depends: base >=4.7 && <5 , bytestring >=0.10.0 && <=0.11.1.0- , streamly >=0.8.0 && <=0.8.0+ , streamly >= 0.8.1 && < 0.8.2 if impl(ghc < 8.1) build-depends: base-compat >=0.11
test/Main.hs view
@@ -4,7 +4,6 @@ import Data.ByteString (ByteString) import Data.Word (Word8)-import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr) import GHC.IO.Handle (Handle) import GHC.Ptr (minusPtr) import System.Random (randomIO)@@ -111,8 +110,8 @@ 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)- in (endPtr `minusPtr` unsafeForeignPtrToPtr nfp) >= 0 `shouldBe` True+ let (Array _ sPtr ePtr) = Strict.toArray (BS.drop 5 bs)+ in (ePtr `minusPtr` sPtr) >= 0 `shouldBe` True prop "Lazy Identity" $ \bs -> do bs2 <- Lazy.fromChunks . Lazy.toChunks $ bs bs `shouldBe` bs2