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.
Synopsis
- class BLen a where
- newtype BLen' a = BLen' {
- getBLen' :: a
- blenGenericNonSum :: forall {cd} {f} {asserts} a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum (BLen' Int) f, asserts ~ '['NoEmpty, 'NoSum], ApplyGCAsserts asserts f) => a -> Int
- blenGenericSum :: forall {cd} {f} {asserts} a. (Generic a, Rep a ~ D1 cd f, GFoldMapSum 'SumOnly (BLen' Int) f, asserts ~ '['NoEmpty, 'NeedSum], ApplyGCAsserts asserts f) => (String -> Int) -> a -> Int
- newtype CBLenly a = CBLenly {
- unCBLenly :: a
- cblen :: forall a n. (n ~ CBLen a, KnownNat n) => Int
Documentation
Instances
Instances
Num a => Monoid (BLen' a) Source # | |
Num a => Semigroup (BLen' a) Source # | |
GenericFoldMap (BLen' Int) Source # | |
Defined in Binrep.BLen.Simple type GenericFoldMapC (BLen' Int) a # genericFoldMapF :: GenericFoldMapC (BLen' Int) a => a -> BLen' Int # | |
type GenericFoldMapC (BLen' Int) a Source # | |
Defined in Binrep.BLen.Simple |
blenGenericNonSum :: forall {cd} {f} {asserts} a. (Generic a, Rep a ~ D1 cd f, GFoldMapNonSum (BLen' Int) f, asserts ~ '['NoEmpty, 'NoSum], ApplyGCAsserts asserts f) => a -> Int Source #
Measure the byte length of a term of the non-sum type a
via its Generic
instance.
blenGenericSum :: forall {cd} {f} {asserts} a. (Generic a, Rep a ~ D1 cd f, GFoldMapSum 'SumOnly (BLen' Int) f, asserts ~ '['NoEmpty, 'NeedSum], ApplyGCAsserts asserts f) => (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!
Deriving via wrapper for types which may derive a BLen
instance through
an existing IsCBLen
instance.
Examples of such types include machine integers, and explicitly-sized types (e.g. Binrep.Type.Sized).