summary refs log tree commit diff stats
path: root/lib/std/isolation.nim
blob: 7d6ac60928f0655e41a24fc7dcd36d2a4859c819 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#
#
#            Nim's Runtime Library
#        (c) Copyright 2020 Nim contributors
#
#    See the file "copying.txt", included in this
#    distribution, for details about the copyright.
#

## This module implements the `Isolated[T]` type for
## safe construction of isolated subgraphs that can be
## passed efficiently to different channels and threads.
##
## .. warning:: This module is experimental and its interface may change.
##

type
  Isolated*[T] = object ## Isolated data can only be moved, not copied.
    value: T

proc `=copy`*[T](dest: var Isolated[T]; src: Isolated[T]) {.error.}

proc `=sink`*[T](dest: var Isolated[T]; src: Isolated[T]) {.inline.} =
  # delegate to value's sink operation
  `=sink`(dest.value, src.value)

proc `=destroy`*[T](dest: var Isolated[T]) {.inline.} =
  # delegate to value's destroy operation
  `=destroy`(dest.value)

func isolate*[T](value: sink T): Isolated[T] {.magic: "Isolate".} =
  ## Creates an isolated subgraph from the expression `value`.
  ## Isolation is checked at compile time.
  ##
  ## Please read https://github.com/nim-lang/RFCs/issues/244
  ## for more details.
  Isolated[T](value: value)

func unsafeIsolate*[T](value: sink T): Isolated[T] =
  ## Creates an isolated subgraph from the expression `value`.
  ## 
  ## .. warning:: The proc doesn't check whether `value` is isolated.
  ## 
  Isolated[T](value: value)

func extract*[T](src: var Isolated[T]): T =
  ## Returns the internal value of `src`.
  ## The value is moved from `src`.
  result = move(src.value)