HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Modulebytestring-0.12.2.0Haskell2010

Data.ByteString.Builder.Prim.Internal

  • Warning:* this module is internal. If you find that you need it please contact the maintainers and explain what you are trying to do and discuss what you would need in the public API. It is important that you do this as the module may not be exposed at all in future releases.

The maintainers are glad to accept patches for further standard encodings of standard Haskell values.

If you need to write your own builder primitives, then be aware that you are writing code with all safety belts off; i.e., *this is the code that might make your application vulnerable to buffer-overflow attacks!* The Data.ByteString.Builder.Prim.Tests module provides you with utilities for testing your encodings thoroughly.

Contents
  1. Fixed-size builder primitives
  2. Bounded-size builder primitives
  3. Shared operators
  4. Helpers
  5. Deprecated
Synopsis

Fixed-size builder primitives

typetype Size = Int

The type used for sizes and sizeBounds of sizes.

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
valuestorableToF :: Storable a => FixedPrim a
#

Deprecated. Deprecated since bytestring-0.12.1.0.This function is dangerous in the presence of internal padding and makes naive assumptions about alignment.

  • For a primitive Haskell type like Int64, use the corresponding primitive like int64Host.

  • For other types, it is recommended to manually write a small function that performs the necessary unaligned write and zeroes or removes any internal padding bits.

Bounded-size builder primitives

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

Encode an Either value using the first BoundedPrim for Left values and the second BoundedPrim for Right values.

Note that the functions eitherB, pairB, and contramapB (written below using >$<) suffice to construct BoundedPrims for all non-recursive algebraic datatypes. For example,

maybeB :: BoundedPrim () -> BoundedPrim a -> BoundedPrim (Maybe a)
maybeB nothing just = maybe (Left ()) Right >$< eitherB nothing just
 

Shared operators

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.

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"

Helpers

valuecaseWordSize_32_64 :: a -> a -> a

Select an implementation depending on bitness. Throw a compile time error if bitness is neither 32 nor 64.

Deprecated