From 7d0ca1c23012dfcd5a4a81245da327c159dd225f Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Sun, 26 Jan 2020 14:27:38 +0100 Subject: [PATCH] Use streamly-bytestring --- hpath-io/hpath-io.cabal | 5 +-- hpath-io/src/HPath/IO.hs | 8 ++-- hpath-io/src/Streamly/ByteString.hs | 57 ------------------------ hpath-io/src/Streamly/ByteString/Lazy.hs | 45 ------------------- 4 files changed, 5 insertions(+), 110 deletions(-) delete mode 100644 hpath-io/src/Streamly/ByteString.hs delete mode 100644 hpath-io/src/Streamly/ByteString/Lazy.hs diff --git a/hpath-io/hpath-io.cabal b/hpath-io/hpath-io.cabal index f1ce5ee..e8c3ecf 100644 --- a/hpath-io/hpath-io.cabal +++ b/hpath-io/hpath-io.cabal @@ -31,10 +31,6 @@ library System.Posix.Directory.Traversals, System.Posix.FD c-sources: cbits/dirutils.c - - other-modules: Streamly.ByteString - Streamly.ByteString.Lazy - -- other-extensions: build-depends: base >= 4.8 && <5 , IfElse , bytestring >= 0.10.0.0 @@ -43,6 +39,7 @@ library , hpath-filepath >= 0.10.2 && < 0.11 , safe-exceptions >= 0.1 , streamly >= 0.7 + , streamly-bytestring >= 0.1 , time >= 1.8 , unix >= 2.5 , unix-bytestring diff --git a/hpath-io/src/HPath/IO.hs b/hpath-io/src/HPath/IO.hs index 54f1c94..e1fbfb2 100644 --- a/hpath-io/src/HPath/IO.hs +++ b/hpath-io/src/HPath/IO.hs @@ -200,8 +200,8 @@ import HPath import HPath.IO.Errors import Prelude hiding (appendFile, readFile, writeFile) import Streamly -import Streamly.ByteString -import qualified Streamly.ByteString.Lazy as SL +import Streamly.External.ByteString +import qualified Streamly.External.ByteString.Lazy as SL import qualified Streamly.Data.Fold as FL import Streamly.Memory.Array import qualified Streamly.FileSystem.Handle as FH @@ -942,7 +942,7 @@ readFileStream :: Path b readFileStream (Path fp) = do fd <- openFd fp SPI.ReadOnly [] Nothing handle <- SPI.fdToHandle fd - let stream = (S.unfold (SU.finally SIO.hClose FH.readChunks) handle) >>= arrayToByteString + let stream = fmap fromArray (S.unfold (SU.finally SIO.hClose FH.readChunks) handle) pure stream @@ -989,7 +989,7 @@ writeFileL (Path fp) fmode lbs = do handle <- bracketOnError (openFd fp SPI.WriteOnly [SPDF.oTrunc] fmode) (SPI.closeFd) $ SPI.fdToHandle finally (streamlyCopy handle) (SIO.hClose handle) where - streamlyCopy tH = S.fold (FH.writeChunks tH) $ SL.fromByteString lbs + streamlyCopy tH = S.fold (FH.writeChunks tH) $ SL.toChunks lbs -- |Append a given ByteString to a file. diff --git a/hpath-io/src/Streamly/ByteString.hs b/hpath-io/src/Streamly/ByteString.hs deleted file mode 100644 index 2cee0a1..0000000 --- a/hpath-io/src/Streamly/ByteString.hs +++ /dev/null @@ -1,57 +0,0 @@ -{-# LANGUAGE RankNTypes #-} -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE RecordWildCards #-} -{-# LANGUAGE ScopedTypeVariables #-} - -module Streamly.ByteString where - -import Control.Monad.IO.Class -import Data.ByteString hiding (length) -import qualified Data.ByteString as BS -import Data.ByteString.Unsafe -import Data.Word (Word8) -import Foreign.ForeignPtr -import Foreign.ForeignPtr.Unsafe -import Foreign.Ptr (castPtr, minusPtr, plusPtr) -import Prelude hiding (length) -import Streamly -import Streamly.Internal.Memory.Array.Types -import Streamly.Memory.Array -import qualified Streamly.Prelude as S - -toByteString :: - forall m. (MonadIO m, MonadAsync m) - => SerialT m (Array Word8) - -> m ByteString -toByteString stream = - let xs = S.mapM arrayToByteString stream - ys = S.foldlM' (\a b -> pure $ a <> b) mempty xs - in ys - -arrayToByteString :: (MonadIO m) => Array Word8 -> m ByteString -arrayToByteString arr - | length arr == 0 = return mempty -arrayToByteString Array {..} = - liftIO $ - withForeignPtr aStart $ \ptr -> - unsafePackCStringFinalizer ptr aLen (return ()) - where - aLen = - let p = unsafeForeignPtrToPtr aStart - in aEnd `minusPtr` p - -byteStringToArray :: (MonadIO m) => ByteString -> m (Array Word8) -byteStringToArray bs = - liftIO $ - unsafeUseAsCStringLen - bs - (\(ptr, _) -> do - let endPtr pr = (castPtr pr `plusPtr` (BS.length bs)) - fptr <- newForeignPtr_ (castPtr ptr) - return $ Array {aStart = fptr, aEnd = endPtr ptr, aBound = endPtr ptr}) - -fromByteString :: - forall m. (MonadIO m) - => ByteString - -> m (Array Word8) -fromByteString bs = byteStringToArray bs diff --git a/hpath-io/src/Streamly/ByteString/Lazy.hs b/hpath-io/src/Streamly/ByteString/Lazy.hs deleted file mode 100644 index 5208dda..0000000 --- a/hpath-io/src/Streamly/ByteString/Lazy.hs +++ /dev/null @@ -1,45 +0,0 @@ -{-# LANGUAGE RankNTypes #-} -{-# LANGUAGE RecordWildCards #-} -{-# LANGUAGE FlexibleContexts #-} -{-# LANGUAGE ScopedTypeVariables #-} - -module Streamly.ByteString.Lazy where - -import Control.Monad.IO.Class -import qualified Data.ByteString as BS -import Data.ByteString.Lazy (ByteString, foldlChunks, fromChunks) -import Data.ByteString.Unsafe -import Data.Word (Word8) -import Foreign.ForeignPtr -import Foreign.ForeignPtr.Unsafe -import Foreign.Ptr (castPtr, minusPtr, plusPtr) -import Prelude hiding (length) -import Streamly -import Streamly.ByteString (arrayToByteString, byteStringToArray) -import Streamly.Memory.Array -import qualified Streamly.Prelude as S - -toByteString :: - forall m. (MonadIO m, MonadAsync m) - => SerialT m (Array Word8) - -> m ByteString -toByteString stream = do - ys :: [BS.ByteString] <- S.toList $ S.mapM arrayToByteString stream - pure $ fromChunks ys - -stepFunction :: - forall m. (MonadIO m) - => SerialT m (Array Word8) - -> BS.ByteString - -> SerialT m (Array Word8) -stepFunction stream1 bs = do - arr <- liftIO $ byteStringToArray bs - let stream2 = pure arr - stream1 <> stream2 - -fromByteString :: - forall m. (MonadIO m) - => ByteString - -> SerialT m (Array Word8) -fromByteString bs = foldlChunks stepFunction mempty bs -