Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Documentation
Newtype wrapper for defining BLen
instances which are allowed to assume
the existence of a valid CBLen
family instance.
WithCBLen | |
|
The length in bytes of a value of the given type can be known on the cheap e.g. by reading a length field, or using compile time information.
Some binary representation building blocks require the notion of length in bytes in order to handle, e.g. null padding. One may always obtain this by serializing the value, then reading out the length of the output bytestring. But in most cases, we can be much more efficient.
- Certain primitives have a size known at compile time, irrelevant of the
value. A
Word64
is always 8 bytes; some data null-padded ton
bytes is exactlyn
bytes long. - For simple ADTs, it's often possible to calculate length in bytes via pattern matching and some numeric operations. Very little actual work.
This type class enables each type to implement its own efficient method of byte length calculation. Aim to write something that plainly feels more efficient than full serialization. If that doesn't feel possible, you might be working with a type ill-suited for binary representation.
A thought: Some instances could be improved by reifying CBLen
. But it would
mess up all the deriving, and it feels like too minor an improvement to be
worthwhile supporting, writing a bunch of newtype wrappers, etc.
Nothing
type CBLen a :: Natural Source #
The length in bytes of any value of the given type is constant.
Many binary representation primitives are constant, or may be designed to
"store" their size in their type. This is a stronger statement about
their length than just blen
.
This is now an associated type family of the BLen
type class in hopes
of simplifying the binrep framework.
The length in bytes of the serialized value.
The default implementation reifies the constant length for the type. If a type-wide constant length is not defined, it will fail at compile time.
Instances
typeNatToBLen :: forall n. KnownNat n => BLenT Source #
cblen :: forall a n. (n ~ CBLen a, KnownNat n) => BLenT Source #
Reify a type's constant byte length to the term level.
module Binrep.BLen.Internal.AsBLen