~euantorano/once.nim

A Nim library that provides a type that will execute a callable only once.
Fix indentation
Add build status badge for debian with Nim devel to README
Fix URL for Nim

clone

read-only
https://git.sr.ht/~euantorano/once.nim
read/write
git@git.sr.ht:~euantorano/once.nim

You can also use your local clone with git send-email.

#once

Once is a Nim library that provides a type that will enforce that a callback is invoked only once.

#Installation

once can be installed using Nimble:

nimble install once

Or add the following to your .nimble file:

# Dependencies

requires "once >= 1.0.0"

#Usage

The tests directory has a couple of simple examples, but usage is briefly as follows:

import once

var i = 0

proc cb() =
  i += 1

var onceInstance = initOnce(cb)

for i in 0..5:
  onceInstance.run()

assert(i == 1)

Once also works in multi-threaded mode:

import once

var i = 0

proc cb() =
  i += 1

var
  onceInstance = initOnce(cb)
  thr: array[0..4, Thread[void]]

proc threadProc() {.thread.} =
  onceInstance.run()

for i in 0..high(thr):
  createThread(thr[i], threadProc)

joinThreads(thr)

assert(i == 1)

#CI Status

OS Status
Debian (Nim stable) builds.sr.ht status
Debian (Nim devel) builds.sr.ht status