Safe Haskell | Safe-Inferred |
---|---|
Language | GHC2021 |
Byte length as a simple pure function, no bells or whistles.
Non-reallocating serializers like store, bytezap or ptr-poker request the expected total byte length when serializing. Thus, they need some way to measure byte length *before* serializing. This is that.
It should be very efficient to calculate serialized byte length for most binrep-compatible Haskell types. If it isn't, consider whether the representation is appropriate for binrep.
Note that you _may_ encode this inside the serializer type (whatever the Put
class stores). I went back and forth on this a couple times. But some binrep
code seems to make more sense when byte length is standalone. And I don't mind
the extra explicitness. So it's here to stay :)
Synopsis
- class BLen a where
- blenGenericNonSum :: forall a. (Generic a, GFoldMapNonSum BLen (Rep a), GAssertNotVoid a, GAssertNotSum a) => a -> Int
- blenGenericSum :: forall a. (Generic a, GFoldMapSum BLen (Rep a), GAssertNotVoid a, GAssertSum a) => (String -> Int) -> a -> Int
- newtype ViaCBLen a = ViaCBLen {
- unViaCBLen :: a
- cblen :: forall a. KnownNat (CBLen a) => Int
Documentation
Class for types with easily-calculated length in bytes.
If it appears hard to calculate byte length for a given type (e.g. without first serializing it, then measuring serialized byte length), consider whether this type is a good fit for binrep.
Instances
blenGenericNonSum :: forall a. (Generic a, GFoldMapNonSum BLen (Rep a), GAssertNotVoid a, GAssertNotSum a) => a -> Int Source #
Measure the byte length of a term of the non-sum type a
via its Generic
instance.
blenGenericSum :: forall a. (Generic a, GFoldMapSum BLen (Rep a), GAssertNotVoid a, GAssertSum a) => (String -> Int) -> a -> Int Source #
Measure the byte length of a term of the sum type a
via its Generic
instance.
You must provide a function to obtain the byte length for the prefix tag, via inspecting the reified constructor names. This is regrettably inefficient. Alas. Do write your own instance if you want better performance!
DerivingVia wrapper for types which may derive a BLen
instance through
an existing IsCBLen
instance (i.e. it is known at compile time)
Examples of such types include machine integers, and explicitly-sized types (e.g. Binrep.Type.Sized).
ViaCBLen | |
|