Skip to content

Commit

Permalink
Use inline per proc and tag lent where appropriate
Browse files Browse the repository at this point in the history
  • Loading branch information
mratsim committed May 21, 2020
1 parent e03513e commit fc87a08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 19 deletions.
Binary file added build.out
Binary file not shown.
35 changes: 16 additions & 19 deletions lib/pure/options.nim
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ type
UnpackDefect* = object of Defect
UnpackError* {.deprecated: "See corresponding Defect".} = UnpackDefect

{.push inline.}

proc option*[T](val: T): Option[T] =
proc option*[T](val: T): Option[T] {.inline.} =
## Can be used to convert a pointer type (`ptr` or `ref` or `proc`) to an option type.
## It converts `nil` to `None`.
##
Expand All @@ -100,7 +98,7 @@ proc option*[T](val: T): Option[T] =
when T isnot SomePointer:
result.has = true

proc some*[T](val: T): Option[T] =
proc some*[T](val: T): Option[T] {.inline.} =
## Returns an `Option` that has the value `val`.
##
## See also:
Expand All @@ -123,7 +121,7 @@ proc some*[T](val: T): Option[T] =
result.has = true
result.val = val

proc none*(T: typedesc): Option[T] =
proc none*(T: typedesc): Option[T] {.inline.} =
## Returns an `Option` for this type that has no value.
##
## See also:
Expand All @@ -138,11 +136,11 @@ proc none*(T: typedesc): Option[T] =
# the default is the none type
discard

proc none*[T]: Option[T] =
proc none*[T]: Option[T] {.inline.} =
## Alias for `none(T) proc <#none,typedesc>`_.
none(T)

proc isSome*[T](self: Option[T]): bool =
proc isSome*[T](self: Option[T]): bool {.inline.} =
## Checks if an `Option` contains a value.
runnableExamples:
var
Expand All @@ -156,7 +154,7 @@ proc isSome*[T](self: Option[T]): bool =
else:
self.has

proc isNone*[T](self: Option[T]): bool =
proc isNone*[T](self: Option[T]): bool {.inline.} =
## Checks if an `Option` is empty.
runnableExamples:
var
Expand All @@ -169,7 +167,7 @@ proc isNone*[T](self: Option[T]): bool =
else:
not self.has

proc get*[T](self: Option[T]): T =
proc get*[T](self: Option[T]): lent T {.inline.} =
## Returns contents of an `Option`. If it is `None`, then an exception is
## thrown.
##
Expand All @@ -187,7 +185,7 @@ proc get*[T](self: Option[T]): T =
raise newException(UnpackError, "Can't obtain a value from a `none`")
self.val

proc get*[T](self: Option[T], otherwise: T): T =
proc get*[T](self: Option[T], otherwise: T): T {.inline.} =
## Returns the contents of the `Option` or an `otherwise` value if
## the `Option` is `None`.
runnableExamples:
Expand All @@ -202,7 +200,7 @@ proc get*[T](self: Option[T], otherwise: T): T =
else:
otherwise

proc get*[T](self: var Option[T]): var T =
proc get*[T](self: var Option[T]): var T {.inline.} =
## Returns contents of the `var Option`. If it is `None`, then an exception
## is thrown.
runnableExamples:
Expand All @@ -217,7 +215,7 @@ proc get*[T](self: var Option[T]): var T =
raise newException(UnpackError, "Can't obtain a value from a `none`")
return self.val

proc map*[T](self: Option[T], callback: proc (input: T)) =
proc map*[T](self: Option[T], callback: proc (input: T)) {.inline.} =
## Applies a `callback` function to the value of the `Option`, if it has one.
##
## See also:
Expand All @@ -241,7 +239,7 @@ proc map*[T](self: Option[T], callback: proc (input: T)) =
if self.isSome:
callback(self.val)

proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] =
proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] {.inline.} =
## Applies a `callback` function to the value of the `Option` and returns an
## `Option` containing the new value.
##
Expand All @@ -268,7 +266,7 @@ proc map*[T, R](self: Option[T], callback: proc (input: T): R): Option[R] =
else:
none(R)

proc flatten*[A](self: Option[Option[A]]): Option[A] =
proc flatten*[A](self: Option[Option[A]]): Option[A] {.inline.} =
## Remove one level of structure in a nested `Option`.
runnableExamples:
let a = some(some(42))
Expand All @@ -280,7 +278,7 @@ proc flatten*[A](self: Option[Option[A]]): Option[A] =
none(A)

proc flatMap*[A, B](self: Option[A],
callback: proc (input: A): Option[B]): Option[B] =
callback: proc (input: A): Option[B]): Option[B] {.inline.} =
## Applies a `callback` function to the value of the `Option` and returns an
## `Option` containing the new value.
##
Expand Down Expand Up @@ -310,7 +308,7 @@ proc flatMap*[A, B](self: Option[A],

map(self, callback).flatten()

proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] {.inline.} =
## Applies a `callback` to the value of the `Option`.
##
## If the `callback` returns `true`, the option is returned as `Some`.
Expand All @@ -335,7 +333,7 @@ proc filter*[T](self: Option[T], callback: proc (input: T): bool): Option[T] =
else:
self

proc `==`*(a, b: Option): bool =
proc `==`*(a, b: Option): bool {.inline.} =
## Returns `true` if both `Option`s are `None`,
## or if they are both `Some` and have equal values.
runnableExamples:
Expand Down Expand Up @@ -365,7 +363,7 @@ proc `$`*[T](self: Option[T]): string =
else:
result = "None[" & name(T) & "]"

proc unsafeGet*[T](self: Option[T]): T =
proc unsafeGet*[T](self: Option[T]): lent T {.inline.}=
## Returns the value of a `some`. Behavior is undefined for `none`.
##
## **Note:** Use it only when you are **absolutely sure** the value is present
Expand All @@ -374,7 +372,6 @@ proc unsafeGet*[T](self: Option[T]): T =
assert self.isSome
self.val

{.pop.}

when isMainModule:
import unittest, sequtils
Expand Down

0 comments on commit fc87a08

Please sign in to comment.