File: MutableBuilder.hs

package info (click to toggle)
haskell-basement 0.0.16-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: haskell: 11,336; ansic: 63; makefile: 5
file content (31 lines) | stat: -rw-r--r-- 1,245 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
module Basement.MutableBuilder
    ( Builder(..)
    , BuildingState(..)
    ) where

import           Basement.Compat.Base
import           Basement.Compat.MonadTrans
import           Basement.Types.OffsetSize
import           Basement.Monad

newtype Builder collection mutCollection step state err a = Builder
    { runBuilder :: State (Offset step, BuildingState collection mutCollection step (PrimState state), Maybe err) state a }
    deriving (Functor, Applicative, Monad)

-- | The in-progress state of a building operation.
--
-- The previous buffers are in reverse order, and
-- this contains the current buffer and the state of
-- progress packing the elements inside.
data BuildingState collection mutCollection step state = BuildingState
    { prevChunks     :: [collection]
    , prevChunksSize :: !(CountOf step)
    , curChunk       :: mutCollection state
    , chunkSize      :: !(CountOf step)
    }

instance Monad state => MonadFailure (Builder collection mutCollection step state err) where
    type Failure (Builder collection mutCollection step state err) = err
    mFail builderError = Builder $ State $ \(offset, bs, _)  ->
        return ((), (offset, bs, Just builderError))