{-# LANGUAGE FlexibleContexts #-}
module Data.Capnp.IO
( hGetValue
, getValue
, hPutValue
, putValue
) where
import Control.Monad.Primitive (RealWorld)
import System.IO (Handle, stdin, stdout)
import Codec.Capnp (getRoot, setRoot)
import Data.Capnp.Classes
(Cerialize(..), Decerialize(..), FromStruct(..), ToStruct(..))
import Data.Capnp.TraversalLimit (evalLimitT)
import Data.Mutable (Thaw(..))
import qualified Data.Capnp.Message as M
hGetValue :: FromStruct M.ConstMsg a => Handle -> Int -> IO a
hGetValue handle limit = do
msg <- M.hGetMsg handle limit
evalLimitT limit (getRoot msg)
getValue :: FromStruct M.ConstMsg a => Int -> IO a
getValue = hGetValue stdin
hPutValue :: (Cerialize RealWorld a, ToStruct (M.MutMsg RealWorld) (Cerial (M.MutMsg RealWorld) a))
=> Handle -> a -> IO ()
hPutValue handle value = do
msg <- M.newMessage
root <- evalLimitT maxBound $ cerialize msg value
setRoot root
constMsg <- freeze msg
M.hPutMsg handle constMsg
putValue :: (Cerialize RealWorld a, ToStruct (M.MutMsg RealWorld) (Cerial (M.MutMsg RealWorld) a))
=> a -> IO ()
putValue = hPutValue stdout