{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
module Ivory.Serialize.Safe.LittleEndian where
import Ivory.Language
import Ivory.Serialize.Atoms
import Ivory.Serialize.PackRep
packInto :: (Packable a, ANat len)
=> Ref s1 ('Array len ('Stored Uint8))
-> Uint32
-> ConstRef s2 a
-> Ivory ('Effects r b ('Scope s3)) ()
packInto = packInto' packRep
packInto' :: ANat len
=> PackRep a
-> Ref s1 ('Array len ('Stored Uint8))
-> Uint32
-> ConstRef s2 a
-> Ivory ('Effects r b ('Scope s3)) ()
packInto' rep buf offs val = do
assert $ offs + fromIntegral (packSize rep) <=? arrayLen buf
packSetLE rep (toCArray buf) offs val
packVInto :: (Packable ('Stored a), ANat len, IvoryInit a)
=> Ref s1 ('Array len ('Stored Uint8))
-> Uint32
-> a
-> Ivory ('Effects r b ('Scope s2)) ()
packVInto = packVInto' packRep
packVInto' :: (ANat len, IvoryInit a)
=> PackRep ('Stored a)
-> Ref s1 ('Array len ('Stored Uint8))
-> Uint32
-> a
-> Ivory ('Effects r b ('Scope s2)) ()
packVInto' rep buf offs val = do
tmp <- local $ ival val
packInto' rep buf offs (constRef tmp)
unpackFrom :: (Packable a, ANat len)
=> ConstRef s1 ('Array len ('Stored Uint8))
-> Uint32
-> Ref s2 a
-> Ivory ('Effects r b ('Scope s3)) ()
unpackFrom = unpackFrom' packRep
unpackFrom' :: ANat len
=> PackRep a
-> ConstRef s1 ('Array len ('Stored Uint8))
-> Uint32
-> Ref s2 a
-> Ivory ('Effects r b ('Scope s3)) ()
unpackFrom' rep buf offs val = do
assert $ offs + fromIntegral (packSize rep) <=? arrayLen buf
packGetLE rep (toCArray buf) offs val
unpackVFrom :: (Packable ('Stored a), ANat len, IvoryStore a, IvoryZeroVal a)
=> ConstRef s1 ('Array len ('Stored Uint8))
-> Uint32
-> Ivory ('Effects r b ('Scope s2)) a
unpackVFrom = unpackVFrom' packRep
unpackVFrom' :: (Packable ('Stored a), ANat len, IvoryStore a, IvoryZeroVal a)
=> PackRep ('Stored a)
-> ConstRef s1 ('Array len ('Stored Uint8))
-> Uint32
-> Ivory ('Effects r b ('Scope s2)) a
unpackVFrom' rep buf offs = do
tmp <- local izero
unpackFrom' rep buf offs tmp
deref tmp