Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict Future value #471

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions chronos/config.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@
## in transition periods leading up to a breaking release that starts enforcing
## them and removes the option.
##
## `chronosPreviewV4` is a preview flag to enable v4 semantics - in particular,
## it enables strict exception checking and disables parts of the deprecated
## API and other changes being prepared for the upcoming release
##
## `chronosDebug` can be defined to enable several debugging helpers that come
## with a runtime cost - it is recommeneded to not enable these in production
## code.
Expand All @@ -26,8 +22,6 @@ const
##
## `Exception` handling may be removed in future chronos versions.

chronosStrictFutureAccess* {.booldefine.}: bool = defined(chronosPreviewV4)

chronosStackTrace* {.booldefine.}: bool = defined(chronosDebug)
## Include stack traces in futures for creation and completion points

Expand Down
20 changes: 8 additions & 12 deletions chronos/futures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -209,31 +209,27 @@ func value*[T: not void](future: Future[T]): lent T =
##
## See `read` for a version that raises a catchable error when future
## has not completed.
when chronosStrictFutureAccess:
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)

future.internalValue

func value*(future: Future[void]) =
## Return the value in a completed future - raises Defect when
## `fut.completed()` is `false`.
## Raises Defect when `fut.completed()` is `false`.
##
## See `read` for a version that raises a catchable error when future
## has not completed.
when chronosStrictFutureAccess:
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)
if not future.completed():
raiseFutureDefect("Future not completed while accessing value", future)

func error*(future: FutureBase): ref CatchableError =
## Return the error of `future`, or `nil` if future did not fail.
##
## See `readError` for a version that raises a catchable error when the
## future has not failed.
when chronosStrictFutureAccess:
if not future.failed() and not future.cancelled():
raiseFutureDefect(
"Future not failed/cancelled while accessing error", future)
if not future.failed() and not future.cancelled():
raiseFutureDefect(
"Future not failed/cancelled while accessing error", future)

future.internalError

Expand Down
13 changes: 6 additions & 7 deletions chronos/internal/asyncfutures.nim
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,9 @@ proc tryCancel(future: FutureBase, loc: ptr SrcLoc): bool =
if not(isNil(future.internalChild)):
# If you hit this assertion, you should have used the `CancelledError`
# mechanism and/or use a regular `addCallback`
when chronosStrictFutureAccess:
doAssert future.internalCancelcb.isNil,
"futures returned from `{.async.}` functions must not use " &
"`cancelCallback`"
doAssert future.internalCancelcb.isNil,
"futures returned from `{.async.}` functions must not use " &
"`cancelCallback`"
tryCancel(future.internalChild, loc)
else:
if not(isNil(future.internalCancelcb)):
Expand Down Expand Up @@ -353,9 +352,9 @@ proc `cancelCallback=`*(future: FutureBase, cb: CallbackFunc) =
## This callback will be called immediately as ``future.cancel()`` invoked and
## must be set before future is finished.

when chronosStrictFutureAccess:
doAssert not future.finished(),
"cancellation callback must be set before finishing the future"
doAssert not future.finished(),
"cancellation callback must be set before finishing the future"

future.internalCancelcb = cb

{.push stackTrace: off.}
Expand Down