Support binding closure iterators (except on Nim == 2.0)

See nim-lang/Nim#22932.
This commit is contained in:
Nickolay Bukreyev 2023-11-14 14:27:12 +07:00 committed by markspanbroek
parent fe47a19825
commit cdf639c4ea
2 changed files with 20 additions and 3 deletions

View File

@ -2,12 +2,17 @@ import std/options
import std/macros
import ./private/binderror
when (NimMajor, NimMinor) < (1, 1):
type SomePointer = ref | ptr | pointer
elif (NimMajor, NimMinor) == (2, 0): # Broken in 2.0.0, fixed in 2.1.1.
type SomePointer = ref | ptr | pointer | proc
else:
type SomePointer = ref | ptr | pointer | proc | iterator {.closure.}
template toOption[T](option: Option[T]): Option[T] =
option
template toOption[T: ref | ptr | pointer | proc](value: T): Option[T] =
# `std/options` don't consider closure iterators to be pointer types
# (probably a bug) so we don't list them here.
template toOption[T: SomePointer](value: T): Option[T] =
value.option
proc placeholder(T: type): T =

View File

@ -187,6 +187,18 @@ suite "optionals":
if a =? p:
fail
when (NimMajor, NimMinor) >= (1, 1) and (NimMajor, NimMinor) != (2, 0):
var it = iterator: int = yield 2
if a =? it:
for x in a:
check x == 2
else:
fail
it = nil
if a =? it:
fail
test "=? rejects non-reference types":
check `not` compiles do:
if a =? 0: