This commit is contained in:
Jaremy Creechley 2023-08-25 15:14:53 -07:00 committed by Dmitriy Ryajov
parent 0d4e6f203a
commit 768de3bc53
No known key found for this signature in database
GPG Key ID: DA8C680CE7C657A4

View File

@ -1,16 +1,17 @@
# import std/atomics
import threading/smartptrs
type
DataBufferHolder* = object
buf: ptr UncheckedArray[byte]
size: int
DataBuffer* = SharedPtr[DataBufferHolder]
DataBuffer* = SharedPtr[DataBufferHolder] ##\
## A fixed length data buffer using a SharedPtr.
## It is thread safe even with `refc` since
## it doesn't use string or seq types internally.
##
KeyBuffer* = DataBuffer
ValueBuffer* = DataBuffer
StringBuffer* = DataBuffer
@ -27,6 +28,7 @@ proc `=destroy`*(x: var DataBufferHolder) =
proc len*(a: DataBuffer): int = a[].size
proc new*(tp: typedesc[DataBuffer], size: int = 0): DataBuffer =
## allocate new buffer with given size
newSharedPtr(DataBufferHolder(
buf: cast[typeof(result[].buf)](allocShared0(size)),
size: size,
@ -40,18 +42,22 @@ proc new*[T: byte | char](tp: typedesc[DataBuffer], data: openArray[T]): DataBuf
copyMem(result[].buf, unsafeAddr data[0], data.len)
proc toSeq*[T: byte | char](a: DataBuffer, tp: typedesc[T]): seq[T] =
## convert buffer to a seq type using copy and either a byte or char
result = newSeq[T](a.len)
copyMem(addr result[0], unsafeAddr a[].buf[0], a.len)
proc toString*(data: DataBuffer): string =
## convert buffer to string type using copy
result = newString(data.len())
if data.len() > 0:
copyMem(addr result[0], unsafeAddr data[].buf[0], data.len)
proc toCatchable*(data: CatchableErrorBuffer): ref CatchableError =
result = (ref CatchableError)(msg: data.msg.toString())
proc toCatchable*(err: CatchableErrorBuffer): ref CatchableError =
## convert back to a ref CatchableError
result = (ref CatchableError)(msg: err.msg.toString())
proc toBuffer*(err: ref Exception): CatchableErrorBuffer =
## convert exception to an object with StringBuffer
return CatchableErrorBuffer(
msg: StringBuffer.new(err.msg)
)