@@ -84,7 +84,8 @@ import Data.IntSet (IntSet)
8484import qualified Data.IntSet as IntSet (empty, insert, elems, singleton, null, delete, unions)
8585import Data.Map (Map)
8686import qualified Data.Map as Map (empty, elems, size)
87−import Data.Lens.Lazy (Lens, lens, intMapLens, mapLens, (^.), (^=), (^+=), (^%=))
87+import Data.Accessor (Accessor, accessor, (^.), (^=), (^:))
88+import qualified Data.Accessor.Container as DAC (mapMaybe, intMapMaybe)
8889import System.IO (hPutStrLn, stderr)
8990
9091-- $design
@@ -181,7 +182,7 @@ import System.IO (hPutStrLn, stderr)
181182-- Internal datatypes --
182183--------------------------------------------------------------------------------
183184
184−-- We use underscores for fields that we might update (using lenses)
185+-- We use underscores for fields that we might update (using accessores)
185186--
186187-- All data types follow the same structure:
187188--
@@ -396,7 +397,7 @@ apiClose theirEndPoint connId connAlive = void . tryIO $ do
396397 then do
397398 writeIORef connAlive False
398399 sendOn vst [encodeInt32 CloseConnection, encodeInt32 connId]
399− return (RemoteEndPointValid . (remoteOutgoing ^+= (-1)) $ vst)
400+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ vst)
400401 else
401402 return (RemoteEndPointValid vst)
402403 RemoteEndPointClosed -> do
@@ -559,7 +560,7 @@ requestConnectionTo ourEndPoint theirAddress = go
559560 endPointStateSnapshot <- modifyMVar theirState $ \st ->
560561 case st of
561562 RemoteEndPointValid ep ->
562− return (RemoteEndPointValid . (remoteOutgoing ^+= 1) $ ep, st)
563+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ ep, st)
563564 _ ->
564565 return (st, st)
565566
@@ -595,7 +596,7 @@ requestConnectionTo ourEndPoint theirAddress = go
595596 let failureHandler :: IOException -> IO b
596597 failureHandler err = do
597598 modifyMVar_ theirState $ \(RemoteEndPointValid ep) ->
598− return (RemoteEndPointValid . (remoteOutgoing ^+= (-1)) $ ep)
599+ return (RemoteEndPointValid . (remoteOutgoing ^: (+ 1)) $ ep)
599600 -- TODO: should we call closeIfUnused here?
600601 throw $ FailedWith ConnectFailed (show err)
601602
@@ -754,7 +755,7 @@ doRemoteRequest (ourEndPoint, theirEndPoint) header = do
754755 reqId <- modifyMVar (localState ourEndPoint) $ \st -> case st of
755756 LocalEndPointValid vst -> do
756757 let reqId = vst ^. nextCtrlRequestId
757− return (LocalEndPointValid . (nextCtrlRequestId ^+= 1) . (pendingCtrlRequestsAt reqId ^= Just reply) $ vst, reqId)
758+ return (LocalEndPointValid . (nextCtrlRequestId ^: (+ 1)) . (pendingCtrlRequestsAt reqId ^= Just reply) $ vst, reqId)
758759 LocalEndPointClosed ->
759760 throw (userError "Local endpoint closed")
760761 withMVar (remoteState theirEndPoint) $ \(RemoteEndPointValid vst) ->
@@ -785,7 +786,7 @@ forkEndPointThread ourEndPoint p =
785786 modifyMVar ourState $ \st -> case st of
786787 LocalEndPointValid vst -> do
787788 tid <- forkIO (p >> removeThread)
788− return (LocalEndPointValid . (internalThreads ^%= (tid :)) $ vst, True)
789+ return (LocalEndPointValid . (internalThreads ^: (tid :)) $ vst, True)
789790 LocalEndPointClosed ->
790791 return (LocalEndPointClosed, False)
791792 where
@@ -794,7 +795,7 @@ forkEndPointThread ourEndPoint p =
794795 tid <- myThreadId
795796 modifyMVar_ ourState $ \st -> case st of
796797 LocalEndPointValid vst ->
797− return (LocalEndPointValid . (internalThreads ^%= filter (/= tid)) $ vst)
798+ return (LocalEndPointValid . (internalThreads ^: filter (/= tid)) $ vst)
798799 LocalEndPointClosed ->
799800 return LocalEndPointClosed
800801
@@ -944,7 +945,7 @@ handleIncomingMessages (ourEndPoint, theirEndPoint) = do
944945 modifyMVar_ theirState $ \st -> do
945946 vst <- case st of
946947 RemoteEndPointValid vst ->
947− return (remoteIncoming ^%= IntSet.insert newId $ vst)
948+ return (remoteIncoming ^: IntSet.insert newId $ vst)
948949 RemoteEndPointClosing resolved vst -> do
949950 -- If the endpoint is in closing state that means we send a
950951 -- CloseSocket request to the remote endpoint. If the remote
@@ -987,7 +988,7 @@ handleIncomingMessages (ourEndPoint, theirEndPoint) = do
987988 -- TODO: we should check that this connection is in fact open
988989 writeChan ourChannel (ConnectionClosed cid)
989990 modifyMVar_ theirState $ \(RemoteEndPointValid vst) ->
990− return (RemoteEndPointValid . (remoteIncoming ^%= IntSet.delete cid) $ vst)
991+ return (RemoteEndPointValid . (remoteIncoming ^: IntSet.delete cid) $ vst)
991992 closeIfUnused theirEndPoint
992993
993994 -- Close the socket (if we don't have any outgoing connections)
@@ -1090,38 +1091,38 @@ firstNonReservedConnectionId :: ConnectionId
10901091firstNonReservedConnectionId = 1024
10911092
10921093--------------------------------------------------------------------------------
1093−-- Lens definitions --
1094+-- Accessor definitions --
10941095--------------------------------------------------------------------------------
10951096
1096−localEndPoints :: Lens ValidTransportState (Map EndPointAddress LocalEndPoint)
1097−localEndPoints = lens _localEndPoints (\es st -> st { _localEndPoints = es })
1097+localEndPoints :: Accessor ValidTransportState (Map EndPointAddress LocalEndPoint)
1098+localEndPoints = accessor _localEndPoints (\es st -> st { _localEndPoints = es })
10981099
1099−pendingCtrlRequests :: Lens ValidLocalEndPointState (IntMap (MVar [ByteString]))
1100−pendingCtrlRequests = lens _pendingCtrlRequests (\rep st -> st { _pendingCtrlRequests = rep })
1100+pendingCtrlRequests :: Accessor ValidLocalEndPointState (IntMap (MVar [ByteString]))
1101+pendingCtrlRequests = accessor _pendingCtrlRequests (\rep st -> st { _pendingCtrlRequests = rep })
11011102
1102−nextCtrlRequestId :: Lens ValidLocalEndPointState ControlRequestId
1103−nextCtrlRequestId = lens _nextCtrlRequestId (\cid st -> st { _nextCtrlRequestId = cid })
1103+nextCtrlRequestId :: Accessor ValidLocalEndPointState ControlRequestId
1104+nextCtrlRequestId = accessor _nextCtrlRequestId (\cid st -> st { _nextCtrlRequestId = cid })
11041105
1105−nextConnectionId :: Lens ValidLocalEndPointState ConnectionId
1106−nextConnectionId = lens _nextConnectionId (\cix st -> st { _nextConnectionId = cix })
1106+nextConnectionId :: Accessor ValidLocalEndPointState ConnectionId
1107+nextConnectionId = accessor _nextConnectionId (\cix st -> st { _nextConnectionId = cix })
11071108
1108−localConnections :: Lens ValidLocalEndPointState (Map EndPointAddress RemoteEndPoint)
1109−localConnections = lens _localConnections (\es st -> st { _localConnections = es })
1109+localConnections :: Accessor ValidLocalEndPointState (Map EndPointAddress RemoteEndPoint)
1110+localConnections = accessor _localConnections (\es st -> st { _localConnections = es })
11101111
1111−internalThreads :: Lens ValidLocalEndPointState [ThreadId]
1112−internalThreads = lens _internalThreads (\ts st -> st { _internalThreads = ts })
1112+internalThreads :: Accessor ValidLocalEndPointState [ThreadId]
1113+internalThreads = accessor _internalThreads (\ts st -> st { _internalThreads = ts })
11131114
1114−localEndPointAt :: EndPointAddress -> Lens ValidTransportState (Maybe LocalEndPoint)
1115−localEndPointAt addr = localEndPoints >>> mapLens addr
1115+localEndPointAt :: EndPointAddress -> Accessor ValidTransportState (Maybe LocalEndPoint)
1116+localEndPointAt addr = localEndPoints >>> DAC.mapMaybe addr
11161117
1117−pendingCtrlRequestsAt :: ControlRequestId -> Lens ValidLocalEndPointState (Maybe (MVar [ByteString]))
1118−pendingCtrlRequestsAt ix = pendingCtrlRequests >>> intMapLens (fromIntegral ix)
1118+pendingCtrlRequestsAt :: ControlRequestId -> Accessor ValidLocalEndPointState (Maybe (MVar [ByteString]))
1119+pendingCtrlRequestsAt ix = pendingCtrlRequests >>> DAC.intMapMaybe (fromIntegral ix)
11191120
1120−localConnectionTo :: EndPointAddress -> Lens ValidLocalEndPointState (Maybe RemoteEndPoint)
1121−localConnectionTo addr = localConnections >>> mapLens addr
1121+localConnectionTo :: EndPointAddress -> Accessor ValidLocalEndPointState (Maybe RemoteEndPoint)
1122+localConnectionTo addr = localConnections >>> DAC.mapMaybe addr
11221123
1123−remoteOutgoing :: Lens ValidRemoteEndPointState Int
1124−remoteOutgoing = lens _remoteOutgoing (\cs conn -> conn { _remoteOutgoing = cs })
1124+remoteOutgoing :: Accessor ValidRemoteEndPointState Int
1125+remoteOutgoing = accessor _remoteOutgoing (\cs conn -> conn { _remoteOutgoing = cs })
11251126
1126−remoteIncoming :: Lens ValidRemoteEndPointState IntSet
1127−remoteIncoming = lens _remoteIncoming (\cs conn -> conn { _remoteIncoming = cs })
1127+remoteIncoming :: Accessor ValidRemoteEndPointState IntSet
1128+remoteIncoming = accessor _remoteIncoming (\cs conn -> conn { _remoteIncoming = cs })