{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeFamilies #-}
module Codec.Capnp
( newRoot
, setRoot
, getRoot
) where
import Capnp.Classes
import qualified Capnp.Message as M
import qualified Capnp.Untyped as U
newRoot :: (ToStruct (M.MutMsg s) a, Allocate s a, M.WriteCtx m s)
=> M.MutMsg s -> m a
newRoot msg = do
ret <- new msg
setRoot ret
pure ret
setRoot :: (ToStruct (M.MutMsg s) a, M.WriteCtx m s) => a -> m ()
setRoot = U.setRoot . toStruct
getRoot :: (FromStruct msg a, U.ReadCtx m msg) => msg -> m a
getRoot msg = U.rootPtr msg >>= fromStruct