No description
Find a file
2021-10-20 14:19:41 +03:00
.gitignore Initial commit 2016-05-18 17:49:31 +03:00
.travis.yml Use different docker image for tests 2017-12-20 18:41:59 +02:00
LICENSE Initial commit 2016-05-18 17:49:31 +03:00
persistent_enums.nim Persistent enums are back 2021-10-20 14:19:41 +03:00
persistent_enums.nimble Persistent enums are back 2021-10-20 14:19:41 +03:00
README.md Persistent enums are back 2021-10-20 14:19:41 +03:00

persistent_enums Build Status

Define enums which values preserve their binary representation upon inserting or reordering

# Imagine you have the following enum
type MyEnum* {.persistent.} = enum
    myFirstValue = 0
    mySecondValue

# You may store enum value in binary form to somewhere:
var serializedVal = mySecondValue
writeInt16ToSomewhere(cast[ptr int16](addr serializedVal))
# Then next version of your app may add more values to the enum, e.g.
type MyEnum* {.persistent.} = enum
    myFirstValue = 0
    myNewlyInsertedValue
    mySecondValue

# Reading old binary value
var deserializedVal : MyEnum
readInt16FromSomewhere(cast[ptr int16](addr deserializedVal)

# With normal enums you would end up with deserializedVal == myNewlyInsertedValue
# But with persistent enum the following assertion will hold
assert(deserializedVal == mySecondValue)

Under the hood

There is no runtime cost. Binary represenations of enum values are actually equal to crc16 of their string representation, unless the value is explicitly set in enum definition. In case of collision a compile time error will be raised.