module Bytezap.Write.Internal where

import Bytezap.Poke qualified as P

-- | A 'Poke' with the associated size it pokes.
data Write s = Write { forall s. Write s -> Int
size :: Int, forall s. Write s -> Poke s
poke :: P.Poke s }

-- | Sequence the 'Poke's, sum the sizes.
instance Semigroup (Write s) where
    -- TODO feels like this might be INLINE[1] or even INLINE[0]?
    Write Int
ll Poke s
lp <> :: Write s -> Write s -> Write s
<> Write Int
rl Poke s
rp = Int -> Poke s -> Write s
forall s. Int -> Poke s -> Write s
Write (Int
ll Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
rl) (Poke s
lp Poke s -> Poke s -> Poke s
forall a. Semigroup a => a -> a -> a
<> Poke s
rp)

-- | The empty 'Write' is the empty 'Poke', which writes zero bytes.
instance Monoid (Write s) where
    mempty :: Write s
mempty = Int -> Poke s -> Write s
forall s. Int -> Poke s -> Write s
Write Int
0 Poke s
forall a. Monoid a => a
mempty