regions-0.3: Provides the region monad for safely opening and working with scarce resources.Source codeContentsIndex
Control.Monad.Trans.Region.Unsafe
MaintainerBas van Dijk <v.dijk.bas@gmail.com>
Contents
Scarce resources
Description

WARNING: This module should not be used by end-users directly because it allows access to the internalHandle of a resource which enables them to close the resource manually, which will defeat the safety-guarantees that this package provides!

This module should only be used by library authors wishing to allow their end-users to open their resources in a region.

To create a module or library that allows your users to open your resources in a region, the only thing you have to do is to define an instance for Resource for your type of resource.

Make sure not to re-export anything from this module. Either re-export things from Control.Monad.Trans.Region or tell your users to import that module directly.

Synopsis
class Resource resource where
data Handle resource :: *
openResource :: resource -> IO (Handle resource)
closeResource :: Handle resource -> IO ()
internalHandle :: RegionalHandle resource r -> Handle resource
mapInternalHandle :: (Handle resource1 -> Handle resource2) -> RegionalHandle resource1 r -> RegionalHandle resource2 r
Scarce resources
class Resource resource whereSource

Class of scarce resources. A scarce resource is a resource that only one user can use at a time. (like a file, memory pointer or USB device for example).

Because of the scarcity, these resources need to be opened to grant temporary sole access to the resource. When the resource is no longer needed it should be closed a.s.a.p to grant others access to the resource.

Associated Types
data Handle resource :: *Source
Methods
openResource :: resource -> IO (Handle resource)Source
closeResource :: Handle resource -> IO ()Source
internalHandle :: RegionalHandle resource r -> Handle resourceSource

Get the internal handle from the given regional handle.

Warning: This function should not be exported to or used by end-users because it allows them to close the handle manually, which will defeat the safety-guarantees that this package provides!

Tip: If you enable the ViewPatterns language extension you can use internalHandle as a view-pattern as in the following example from the usb-safe package:

      resetDevice :: (pr `ParentOf` cr, MonadIO cr)
                  -> RegionalHandle USB.Device pr -> cr ()
      resetDevice (internalHandle -> (DeviceHandle ...)) = ...
mapInternalHandle :: (Handle resource1 -> Handle resource2) -> RegionalHandle resource1 r -> RegionalHandle resource2 rSource
Modify the internal handle from the given regional handle.
Produced by Haddock version 2.6.0