module Foundation.Collection.Buildable
( Buildable(..)
, Builder(..)
, BuildingState(..)
, builderLift
) where
import Foundation.Array.Unboxed
import Foundation.Array.Unboxed.Mutable
import qualified Foundation.Array.Boxed as BA
import qualified Foundation.String.UTF8 as S
import Foundation.Collection.Element
import Foundation.Internal.Base
import Foundation.Primitive.Monad
import Foundation.Boot.Builder
import Foundation.Internal.MonadTrans
class Buildable col where
type Mutable col :: * -> *
type Step col
append :: (PrimMonad prim) => Element col -> Builder col (Mutable col) (Step col) prim ()
build :: (PrimMonad prim)
=> Int
-> Builder col (Mutable col) (Step col) prim ()
-> prim col
builderLift :: (Buildable c, PrimMonad prim)
=> prim a
-> Builder c (Mutable c) (Step c) prim a
builderLift f = Builder $ State $ \(i, st) -> do
ret <- f
return (ret, (i, st))
instance PrimType ty => Buildable (UArray ty) where
type Mutable (UArray ty) = MUArray ty
type Step (UArray ty) = ty
append = builderAppend
build = builderBuild
instance Buildable (BA.Array ty) where
type Mutable (BA.Array ty) = BA.MArray ty
type Step (BA.Array ty) = ty
append = BA.builderAppend
build = BA.builderBuild
instance Buildable S.String where
type Mutable S.String = S.MutableString
type Step S.String = Word8
append = S.builderAppend
build = S.builderBuild