tagged-binary-0.1.2.0: Provides tools for serializing data tagged with type information.

Portabilityportable
Stabilityunstable
Maintainerjustin@jle.im
Safe HaskellNone

Data.Binary.Tagged.Internal

Contents

Description

Internals for the library, exported in case you should need it. Usually, the parts you would need should be re-exported in Tagged.

Synopsis

Data types

data Tagged a Source

A data type tupling together data with a TagFingerprint, representing data tagged with its type.

It's best to interface directly with data using encodeTagged, decodeTagged, etc, using tag to tag data and extractTagged to extract data from valid tagged data. This type is exported mostly when you want to specifically decode a ByteString into tagged data, and manually extract it yourself. If you are writing a framework, it is preferred to handle this for the end user.

Instances

Typeable1 Tagged 
Eq a => Eq (Tagged a) 
Show a => Show (Tagged a) 
Generic (Tagged a) 
Binary a => Binary (Tagged a) 

data TagFingerprint Source

A data type representing a fingerprint for a Typeable type. Ideally, this would be Internal's own Fingerprint types; however, for some reason, the fingerprints for the same data type from the same modules differ between different GHC backends. So for now, it is just a ByteString representation of the name of the type. This is literally a bad idea, and so two types with the same name but from different modules will share a non-unique TagFingerprint. Hopefully in the future when I find out a way to fix this or the GHC backend maintainers find a way to provide consistent type fingerprints, this will be fixed.

This type is mostly used for the ability to categorized Tagged items by their type.

There is a Default instance, because the constructor is hidden. For now, it is just an empty ByteString, but when fingerprinting works for real, think of it as a way to generate a fingerprint that will most likely not be matched by any type, in case the need ever comes up.

Tagging and extracting data

tag :: Typeable a => a -> Tagged aSource

Wrap data inside a Tagged tuple.

getTagged :: Typeable a => Tagged a -> Maybe aSource

Extract data out of a Tagged, but only the type of the data matches the type represented by the fingerprint. It is polymorphic on its output and meant to be used when decoding a Tagged item with a desired type.

tagMatched :: Typeable a => Tagged a -> BoolSource

Check if the type inside the Tagged matches the fingerprint.

TagFingerprint utilities

typeFingerprint :: Typeable a => a -> TagFingerprintSource

Compute the Fingerprint representing a type. It is non-strict on its parameter, so passing in undefined should work if you want to just get the Fingerprint of a specific type without having data of that type on hand:

 typeFingerprint (undefined :: Int)

tagFingerprint :: Tagged a -> TagFingerprintSource

Extract the Fingerprint out of a Tagged. Mostly used so that you can categorize and associate Tagged items; to check if a Tagged is of a desired typed, getTagged and tagMatched might be more useful.

bsFingerprint :: ByteString -> Maybe TagFingerprintSource

With a ByteString, expecting tagged data, returns the Fingerprint that the data is tagged with. Returns Nothing if the data is not decodable as tagged data. Might accidentally decode untagged data though!