Add vtableAddr as a reusable helper for working around a Nim issue
This commit is contained in:
parent
9da65e792e
commit
fa415774c3
1 changed files with 16 additions and 6 deletions
|
|
@ -23,12 +23,15 @@ type
|
||||||
OutputStream* = ref OutputStreamObj
|
OutputStream* = ref OutputStreamObj
|
||||||
OutputStreamVar = OutputStream
|
OutputStreamVar = OutputStream
|
||||||
|
|
||||||
OutputStreamVTable* = object
|
WritePageProc* = proc (s: OutputStream, page: openarray[byte])
|
||||||
writePage*: proc (s: OutputStream, page: openarray[byte])
|
{.nimcall, gcsafe, raises: [IOError, Defect].}
|
||||||
{.nimcall, gcsafe, raises: [IOError, Defect].}
|
|
||||||
|
|
||||||
flush*: proc (s: OutputStream)
|
FlushProc* = proc (s: OutputStream)
|
||||||
{.nimcall, gcsafe, raises: [IOError, Defect].}
|
{.nimcall, gcsafe, raises: [IOError, Defect].}
|
||||||
|
|
||||||
|
OutputStreamVTable* = object
|
||||||
|
writePage*: WritePageProc
|
||||||
|
flush*: FlushProc
|
||||||
|
|
||||||
WriteCursor* = object
|
WriteCursor* = object
|
||||||
head, bufferEnd: ptr byte
|
head, bufferEnd: ptr byte
|
||||||
|
|
@ -107,12 +110,19 @@ let FileStreamVTable = OutputStreamVTable(
|
||||||
flushFile output.file
|
flushFile output.file
|
||||||
)
|
)
|
||||||
|
|
||||||
|
template vtableAddr*(vtable: OutputStreamVTable): ptr OutputStreamVTable =
|
||||||
|
## This is a simple work-around for the somewhat broken side
|
||||||
|
## effects analysis of Nim - reading from global let variables
|
||||||
|
## is considered a side-effect.
|
||||||
|
{.noSideEffect.}:
|
||||||
|
unsafeAddr vtable
|
||||||
|
|
||||||
proc init*(T: type OutputStream,
|
proc init*(T: type OutputStream,
|
||||||
filename: string,
|
filename: string,
|
||||||
pageSize = defaultPageSize): OutputStream =
|
pageSize = defaultPageSize): OutputStream =
|
||||||
implementOutputStream pageSize,
|
implementOutputStream pageSize,
|
||||||
outputDevice = FileOutput(file: open(filename, fmWrite)),
|
outputDevice = FileOutput(file: open(filename, fmWrite)),
|
||||||
vtable = unsafeAddr FileStreamVTable
|
vtable = vtableAddr FileStreamVTable
|
||||||
|
|
||||||
proc init*(T: type OutputStream,
|
proc init*(T: type OutputStream,
|
||||||
buffer: pointer, len: int): OutputStream =
|
buffer: pointer, len: int): OutputStream =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue