Add raising `Defect` functions to AsyncQueue.

This commit is contained in:
cheatfate 2023-11-16 01:58:02 +02:00
parent b8d835b2bd
commit 3ef6ad5eaf
No known key found for this signature in database
GPG Key ID: 46ADD633A7201F95
1 changed files with 60 additions and 19 deletions

View File

@ -258,7 +258,7 @@ proc popLastImpl[T](aq: AsyncQueue[T]): T =
res
proc addFirstNoWait*[T](aq: AsyncQueue[T], item: T) {.
raises: [AsyncQueueFullError].}=
raises: [AsyncQueueFullError].} =
## Put an item ``item`` to the beginning of the queue ``aq`` immediately.
##
## If queue ``aq`` is full, then ``AsyncQueueFullError`` exception raised.
@ -267,7 +267,7 @@ proc addFirstNoWait*[T](aq: AsyncQueue[T], item: T) {.
aq.addFirstImpl(item)
proc addLastNoWait*[T](aq: AsyncQueue[T], item: T) {.
raises: [AsyncQueueFullError].}=
raises: [AsyncQueueFullError].} =
## Put an item ``item`` at the end of the queue ``aq`` immediately.
##
## If queue ``aq`` is full, then ``AsyncQueueFullError`` exception raised.
@ -275,6 +275,22 @@ proc addLastNoWait*[T](aq: AsyncQueue[T], item: T) {.
raise newException(AsyncQueueFullError, "AsyncQueue is full!")
aq.addLastImpl(item)
proc addFirstNoWaitSafe*[T](aq: AsyncQueue[T], item: T) {.
raises: [].} =
## Put an item ``item`` to the beginning of the queue ``aq`` immediately.
##
## If queue ``aq`` is full, then ``Defect`` exception raised.
doAssert(not(aq.full()), "AsyncQueue is full!")
aq.addFirstImpl(item)
proc addLastNoWaitSafe*[T](aq: AsyncQueue[T], item: T) {.
raises: [].} =
## Put an item ``item`` at the end of the queue ``aq`` immediately.
##
## If queue ``aq`` is full, then ``Defect`` exception raised.
doAssert(not(aq.full()), "AsyncQueue is full!")
aq.addLastImpl(item)
proc popFirstNoWait*[T](aq: AsyncQueue[T]): T {.
raises: [AsyncQueueEmptyError].} =
## Get an item from the beginning of the queue ``aq`` immediately.
@ -293,11 +309,29 @@ proc popLastNoWait*[T](aq: AsyncQueue[T]): T {.
raise newException(AsyncQueueEmptyError, "AsyncQueue is empty!")
aq.popLastImpl()
proc addFirst*[T](aq: AsyncQueue[T], item: T) {.async: (raises: [CancelledError]).} =
proc popFirstNoWaitSafe*[T](aq: AsyncQueue[T]): T {.
raises: [].} =
## Get an item from the beginning of the queue ``aq`` immediately.
##
## If queue ``aq`` is empty, then ``Defect`` raised.
doAssert(not(aq.empty()), "AsyncQueue is empty!")
aq.popLastImpl()
proc popLastNoWaitSafe*[T](aq: AsyncQueue[T]): T {.
raises: [].} =
## Get an item from the end of the queue ``aq`` immediately.
##
## If queue ``aq`` is empty, then ``Defect`` exception raised.
doAssert(not(aq.empty()), "AsyncQueue is empty!")
aq.popLastImpl()
proc addFirst*[T](aq: AsyncQueue[T], item: T) {.
async: (raises: [CancelledError]).} =
## Put an ``item`` to the beginning of the queue ``aq``. If the queue is full,
## wait until a free slot is available before adding item.
while aq.full():
let putter = Future[void].Raising([CancelledError]).init("AsyncQueue.addFirst")
let putter =
Future[void].Raising([CancelledError]).init("AsyncQueue.addFirst")
aq.putters.add(putter)
try:
await putter
@ -307,11 +341,13 @@ proc addFirst*[T](aq: AsyncQueue[T], item: T) {.async: (raises: [CancelledError]
raise exc
aq.addFirstImpl(item)
proc addLast*[T](aq: AsyncQueue[T], item: T) {.async: (raises: [CancelledError]).} =
proc addLast*[T](aq: AsyncQueue[T], item: T) {.
async: (raises: [CancelledError]).} =
## Put an ``item`` to the end of the queue ``aq``. If the queue is full,
## wait until a free slot is available before adding item.
while aq.full():
let putter = Future[void].Raising([CancelledError]).init("AsyncQueue.addLast")
let putter =
Future[void].Raising([CancelledError]).init("AsyncQueue.addLast")
aq.putters.add(putter)
try:
await putter
@ -321,11 +357,13 @@ proc addLast*[T](aq: AsyncQueue[T], item: T) {.async: (raises: [CancelledError])
raise exc
aq.addLastImpl(item)
proc popFirst*[T](aq: AsyncQueue[T]): Future[T] {.async: (raises: [CancelledError]).} =
proc popFirst*[T](aq: AsyncQueue[T]): Future[T] {.
async: (raises: [CancelledError]).} =
## Remove and return an ``item`` from the beginning of the queue ``aq``.
## If the queue is empty, wait until an item is available.
while aq.empty():
let getter = Future[void].Raising([CancelledError]).init("AsyncQueue.popFirst")
let getter =
Future[void].Raising([CancelledError]).init("AsyncQueue.popFirst")
aq.getters.add(getter)
try:
await getter
@ -335,11 +373,13 @@ proc popFirst*[T](aq: AsyncQueue[T]): Future[T] {.async: (raises: [CancelledErro
raise exc
aq.popFirstImpl()
proc popLast*[T](aq: AsyncQueue[T]): Future[T] {.async: (raises: [CancelledError]).} =
proc popLast*[T](aq: AsyncQueue[T]): Future[T] {.
async: (raises: [CancelledError]).} =
## Remove and return an ``item`` from the end of the queue ``aq``.
## If the queue is empty, wait until an item is available.
while aq.empty():
let getter = Future[void].Raising([CancelledError]).init("AsyncQueue.popLast")
let getter =
Future[void].Raising([CancelledError]).init("AsyncQueue.popLast")
aq.getters.add(getter)
try:
await getter
@ -509,7 +549,8 @@ proc close*(ab: AsyncEventQueue) {.raises: [].} =
ab.readers.reset()
ab.queue.clear()
proc closeWait*(ab: AsyncEventQueue): Future[void] {.async: (raw: true, raises: []).} =
proc closeWait*(ab: AsyncEventQueue): Future[void] {.
async: (raw: true, raises: []).} =
let retFuture = newFuture[void]("AsyncEventQueue.closeWait()",
{FutureFlag.OwnCancelSchedule})
proc continuation(udata: pointer) {.gcsafe.} =