Modulesafecopy-0.10.4.3Haskell2010
Data.SafeCopy.Internal
- 12 types
- 6 classes
- 54 values
- Packagesafecopy-0.10.4.3
- Exports73
- LanguageHaskell2010
- LicenceLicenseRef-PublicDomain
- SourceSafeCopy.hs
The default kind. Does not extend any type.
Place a value in an unbreakable container.
The extended_base kind lets the system know that there is at least one future version of this type.
The extended_extension kind lets the system know that there is at least one previous and one future version of this type.
The extension kind lets the system know that there is at least one previous version of this type. A given data type can only extend a single other data type. However, it is perfectly fine to build chains of extensions. The migrations between each step is handled automatically.
Parse a version tag and return the corresponding migrated parser. This is useful when you can prove that multiple values have the same version. See getSafePut.
Whereas the other getSafeGet is only run when we know we need a version, this one is run for every field and must decide whether to read a version or not. It constructs a Map TypeRep Int32 and reads when the new TypeRep is not in the map.
Serialize the version tag and return the associated putter. This is useful when serializing multiple values with the same version. See getSafeGet.
This version returns (Put, Put), the collected version tags and the collected serialized fields. The original getSafePut result type prevents doing this because each fields may have a different type. Maybe you can show me a better way
See safePutGeneric. A copy of the code in the default implementation of the putCopy method.
Parse a version tagged data type and then migrate it to the desired type. Any serialized value has been extended by the return type can be parsed.
Serialize a data type by first writing out its version tag. This is much simpler than the corresponding safeGet since previous versions don't come into play.
Generic only version of safePut. Instead of calling putCopy it calls putCopyDefault, a copy of the implementation of the SafeCopy default method for putCopy.
PutM doesn't have reasonable fail implementation. It just throws an unpure exception anyway.
Constructors
Constructors
Instances1Show
Show DatatypeInfoDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy
Instances3GGetCopy
(GGetCopy f p, GGetCopy g p, p ~ DatatypeInfo) => GGetCopy (f :+: g) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGGetFields f p => GGetCopy (M1 C c f) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy(GGetCopy f p, p ~ DatatypeInfo) => GGetCopy (M1 D d f) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyThe M1 type has a fourth type parameter p:
newtype M1 i (c :: Meta) (f :: k -> *) (p :: k) = M1 {unM1 :: f p}
Note that the type of the M1 field is
f p, so in order to express this type we add a parameter of type p that we can apply to values of type f.
Instances5GGetFields
GGetFields U1 pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGGetFields V1 pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopySafeCopy a => GGetFields (K1 R a) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy(GGetFields f p, GGetFields g p) => GGetFields (f :*: g) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGGetFields f p => GGetFields (M1 S c f) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy
Instances3GPutCopy
(GPutCopy f p, GPutCopy g p) => GPutCopy (f :+: g) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGPutCopy a p => GPutCopy (M1 D c a) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy(GPutFields a p, p ~ DatatypeInfo) => GPutCopy (M1 C c a) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy
gputFields traverses the fields of a constructor and returns a put for the safecopy versions and a put for the field values.
Instances5GPutFields
GPutFields U1 pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGPutFields V1 pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopySafeCopy a => GPutFields (K1 R a) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy(GPutFields f p, GPutFields g p) => GPutFields (f :*: g) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyGPutFields f p => GPutFields (M1 S c f) pDefined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy
The kind of a data type determines how it is tagged (if at all).
Primitives kinds (see primitive) are not tagged with a version id and hence cannot be extended later.
Extensions (see extension) tell the system that there exists a previous version of the data type which should be migrated if needed.
There is also a default kind which is neither primitive nor an extension of a previous type.
The central mechanism for dealing with version control.
This type class specifies what data migrations can happen and how they happen.
Associated types
type family MigrateFrom aThis is the type we're extending. Each type capable of migration can only extend one other type.
Methods
migrate :: MigrateFrom a -> aThis method specifies how to migrate from the older type to the newer one. It will never be necessary to use this function manually as it all taken care of internally in the library.
This is the type we're extending. Each type capable of migration can only extend one other type.
Instances2MigrateFrom
type MigrateFrom Double = CerealDouble040Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphantype MigrateFrom Float = CerealFloat040Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan
Wrapper for data that was saved without a version tag.
Constructors
PrimgetPrimitive :: a
The centerpiece of this library. Defines a version for a data type together with how it should be serialized/parsed.
Users should define instances of SafeCopy for their types even though getCopy and putCopy can't be used directly. To serialize/parse a data type using SafeCopy, see safeGet and safePut.
Methods
version :: Version aThe version of the type.
Only used as a key so it must be unique (this is checked at run-time) but doesn't have to be sequential or continuous.
The default version is '0'.
kind :: Kind agetCopy :: Contained (Get a)This method defines how a value should be parsed without also worrying about writing out the version tag. This function cannot be used directly. One should use safeGet, instead.
putCopy :: a -> Contained PutThis method defines how a value should be parsed without worrying about previous versions or migrations. This function cannot be used directly. One should use 'safePut, instead.
internalConsistency :: Consistency aInternal function that should not be overrided.
Consistentiff the version history is consistent (i.e. there are no duplicate version numbers) and the chain of migrations is valid.This function is in the typeclass so that this information is calculated only once during the program lifetime, instead of everytime safeGet or safePut is used.
objectProfile :: Profile aVersion profile.
errorTypeName :: Proxy a -> StringThe name of the type. This is only used in error message strings.
Instances64SafeCopy, …
SafeCopy ByteStringDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy ByteStringDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy IntSetDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy IntegerDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy NaturalDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Int16Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Int32Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Int64Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Int8Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy BoolDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy CharDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy DoubleDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy FloatDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy IntDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy OrderingDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy WordDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Word16Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Word32Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Word64Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy Word8Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy CalendarTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy ClockTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy DayDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy MonthDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy TimeDiffDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy CerealDouble040Defined in safecopy-0.10.4.3 · Data.SafeCopy.InstancesSafeCopy CerealFloat040Defined in safecopy-0.10.4.3 · Data.SafeCopy.InstancesSafeCopy TextDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy TextDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy DayDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy AbsoluteTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy DiffTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy NominalDiffTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy UTCTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy UniversalTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy LocalTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy TimeOfDayDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy TimeZoneDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy ZonedTimeDefined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy ()Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (IntMap a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (Seq a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (Tree a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (NonEmpty a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (Maybe a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (Prim a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy (Vector a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphanSafeCopy a => SafeCopy [a]Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(Integral a, SafeCopy a) => SafeCopy (Ratio a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, Ord a) => SafeCopy (Set a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, Storable a) => SafeCopy (Vector a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, Prim a) => SafeCopy (Vector a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, Unbox a) => SafeCopy (Vector a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(IArray UArray e, Ix i, SafeCopy e, SafeCopy i) => SafeCopy (UArray i e)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(HasResolution a, Fractional (Fixed a), Typeable a) => SafeCopy (Fixed a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(Ix i, SafeCopy e, SafeCopy i) => SafeCopy (Array i e)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b) => SafeCopy (Either a b)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b) => SafeCopy (a, b)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, Ord a) => SafeCopy (Map a b)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, SafeCopy c) => SafeCopy (a, b, c)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, SafeCopy c, SafeCopy d) => SafeCopy (a, b, c, d)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, SafeCopy c, SafeCopy d, SafeCopy e) => SafeCopy (a, b, c, d, e)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, SafeCopy c, SafeCopy d, SafeCopy e, SafeCopy f) => SafeCopy (a, b, c, d, e, f)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan(SafeCopy a, SafeCopy b, SafeCopy c, SafeCopy d, SafeCopy e, SafeCopy f, SafeCopy g) => SafeCopy (a, b, c, d, e, f, g)Defined in safecopy-0.10.4.3 · Data.SafeCopy.Instances · orphan
Instances5Eq, Num, Read, Show, Serialize
Eq (Version a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyNum (Version a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyRead (Version a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopyShow (Version a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopySerialize (Version a)Defined in safecopy-0.10.4.3 · Data.SafeCopy.SafeCopy
Derive an instance of SafeCopy.
When serializing, we put a Word8 describing the
constructor (if the data type has more than one
constructor). For each type used in the constructor, we
call getSafePut (which immediately serializes the version
of the type). Then, for each field in the constructor, we
use one of the put functions obtained in the last step.
For example, given the data type and the declaration below
data T0 b = T0 b Int
deriveSafeCopy 1 'base ''T0
we generate
instance (SafeCopy a, SafeCopy b) =>
SafeCopy (T0 b) where
putCopy (T0 arg1 arg2) = contain $ do put_b <- getSafePut
put_Int <- getSafePut
put_b arg1
put_Int arg2
return ()
getCopy = contain $ do get_b <- getSafeGet
get_Int <- getSafeGet
return T0 <*> get_b <*> get_Int
version = 1
kind = base
And, should we create another data type as a newer version of T0, such as
data T a b = C a a | D b Int
deriveSafeCopy 2 'extension ''T
instance SafeCopy b => Migrate (T a b) where
type MigrateFrom (T a b) = T0 b
migrate (T0 b i) = D b i
we generate
instance (SafeCopy a, SafeCopy b) =>
SafeCopy (T a b) where
putCopy (C arg1 arg2) = contain $ do putWord8 0
put_a <- getSafePut
put_a arg1
put_a arg2
return ()
putCopy (D arg1 arg2) = contain $ do putWord8 1
put_b <- getSafePut
put_Int <- getSafePut
put_b arg1
put_Int arg2
return ()
getCopy = contain $ do tag <- getWord8
case tag of
0 -> do get_a <- getSafeGet
return C <*> get_a <*> get_a
1 -> do get_b <- getSafeGet
get_Int <- getSafeGet
return D <*> get_b <*> get_Int
_ -> fail $ "Could not identify tag \"" ++
show tag ++ "\" for type Main.T " ++
"that has only 2 constructors. " ++
"Maybe your data is corrupted?"
version = 2
kind = extension
Note that by using getSafePut, we saved 4 bytes in the case
of the C constructor. For D and T0, we didn't save
anything. The instance derived by this function always use
at most the same space as those generated by
deriveSafeCopySimple, but never more (as we don't call
getSafePut/getSafeGet for types that aren't needed).
Note that you may use deriveSafeCopySimple with one version of your data type and deriveSafeCopy in another version without any problems.
Derive an instance of SafeCopy. The instance derived by
this function should be compatible with the instance derived
by the module Happstack.Data.SerializeTH of the
happstack-data package. The instances use only safePut
and safeGet (as do the instances created by
deriveSafeCopySimple), but we also always write a Word8
tag, even if the data type isn't a sum type.
For example, given the data type and the declaration below
data T0 b = T0 b Int
deriveSafeCopy 1 'base ''T0
we generate
instance (SafeCopy a, SafeCopy b) =>
SafeCopy (T0 b) where
putCopy (T0 arg1 arg2) = contain $ do putWord8 0
safePut arg1
safePut arg2
return ()
getCopy = contain $ do tag <- getWord8
case tag of
0 -> do return T0 <*> safeGet <*> safeGet
_ -> fail $ "Could not identify tag \"" ++
show tag ++ "\" for type Main.T0 " ++
"that has only 1 constructors. " ++
"Maybe your data is corrupted?"
version = 1
kind = base
This instance always consumes at least the same space as deriveSafeCopy or deriveSafeCopySimple, but may use more because of the useless tag. So we recomend using it only if you really need to read a previous version in this format, and not for newer versions.
Note that you may use deriveSafeCopy with one version of your data type and deriveSafeCopyHappstackData in another version without any problems.
Derive an instance of SafeCopy. The instance derived by this function is simpler than the one derived by deriveSafeCopy in that we always use safePut and safeGet (instead of getSafePut and getSafeGet).
When serializing, we put a Word8 describing the
constructor (if the data type has more than one constructor)
and, for each field of the constructor, we use safePut.
For example, given the data type and the declaration below
data T a b = C a a | D b Int
deriveSafeCopySimple 1 'base ''T
we generate
instance (SafeCopy a, SafeCopy b) =>
SafeCopy (T a b) where
putCopy (C arg1 arg2) = contain $ do putWord8 0
safePut arg1
safePut arg2
return ()
putCopy (D arg1 arg2) = contain $ do putWord8 1
safePut arg1
safePut arg2
return ()
getCopy = contain $ do tag <- getWord8
case tag of
0 -> do return C <*> safeGet <*> safeGet
1 -> do return D <*> safeGet <*> safeGet
_ -> fail $ "Could not identify tag \"" ++
show tag ++ "\" for type Main.T " ++
"that has only 2 constructors. " ++
"Maybe your data is corrupted?"
version = 1
kind = base
Using this simpler instance means that you may spend more bytes when serializing data. On the other hand, it is more straightforward and may match any other format you used in the past.
Note that you may use deriveSafeCopy with one version of your data type and deriveSafeCopySimple in another version without any problems.
Follow type synonyms. This allows us to see, for example,
that [Char] and String are the same type and we just need
to call getSafePut or getSafeGet once for both.