mirror of
https://github.com/logos-storage/questionable.git
synced 2026-01-02 13:53:11 +00:00
Support binding closure iterators (except on Nim == 2.0)
See nim-lang/Nim#22932.
This commit is contained in:
parent
fe47a19825
commit
cdf639c4ea
@ -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 =
|
||||
|
||||
@ -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:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user