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

Copyright(c) Justin Le 2015
LicenseMIT
Maintainerjustin@jle.im
Stabilityunstable
Portabilityportable
Safe HaskellNone
LanguageHaskell2010

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 Data.Binary.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

Eq a => Eq (Tagged a) Source # 

Methods

(==) :: Tagged a -> Tagged a -> Bool #

(/=) :: Tagged a -> Tagged a -> Bool #

Show a => Show (Tagged a) Source # 

Methods

showsPrec :: Int -> Tagged a -> ShowS #

show :: Tagged a -> String #

showList :: [Tagged a] -> ShowS #

Generic (Tagged a) Source # 

Associated Types

type Rep (Tagged a) :: * -> * #

Methods

from :: Tagged a -> Rep (Tagged a) x #

to :: Rep (Tagged a) x -> Tagged a #

Binary a => Binary (Tagged a) Source # 

Methods

put :: Tagged a -> Put #

get :: Get (Tagged a) #

putList :: [Tagged a] -> Put #

type Rep (Tagged a) Source # 
type Rep (Tagged a) = D1 (MetaData "Tagged" "Data.Binary.Tagged.Internal" "tagged-binary-0.2.0.1-Lc3n6I7VjZeMYbUEWKzSH" False) (C1 (MetaCons "Tagged" PrefixI False) ((:*:) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 TagFingerprint)) (S1 (MetaSel (Nothing Symbol) NoSourceUnpackedness SourceStrict DecidedStrict) (Rec0 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.

emptyTagFP gives a TagFingerprint that will most likely never be matched by any actual tag from a real type, so can be used as a test if needed. This replaces functionality that used to come from the Default instance.

Tagging and extracting data

tag :: Typeable a => a -> Tagged a Source #

Wrap data inside a Tagged tuple.

getTagged :: Typeable a => Tagged a -> Maybe a Source #

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 -> Bool Source #

Check if the type inside the Tagged matches the fingerprint.

TagFingerprint utilities

typeFingerprint :: Typeable a => a -> TagFingerprint Source #

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 -> TagFingerprint Source #

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 TagFingerprint Source #

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!

emptyTagFP :: TagFingerprint Source #

TagFingerprint that is meant to never be matched by any actual normal type's TagFingerprint.