I tried to get the following code to work, but for some reason, the object won't let me add more variants. I have no idea why this is happening, especially since the variant type doesn't contain any references. Please see the error below. Thanks in advance for your help.
Code:
import
std/typetraits,
assume/typeit
type
QueueHandle_t = int
UBaseType_t = int
# Mock implementations of the queue functions
proc xQueueCreate(len: UBaseType_t, size: UBaseType_t): QueueHandle_t =
return 1
proc containsRef*(_: seq): bool = true
proc containsRef*(_: ref): bool = true
proc containsRef*(_: string): bool = true
proc containsRef*(val: distinct): bool =
mixin containsRef
containsRef(val.distinctBase())
proc containsRef*[T](_: openArray[T]): bool =
mixin containsRef
containsRef(default(T))
proc containsRef*[T](_: T): bool = false
proc containsRef(t: object or tuple): bool =
typeit(t, {titAllFields}):
if containsRef(it):
return true
false
proc containsRef(T: typedesc): bool =
mixin containsRef
containsRef(default(T))
type NoRef = concept type C
not(containsRef(C))
type
Queue*[T: NoRef] = object
handle*: QueueHandle_t
QueueError* = object of IOError
QueueRecvError* = object of QueueError
QueueFullError* = object of QueueError
proc init*[V: NoRef](T: typedesc[Queue[V]], len: UBaseType_t): T =
result.handle = xQueueCreate(len, sizeof(T).UBaseType_t)
## Some usage examples below:
type
ValidObjectKind* = enum
voSomeVariant
voAnotherVariant
ValidObject = object
someParam: uint32
case kind*: ValidObjectKind
of voSomeVariant:
someVariant: uint32
of voAnotherVariant:
anotherVariant* : bool
var vQueue = Queue[ValidObject].init(10)
Error:
got: <typedesc[ValidObject]> but expected: <T: NoRef>
Calling containsRef manually shows the issue:
proc containsRef(t: object or tuple): bool =
typeit(t, {titAllFields}):
if containsRef(it):
return true
false
Attempts to access a field that depends on the kind:
proc containsRef(t: object or tuple): bool =
typeit(t, {titAllFields}):
if containsRef(typeof it):
return true
false
Resolves that.