Last active
January 3, 2016 14:29
-
-
Save mrzv/8476229 to your computer and use it in GitHub Desktop.
nimrod modules error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# --- serialize.nim --- | |
import streams | |
#import archive # works if I uncomment this line | |
proc `<<`*[TA,T] (a: TA, x: T): TA | |
proc `>>`*[TA,T] (a: TA, x: var T): TA | |
proc save*[TA,T](a: TA, x: seq[T]): TA = | |
discard a << len(x) | |
for v in x: | |
discard a << v | |
return a | |
proc load*[TA,T](a: TA, x: var seq[T]): TA = | |
var | |
length: int | |
v: T | |
discard a >> length | |
x = @[] | |
for i in 0..length-1: | |
discard a >> v | |
x.add(v) | |
return a | |
proc `<<`[TA,T] (a: TA, x: T): TA = | |
save(a, x) | |
proc `>>`[TA,T] (a: TA, x: var T): TA = | |
load(a, x) | |
# --- archive.nim --- | |
import streams | |
type | |
TBinArchive* = object | |
stream*: PStringStream | |
proc reset*(a: TBinArchive): void = | |
a.stream.setPosition(0) | |
# Save | |
proc save*(a: TBinArchive, x: int): TBinArchive = | |
a.stream.write(x) | |
return a | |
proc save*(a: TBinArchive, s: string): TBinArchive = | |
save(a, len(s)).stream.write(s) | |
return a | |
# Load | |
proc load*(a: TBinArchive, x: var int): TBinArchive = | |
discard a.stream.readData(addr(x), sizeof(int)) | |
return a | |
proc load*(a: TBinArchive, s: var string): TBinArchive = | |
var length: int | |
s = load(a, length).stream.readStr(length) | |
return a | |
# --- test.nim --- | |
import archive, serialize | |
import streams | |
when isMainModule: | |
var a: TBinArchive = TBinArchive(stream: newStringStream()) | |
var lst: seq[int] = @[2,3,4,5] | |
var f: float = 5.6 | |
discard a << "ABC" << 64 << lst | |
reset(a) | |
var s: string | |
var i: int | |
var lst2: seq[int] | |
discard a >> s >> i >> lst2 | |
echo s, " ", i | |
for v in lst2: | |
echo v |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment