-- | Module: Internal.Rpc.Breaker
--
-- This module serves to break a dependency cycle between the rpc
-- system and the serialization code; see Note [Breaker] in
-- "Capnp.Rpc.Untyped" for details.
module Internal.Rpc.Breaker
  ( Client (..),
    Pipeline (..),
    nullClient,
    -- | ** Internals
    Opaque,
    makeOpaque,
    reflectOpaque,
  )
where

import Data.Dynamic (Dynamic, Typeable, fromDynamic, toDyn)

-- | A reference to a capability, which may be live either in the current vat
-- or elsewhere. Holding a client affords making method calls on a capability
-- or modifying the local vat's reference count to it.
newtype Client = Client Opaque
  deriving (Client -> Client -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Client -> Client -> Bool
$c/= :: Client -> Client -> Bool
== :: Client -> Client -> Bool
$c== :: Client -> Client -> Bool
Eq)

instance Show Client where
  show :: Client -> String
show Client
client =
    if Client
client forall a. Eq a => a -> a -> Bool
== Client
nullClient
      then String
"nullClient"
      else String
"({- capability; not statically representable -})"

-- | A 'Pipeline' is a reference to a value within a message that has not yet arrived.
newtype Pipeline = Pipeline Opaque

-- | A null client. This is the only client value that can be represented
-- statically. Throws exceptions in response to all method calls.
nullClient :: Client
nullClient :: Client
nullClient = Opaque -> Client
Client forall a b. (a -> b) -> a -> b
$ forall a. (Typeable a, Eq a) => a -> Opaque
makeOpaque ()

data Opaque = Opaque
  { Opaque -> Dynamic
opDyn :: Dynamic,
    Opaque -> Opaque -> Bool
opEq :: Opaque -> Bool
  }

makeOpaque :: (Typeable a, Eq a) => a -> Opaque
makeOpaque :: forall a. (Typeable a, Eq a) => a -> Opaque
makeOpaque a
v =
  Opaque
    { opDyn :: Dynamic
opDyn = forall a. Typeable a => a -> Dynamic
toDyn a
v,
      opEq :: Opaque -> Bool
opEq = \Opaque
o -> forall a. Typeable a => Dynamic -> Maybe a
fromDynamic (Opaque -> Dynamic
opDyn Opaque
o) forall a. Eq a => a -> a -> Bool
== forall a. a -> Maybe a
Just a
v
    }

reflectOpaque :: Opaque -> Dynamic
reflectOpaque :: Opaque -> Dynamic
reflectOpaque = Opaque -> Dynamic
opDyn

instance Eq Opaque where
  Opaque
x == :: Opaque -> Opaque -> Bool
== Opaque
y = Opaque -> Opaque -> Bool
opEq Opaque
x Opaque
y