HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulererebase-1.21.2Haskell2010

Data.ByteString.Builder.Prim

  • 2 types
  • 75 values
  • Packagererebase-1.21.2
  • Exports77
  • LanguageHaskell2010
  • LicenceMIT
  • SourceInternal.hs
value(>$<) :: Contravariant f => (b -> a) -> f a -> f b
#

A fmap-like operator for builder primitives, both bounded and fixed size.

Builder primitives are contravariant so it's like the normal fmap, but backwards (look at the type). (If it helps to remember, the operator symbol is like ($) but backwards.)

We can use it for example to prepend and/or append fixed values to an primitive.

 import Data.ByteString.Builder.Prim as P
showEncoding ((\x -> ('\'', (x, '\''))) >$< fixed3) 'x' = "'x'"
  where
    fixed3 = P.char7 >*< P.char7 >*< P.char7

Note that the rather verbose syntax for composition stems from the requirement to be able to compute the size / size bound at compile time.

valueprimBounded :: BoundedPrim a -> a -> Builder
#

Create a Builder that encodes values with the given BoundedPrim.

We rewrite consecutive uses of primBounded such that the bound-checks are fused. For example,

primBounded (word32 c1) `mappend` primBounded (word32 c2)

is rewritten such that the resulting Builder checks only once, if ther are at 8 free bytes, instead of checking twice, if there are 4 free bytes. This optimization is not observationally equivalent in a strict sense, as it influences the boundaries of the generated chunks. However, for a user of this library it is observationally equivalent, as chunk boundaries of a LazyByteString can only be observed through the internal interface. Moreover, we expect that all primitives write much fewer than 4kb (the default short buffer size). Hence, it is safe to ignore the additional memory spilled due to the more aggressive buffer wrapping introduced by this optimization.

valueprimMapListBounded :: BoundedPrim a -> [a] -> Builder
#

Create a Builder that encodes a list of values consecutively using a BoundedPrim for each element. This function is more efficient than

mconcat . map (primBounded w)

or

foldMap (primBounded w)

because it moves several variables out of the inner loop.

datadata BoundedPrim a
#

A builder primitive that always results in sequence of bytes that is no longer than a pre-determined bound.

Instances2Contravariant, Monoidal
  • Contravariant BoundedPrimDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.Prim.Internal
  • Monoidal BoundedPrimDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.Prim.Internal

Heavy inlining. Encode all bytes of a StrictByteString from left-to-right with a FixedPrim. This function is quite versatile. For example, we can use it to construct a Builder that maps every byte before copying it to the buffer to be filled.

mapToBuilder :: (Word8 -> Word8) -> S.StrictByteString -> Builder
mapToBuilder f = primMapByteStringFixed (contramapF f word8)

We can also use it to hex-encode a StrictByteString as shown by the byteStringHex example above.

Encode a Float in native host order and host endianness. Values written this way are not portable to different endian machines, without conversion.

valueintHost :: FixedPrim Int
#

Encode a single native machine Int. The Ints is encoded in host order, host endian form, for the machine you are on. On a 64 bit machine the Int is an 8 byte value, on a 32 bit machine, 4 bytes. Values encoded this way are not portable to different endian or integer sized machines, without conversion.

valuewordHost :: FixedPrim Word
#

Encode a single native machine Word. The Words is encoded in host order, host endian form, for the machine you are on. On a 64 bit machine the Word is an 8 byte value, on a 32 bit machine, 4 bytes. Values encoded this way are not portable to different endian or word sized machines, without conversion.

value(>*<) :: Monoidal f => f a -> f b -> f (a, b)
#

A pairing/concatenation operator for builder primitives, both bounded and fixed size.

For example,

toLazyByteString (primFixed (char7 >*< char7) ('x','y')) = "xy"

We can combine multiple primitives using >*< multiple times.

toLazyByteString (primFixed (char7 >*< char7 >*< char7) ('x',('y','z'))) = "xyz"
datadata FixedPrim a
#

A builder primitive that always results in a sequence of bytes of a pre-determined, fixed size.

Instances2Contravariant, Monoidal
  • Contravariant FixedPrimDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.Prim.Internal
  • Monoidal FixedPrimDefined in bytestring-0.12.2.0 · Data.ByteString.Builder.Prim.Internal