Unpack.hs.template 972 B

12345678910111213141516171819202122232425262728
  1. {-# LANGUAGE DataKinds #-}
  2. {-# LANGUAGE RankNTypes #-}
  3. {-# LANGUAGE ScopedTypeVariables #-}
  4. module $module_path$.Unpack where
  5. import Ivory.Language
  6. import Ivory.Serialize
  7. import Ivory.Stdlib
  8. unpackWithCallback :: forall n a s1 s2 r b s0
  9. . (ANat n, IvorySizeOf a, IvoryArea a, IvoryZero a, Packable a)
  10. => ConstRef s1 (Array n (Stored Uint8))
  11. -> Ref s2 (Stored Uint32)
  12. -> (ConstRef (Stack s0) a -> Ivory ('Effects r b (Scope s0)) IBool)
  13. -> Ivory ('Effects r b (Scope s0)) IBool
  14. unpackWithCallback arr offs k = do
  15. o <- deref offs
  16. rv <- local (ival false)
  17. let sufficient_remaining = ((o + fromIntegral (packSize (packRep :: PackRep a))) <?
  18. arrayLen arr)
  19. when sufficient_remaining \$ do
  20. v <- local izero
  21. unpackFrom arr o v
  22. offs += fromInteger (packSize (packRep :: PackRep a))
  23. r <- k (constRef v)
  24. store rv r
  25. deref rv