summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIvanMiljenovic <>2018-02-06 05:37:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2018-02-06 05:37:00 (GMT)
commit027e7f91811c5a4e44069eb4e6c7dea9f2001b21 (patch)
treeb965f3af5747832d7aea6b8c38cb3035a291612f
parentd4b6b312423baafd700a40968c3080c6462c0124 (diff)
version 0.2.1.00.2.1.0
-rw-r--r--ChangeLog.md7
-rw-r--r--README.md2
-rw-r--r--src/Streaming/With/Lifted.hs66
-rw-r--r--streaming-with.cabal4
4 files changed, 72 insertions, 7 deletions
diff --git a/ChangeLog.md b/ChangeLog.md
index 5b27302..5673ee1 100644
--- a/ChangeLog.md
+++ b/ChangeLog.md
@@ -1,5 +1,12 @@
# Revision history for streaming-with
+## 0.2.1.0 -- 2018-02-06
+
+* Add the `RunWithable` class.
+
+* Add the `within`, `liftActionIO` and `liftThrow` functions for use
+ with `Withable`.
+
## 0.2.0.0 -- 2017-07-07
* Add `Monad w` constraint to `Withable w`
diff --git a/README.md b/README.md
index 0401c6b..7dd235c 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,7 @@
streaming-with
==============
-[![Hackage](https://img.shields.io/hackage/v/streaming-with.svg)](https://hackage.haskell.org/package/streaming-with) [![Build Status](https://travis-ci.org/ivan-m/streaming-with.svg)](https://travis-ci.org/ivan-m/streaming-with)
+[![Hackage](https://img.shields.io/hackage/v/streaming-with.svg)](https://hackage.haskell.org/package/streaming-with) [![Build Status](https://travis-ci.org/haskell-streaming/streaming-with.svg)](https://travis-ci.org/haskell-streaming/streaming-with)
> with/bracket-style idioms for use with [streaming]
diff --git a/src/Streaming/With/Lifted.hs b/src/Streaming/With/Lifted.hs
index 2d073e1..b703dae 100644
--- a/src/Streaming/With/Lifted.hs
+++ b/src/Streaming/With/Lifted.hs
@@ -1,4 +1,4 @@
-{-# LANGUAGE FlexibleContexts, RankNTypes, TypeFamilies #-}
+{-# LANGUAGE FlexibleContexts, FlexibleInstances, RankNTypes, TypeFamilies #-}
{- |
Module : Streaming.With.Lifted
@@ -37,6 +37,9 @@
-}
module Streaming.With.Lifted
( Withable (..)
+ , RunWithable (..)
+ , within
+ , liftActionIO
-- * File-handling
, withFile
, withBinaryFile
@@ -58,11 +61,12 @@ module Streaming.With.Lifted
import Data.ByteString.Streaming (ByteString)
import qualified Streaming.With as W
-import Control.Monad.Catch (MonadMask, bracket)
+import Control.Exception (Exception)
+import Control.Monad.Catch (MonadMask, bracket, throwM)
import Control.Monad.IO.Class (MonadIO, liftIO)
-import Control.Monad.Managed (Managed, managed)
+import Control.Monad.Managed (Managed, managed, runManaged)
import Control.Monad.Trans.Class (lift)
-import Control.Monad.Trans.Cont (ContT(..))
+import Control.Monad.Trans.Cont (ContT(..), runContT)
import System.IO (Handle, IOMode)
--------------------------------------------------------------------------------
@@ -99,6 +103,60 @@ instance (MonadMask m, MonadIO m) => Withable (ContT r m) where
liftAction = lift
+-- | Safely run the provided continuation.
+--
+-- A result of type '()' is required to ensure no resources are
+-- leaked.
+--
+-- Note that you cannot write something like:
+--
+-- > copyBoth :: FilePath -> FilePath -> FilePath -> IO ()
+-- > copyBoth inF1 inF2 outF = runWith $ do
+-- > bs1 <- withBinaryFileContents inF1
+-- > bs2 <- withBinaryFileContents inF2
+-- > writeBinaryFile outF bs1
+-- > appendBinaryFile outF bs2
+--
+-- as the 'RunWithable' instance cannot be inferred. As such, you
+-- will need to specify a type somewhere.
+--
+-- @since 0.2.1.0
+class (Withable w) => RunWithable w where
+ runWith :: w () -> WithMonad w ()
+
+instance RunWithable Managed where
+ runWith = runManaged
+
+instance (MonadMask m, MonadIO m) => RunWithable (ContT () m) where
+ runWith = flip runContT return
+
+-- | A helper function to run a computation within a lifted resource
+-- management expression.
+--
+-- @within w f = w >>= liftAction . f@
+--
+-- @since 0.2.1.0
+within :: (Withable w) => w a -> (a -> WithMonad w b) -> w b
+within w f = w >>= liftAction . f
+
+-- | A helper function for the common case of lifting an @IO@
+-- computation into a @Withable@.
+--
+-- @liftActionIO = liftAction . liftIO@.
+--
+-- @since 0.2.1.0
+liftActionIO :: (Withable w) => IO a -> w a
+liftActionIO = liftAction . liftIO
+
+-- | A helper function for the common case of throwing an exception in
+-- the underlying monad.
+--
+-- @liftThrow = liftAction . throwM@.
+--
+-- @since 0.2.1.0
+liftThrow :: (Withable w, Exception e) => e -> w a
+liftThrow = liftAction . throwM
+
--------------------------------------------------------------------------------
-- | A lifted variant of 'System.IO.withFile'.
diff --git a/streaming-with.cabal b/streaming-with.cabal
index 0149180..6091b13 100644
--- a/streaming-with.cabal
+++ b/streaming-with.cabal
@@ -1,5 +1,5 @@
name: streaming-with
-version: 0.2.0.0
+version: 0.2.1.0
synopsis: with/bracket-style idioms for use with streaming
description:
This package provides the foundations for a continuation-based
@@ -17,7 +17,7 @@ tested-with: GHC == 7.10.2, GHC == 8.0.2, GHC == 8.1.*
source-repository head
type: git
- location: https://github.com/ivan-m/streaming-with.git
+ location: https://github.com/haskell-streaming/streaming-with.git
library
exposed-modules: Streaming.With