Safe Haskell | Safe-Inferred |
---|---|
Language | Haskell2010 |
Documentation
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.
Concepts such as null padding require the notion of length in bytes in order to handle. In a hand-rolled parser, you may keep count of the current length as you go. Here, the individual types keep track, and expose it via this typeclass.
Obtaining the length of a value is usually an O(1)
operation like reading a
field or returning a constant. When it's not, it's often an indicator of a
problematic type e.g. plain Haskell lists.
We derive a default instance for constant-size types by throwing away the value and reifying the type level natural.
Note that one can derive a free BLen
instance for any type with a Put
instance via serializing it and checking the length. _Do not do this._ If you
find you can't write a decent BLen
instance for a type, it may be that you
need to rethink the representation.
Nothing
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