mutable-lens-0.4.0.0: Interoperate mutable references with regular lens

Safe HaskellNone
LanguageHaskell2010

Control.Lens.Mutable.Internal

Synopsis

Documentation

class AsLens s a ref where Source #

Convert a reference type to a Lens'.

Methods

asLens :: ref a -> Lens' s a Source #

Instances
AsLens (S OpST RealWorld) a IORef Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: IORef a -> Lens' (S OpST RealWorld) a Source #

AsLens (S OpMVar RealWorld) a MVar Source #

View a MVar a as a SLens 'OpST RealWorld a.

Note: when this is eventually run in IO, the action will block the thread until there is a value present, as per the semantics of takeMVar#. It will then put a value into the MVar, which will block the thread if the value is absent. GHC doesn't give atomicity guarantees for MVar so it's possible this does happen, e.g. if another producer managed to "get in there" during the intervening period between the two operations. Unfortunately GHC does not provide an atomic modifyMVar function or primop.

If you don't want to deal with this, don't use an MVar, use a TMVar.

Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: MVar a -> Lens' (S OpMVar RealWorld) a Source #

AsLens (S OpSTM RealWorld) a TMVar Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: TMVar a -> Lens' (S OpSTM RealWorld) a Source #

AsLens (S OpSTM RealWorld) a TVar Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: TVar a -> Lens' (S OpSTM RealWorld) a Source #

AsLens (S OpST s) a (STRef s) Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: STRef s a -> Lens' (S OpST s) a Source #

AsLens (S OpST s) a (MutVar s) Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

asLens :: MutVar s a -> Lens' (S OpST s) a Source #

class AsLens s a ref => Allocable s a ref where Source #

A state in which you can allocate new references.

This can be defined on either pure or impure references. For pure references one could e.g. define an instance of this on Map k v with Const k as the reference type - see unit tests for an example.

Minimal complete definition

alloc

Methods

alloc :: a -> s -> (ref a, s) Source #

Allocate a new reference with the given value.

free :: ref a -> s -> (a, s) Source #

Deallocate an existing reference, and return its value.

The default implementation simply writes error into the reference and returns the old value. The caller is responsible for actually throwing away the reference and never using it again, as per Haskell's GC semantics.

isValid :: ref a -> s -> (Bool, s) Source #

Check if a reference is valid.

The default implementation simply forces the reference and returns True. If the reference has already been freed (via free) then an error will be raised, which you can catch in the IO monad as per usual. In other words, the default implementation will never return False.

Instances
Allocable (S OpST RealWorld) a IORef Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Allocable (S OpMVar RealWorld) a MVar Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Allocable (S OpSTM RealWorld) a TMVar Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Allocable (S OpSTM RealWorld) a TVar Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Allocable (S OpST s) a (STRef s) Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

alloc :: a -> S OpST s -> (STRef s a, S OpST s) Source #

free :: STRef s a -> S OpST s -> (a, S OpST s) Source #

isValid :: STRef s a -> S OpST s -> (Bool, S OpST s) Source #

Allocable (S OpST s) a (MutVar s) Source # 
Instance details

Defined in Control.Lens.Mutable.Internal

Methods

alloc :: a -> S OpST s -> (MutVar s a, S OpST s) Source #

free :: MutVar s a -> S OpST s -> (a, S OpST s) Source #

isValid :: MutVar s a -> S OpST s -> (Bool, S OpST s) Source #