https://github.com/yglukhov/persistent_enums
Define enums which values preserve their binary representation upon inserting or reordering
https://github.com/yglukhov/persistent_enums
Last synced: 7 months ago
JSON representation
Define enums which values preserve their binary representation upon inserting or reordering
- Host: GitHub
- URL: https://github.com/yglukhov/persistent_enums
- Owner: yglukhov
- License: mit
- Created: 2016-05-18T14:49:30.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2021-10-20T11:20:02.000Z (almost 4 years ago)
- Last Synced: 2024-10-14T15:04:46.739Z (12 months ago)
- Language: Nim
- Size: 4.88 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# persistent_enums [](https://travis-ci.org/yglukhov/persistent_enums)
Define enums which values preserve their binary representation upon inserting or reordering```nim
# 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))
```
```nim
# 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.