libp2p/utils/opt

Search:
Group by:
Source   Edit  

Procs

proc toOpt[T: ref object](x: T): Opt[T] {.inline.}
Source   Edit  
func toOpt[T](v: Opt[T] | T): Opt[T]
Source   Edit  

Macros

macro withValue[T](self: Opt[T]; value, body, elseStmt: untyped): untyped
Source   Edit  

Templates

template toOpt[T, E](self: Result[T, E]): Opt[T]
Source   Edit  
template withValue[T, E](self: Result[T, E]; value, body: untyped): untyped
Source   Edit  
template withValue[T](self: Opt[T]; value, body: untyped): untyped

This template provides a convenient way to work with Opt types in Nim. It allows you to execute a block of code (body) only when the Opt is not empty.

self is the Opt instance being checked. value is the variable name to be used within the body for the unwrapped value. body is a block of code that is executed only if self contains a value.

The value within body is automatically unwrapped from the Opt, making it simpler to work with without needing explicit checks or unwrapping.

Example:

let myOpt = Opt.some(5)
myOpt.withValue(value):
  echo value # Will print 5

Note: This is a template, and it will be inlined at the call site, offering good performance.

Source   Edit