An open API service indexing awesome lists of open source software.

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

Awesome Lists containing this project

README

          

# persistent_enums [![Build Status](https://travis-ci.org/yglukhov/persistent_enums.svg?branch=master)](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.