{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module Capnp.GenHelpers.Pure
( defaultStruct
, convertValue
, getRoot
, createPure
, toPurePtrConst
, cerializeBasicVec
, cerializeCompositeVec
) where
import Data.Maybe (fromJust)
import Capnp.Classes (cerializeBasicVec, cerializeCompositeVec)
import Capnp.TraversalLimit (evalLimitT)
import Codec.Capnp (getRoot)
import Data.Mutable (freeze)
import Internal.BuildPure (createPure)
import qualified Capnp.Classes as C
import qualified Capnp.Convert as Convert
import qualified Capnp.Message as M
import qualified Capnp.Untyped as U
defaultStruct :: (C.Decerialize a, C.FromStruct M.ConstMsg (C.Cerial M.ConstMsg a)) => a
defaultStruct =
fromJust $
evalLimitT maxBound $
U.rootPtr M.empty >>= C.fromStruct >>= C.decerialize
convertValue ::
( U.RWCtx m s
, M.Message m M.ConstMsg
, C.Cerialize a
, C.ToStruct (M.MutMsg s) (C.Cerial (M.MutMsg s) a)
, C.Decerialize b
, C.FromStruct M.ConstMsg (C.Cerial M.ConstMsg b)
) => a -> m b
convertValue from = do
constMsg :: M.ConstMsg <- Convert.valueToMsg from >>= freeze
Convert.msgToValue constMsg >>= C.decerialize
toPurePtrConst :: C.Decerialize a => C.Cerial M.ConstMsg a -> a
toPurePtrConst = fromJust . evalLimitT maxBound . C.decerialize