packages feed

little-rio 0.1.1 → 0.2.0

raw patch · 3 files changed

+46/−13 lines, 3 filesdep +primitivedep +resourcetdep ~exceptionsdep ~microlensdep ~microlens-mtl

Dependencies added: primitive, resourcet

Dependency ranges changed: exceptions, microlens, microlens-mtl, mtl, unliftio-core

Files

README.md view
@@ -2,6 +2,6 @@  [![CircleCI](https://circleci.com/gh/ejconlon/little-rio/tree/master.svg?style=svg)](https://circleci.com/gh/ejconlon/little-rio/tree/master) -When you need just the `RIO` monad (but you want a `MonadCatch` instance)+When you need just the `RIO` monad (but you want a `MonadCatch` instance and some other orphans) -This is derived from the RIO library ([LICENSE](https://hackage.haskell.org/package/rio-0.1.14.0/src/LICENSE)).+This is derived from the `rio` library ([LICENSE](https://hackage.haskell.org/package/rio-0.1.18.0/src/LICENSE)) and the `rio-orphans` library ([LICENSE](https://hackage.haskell.org/package/rio-orphans-0.1.1.0/src/LICENSE))
little-rio.cabal view
@@ -4,10 +4,10 @@ -- -- see: https://github.com/sol/hpack ----- hash: 164d0fdd886b3210c590587e48d1ccd93c6aa132cf9f4e0ff385825b9c30392b+-- hash: d22342ec62f05927c43b7ecfdacef567fa519fafdf63e0545b49683ac1a14856  name:           little-rio-version:        0.1.1+version:        0.2.0 synopsis:       When you need just the RIO monad description:    Please see the README on GitHub at <https://github.com/ejconlon/little-rio#readme> category:       Control@@ -36,9 +36,11 @@   ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints -fno-warn-unused-top-binds   build-depends:       base >=4.12 && <5-    , exceptions >=0.10-    , microlens >=0.4-    , microlens-mtl >=0.2-    , mtl >=2.2-    , unliftio-core >=0.1+    , exceptions >=0.10 && <1+    , microlens >=0.4 && <1+    , microlens-mtl >=0.2 && <1+    , mtl >=2.2 && <3+    , primitive >=0.7.0.1 && <1+    , resourcet >=1.2.4.2 && <2+    , unliftio-core >=0.1 && <1   default-language: Haskell2010
src/LittleRIO.hs view
@@ -1,16 +1,20 @@ {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE FunctionalDependencies #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-}+{-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UndecidableInstances #-}  {- |-Most definitions follow the RIO lib: https://hackage.haskell.org/package/rio-0.1.14.0/docs/RIO.html+Most definitions follow the RIO lib: https://hackage.haskell.org/package/rio-0.1.18.0/docs/RIO.html+The rest follow from orphans: https://hackage.haskell.org/package/rio-orphans-0.1.1.0/docs/src/RIO.Orphans.html See LICENSE info in the README. -} module LittleRIO-  ( HasStateRef (..)+  ( HasResourceMap (..)+  , HasStateRef (..)   , HasWriteRef (..)   , SomeRef (..)+  , ResourceMap   , RIO (..)   , getStateRef   , liftRIO@@ -23,17 +27,22 @@   , putStateRef   , runRIO   , readSomeRef+  , resourceRIO   , tellWriteRef   , unliftRIO+  , withResourceMap   , writeSomeRef   ) where  import Control.Applicative (liftA2) import Control.Monad.Catch (MonadCatch, MonadMask, MonadThrow) import Control.Monad.IO.Class (MonadIO (..))-import Control.Monad.IO.Unlift (MonadUnliftIO, UnliftIO, askUnliftIO)+import Control.Monad.IO.Unlift (MonadUnliftIO (..), UnliftIO, askUnliftIO)+import Control.Monad.Primitive (PrimMonad (..)) import Control.Monad.Reader (MonadReader (..), ReaderT (..)) import Control.Monad.State (MonadState (..))+import Control.Monad.Trans.Resource (runResourceT)+import Control.Monad.Trans.Resource.Internal (MonadResource (..), ReleaseMap, ResourceT (..)) import Control.Monad.Writer (MonadWriter (..)) import Data.IORef (IORef, newIORef, readIORef, writeIORef) import Lens.Micro (Lens')@@ -50,6 +59,10 @@   mempty = pure mempty   mappend = (<>) +instance PrimMonad (RIO env) where+  type PrimState (RIO env) = PrimState IO+  primitive = RIO . ReaderT . const . primitive+ mapRIO :: (env -> env') -> RIO env' a -> RIO env a mapRIO f m = do   env <- ask@@ -76,7 +89,7 @@ writeSomeRef (SomeRef _ x) = liftIO . x  modifySomeRef :: MonadIO m => SomeRef a -> (a -> a) -> m ()-modifySomeRef (SomeRef read' write) f = liftIO (fmap f read' >>= write)+modifySomeRef (SomeRef read' write) f = liftIO (read' >>= write . f)  ioRefToSomeRef :: IORef a -> SomeRef a ioRefToSomeRef ref = SomeRef (readIORef ref) (writeIORef ref)@@ -142,3 +155,21 @@   tell = tellWriteRef   listen = listenWriteRef   pass = passWriteRef++type ResourceMap = IORef ReleaseMap++withResourceMap :: MonadUnliftIO m => (ResourceMap -> m a) -> m a+withResourceMap inner =+  withRunInIO (\run -> runResourceT (ResourceT (run . inner)))++class HasResourceMap env where+  resourceMapL :: Lens' env ResourceMap++instance HasResourceMap (IORef ReleaseMap) where+  resourceMapL = id++resourceRIO :: HasResourceMap env => ResourceT IO a -> RIO env a+resourceRIO (ResourceT f) = view resourceMapL >>= liftIO . f++instance HasResourceMap env => MonadResource (RIO env) where+  liftResourceT = resourceRIO