rio-orphans-0.1.2.0/src/0000755000000000000000000000000014032141241013056 5ustar0000000000000000rio-orphans-0.1.2.0/src/RIO/0000755000000000000000000000000014032141321013506 5ustar0000000000000000rio-orphans-0.1.2.0/test/0000755000000000000000000000000014032141241013246 5ustar0000000000000000rio-orphans-0.1.2.0/test/RIO/0000755000000000000000000000000014032141241013677 5ustar0000000000000000rio-orphans-0.1.2.0/src/RIO/Orphans.hs0000644000000000000000000000670014032141321015457 0ustar0000000000000000{-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE ImplicitParams #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE NoImplicitPrelude #-} {-# LANGUAGE StandaloneDeriving #-} {-# LANGUAGE TypeFamilies #-} {-# OPTIONS_GHC -fno-warn-orphans #-} -- | Orphan instances for the 'RIO' data type. module RIO.Orphans ( HasResourceMap (..) , ResourceMap , withResourceMap ) where import RIO import Control.Monad.Catch (MonadCatch, MonadMask) import Control.Monad.Base (MonadBase) import Control.Monad.IO.Unlift (askRunInIO) import Control.Monad.Trans.Resource.Internal (MonadResource (..), ReleaseMap, ResourceT (..)) import Control.Monad.Trans.Resource (runResourceT) import Control.Monad.Trans.Control (MonadBaseControl (..)) import qualified Control.Monad.Logger as LegacyLogger import Control.Monad.Logger (MonadLogger (..), MonadLoggerIO (..), LogStr) import System.Log.FastLogger (fromLogStr) import qualified GHC.Stack as GS -- | @since 0.1.0.0 deriving instance MonadCatch (RIO env) -- | @since 0.1.0.0 deriving instance MonadMask (RIO env) -- | @since 0.1.0.0 deriving instance MonadBase IO (RIO env) -- | @since 0.1.0.0 instance MonadBaseControl IO (RIO env) where type StM (RIO env) a = a liftBaseWith = withRunInIO restoreM = return -- | @since 0.1.1.0 instance Display LogStr where display = displayBytesUtf8 . fromLogStr -- | @since 0.1.1.0 instance HasLogFunc env => MonadLogger (RIO env) where monadLoggerLog loc source level msg = let ?callStack = rioCallStack loc in logGeneric source (rioLogLevel level) (display $ LegacyLogger.toLogStr msg) -- | Do not let the generated function escape its RIO context. This may lead -- to log-related cleanup running /before/ the function is called. -- -- @since 0.1.2.0 instance HasLogFunc env => MonadLoggerIO (RIO env) where askLoggerIO = do runInIO <- askRunInIO pure $ \loc source level str -> let ?callStack = rioCallStack loc in runInIO (logGeneric source (rioLogLevel level) (display str)) rioLogLevel :: LegacyLogger.LogLevel -> LogLevel rioLogLevel level = case level of LegacyLogger.LevelDebug -> LevelDebug LegacyLogger.LevelInfo -> LevelInfo LegacyLogger.LevelWarn -> LevelWarn LegacyLogger.LevelError -> LevelError LegacyLogger.LevelOther name -> LevelOther name rioCallStack :: LegacyLogger.Loc -> CallStack rioCallStack loc = GS.fromCallSiteList [("", GS.SrcLoc { GS.srcLocPackage = LegacyLogger.loc_package loc , GS.srcLocModule = LegacyLogger.loc_module loc , GS.srcLocFile = LegacyLogger.loc_filename loc , GS.srcLocStartLine = fst $ LegacyLogger.loc_start loc , GS.srcLocStartCol = snd $ LegacyLogger.loc_start loc , GS.srcLocEndLine = fst $ LegacyLogger.loc_end loc , GS.srcLocEndCol = snd $ LegacyLogger.loc_end loc })] -- | A collection of all of the registered resource cleanup actions. -- -- @since 0.1.0.0 type ResourceMap = IORef ReleaseMap -- | Perform an action with a 'ResourceMap' -- -- @since 0.1.0.0 withResourceMap :: MonadUnliftIO m => (ResourceMap -> m a) -> m a withResourceMap inner = withRunInIO $ \run -> runResourceT $ ResourceT $ run . inner -- | An environment with a 'ResourceMap' -- -- @since 0.1.0.0 class HasResourceMap env where resourceMapL :: Lens' env ResourceMap instance HasResourceMap (IORef ReleaseMap) where resourceMapL = id instance HasResourceMap env => MonadResource (RIO env) where liftResourceT (ResourceT f) = view resourceMapL >>= liftIO . f rio-orphans-0.1.2.0/test/Spec.hs0000644000000000000000000000005414032141241014473 0ustar0000000000000000{-# OPTIONS_GHC -F -pgmF hspec-discover #-} rio-orphans-0.1.2.0/test/RIO/OrphansSpec.hs0000644000000000000000000000141314032141241016457 0ustar0000000000000000{-# LANGUAGE NoImplicitPrelude #-} module RIO.OrphansSpec (spec) where import Test.Hspec import RIO import RIO.Orphans import Control.Monad.Trans.Resource spec :: Spec spec = do it "success" $ do ref <- newIORef False withResourceMap $ \rm -> runRIO rm $ do void $ register $ writeIORef ref True readIORef ref `shouldReturn` True it "exceptions" $ do ref <- newIORef False void $ tryAny $ withResourceMap $ \rm -> runRIO rm $ do void $ register $ writeIORef ref True throwString "ignored" readIORef ref `shouldReturn` True it "no release" $ do ref <- newIORef False void $ tryAny $ withResourceMap $ \rm -> runRIO rm $ do () <- throwString "ignored" writeIORef ref True readIORef ref `shouldReturn` False rio-orphans-0.1.2.0/LICENSE0000644000000000000000000000204314032141241013273 0ustar0000000000000000Copyright (c) 2018 Michael Snoyman Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rio-orphans-0.1.2.0/Setup.hs0000644000000000000000000000005614032141241013724 0ustar0000000000000000import Distribution.Simple main = defaultMain rio-orphans-0.1.2.0/rio-orphans.cabal0000644000000000000000000000316314032141357015527 0ustar0000000000000000cabal-version: 1.12 -- This file has been generated from package.yaml by hpack version 0.33.0. -- -- see: https://github.com/sol/hpack -- -- hash: 73b0abe6a63aaeca941c08d21e429b2221d75c0121c5d65d2dcd362f0beb6f2e name: rio-orphans version: 0.1.2.0 synopsis: Orphan instances for the RIO type in the rio package description: See README and Haddocks at category: Control homepage: https://github.com/commercialhaskell/rio#readme bug-reports: https://github.com/commercialhaskell/rio/issues author: Michael Snoyman maintainer: michael@snoyman.com license: MIT license-file: LICENSE build-type: Simple extra-source-files: README.md ChangeLog.md source-repository head type: git location: https://github.com/commercialhaskell/rio library exposed-modules: RIO.Orphans other-modules: Paths_rio_orphans hs-source-dirs: src build-depends: base >=4.10 && <10 , exceptions , fast-logger , monad-control , monad-logger , resourcet , rio , transformers-base , unliftio-core default-language: Haskell2010 test-suite rio-orphans-test type: exitcode-stdio-1.0 main-is: Spec.hs other-modules: RIO.OrphansSpec Paths_rio_orphans hs-source-dirs: test ghc-options: -threaded -rtsopts -with-rtsopts=-N build-depends: base >=4.10 && <10 , exceptions , fast-logger , hspec , monad-control , monad-logger , resourcet , rio , rio-orphans , transformers-base , unliftio-core default-language: Haskell2010 rio-orphans-0.1.2.0/README.md0000644000000000000000000000044014032141241013544 0ustar0000000000000000# rio-orphans Provides orphan instances for the `RIO` data type. Currently supports: * `MonadBase` from `transformers-base` * `MonadBaseControl` from `monad-control` * `MonadCatch` and `MonadMask` from `exceptions` * `MonadLogger` from `monad-logger` * `MonadResource` from `resourcet` rio-orphans-0.1.2.0/ChangeLog.md0000644000000000000000000000036514032141321014443 0ustar0000000000000000# Changelog for rio-orphans ## 0.1.2.0 * Add an instance for `MonadLoggerIO` typeclass, from the `monad-logger` library ## 0.1.1.0 * Add an instance for `MonadLogger` typeclass, from the `monad-logger` library ## 0.1.0.0 * Initial release