isolation.nim 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2020 Nim contributors
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module implements the `Isolated[T]` type for
  10. ## safe construction of isolated subgraphs that can be
  11. ## passed efficiently to different channels and threads.
  12. ##
  13. ## .. warning:: This module is experimental and its interface may change.
  14. ##
  15. type
  16. Isolated*[T] = object ## Isolated data can only be moved, not copied.
  17. value: T
  18. proc `=copy`*[T](dest: var Isolated[T]; src: Isolated[T]) {.error.}
  19. proc `=sink`*[T](dest: var Isolated[T]; src: Isolated[T]) {.inline.} =
  20. # delegate to value's sink operation
  21. `=sink`(dest.value, src.value)
  22. proc `=destroy`*[T](dest: var Isolated[T]) {.inline.} =
  23. # delegate to value's destroy operation
  24. `=destroy`(dest.value)
  25. func isolate*[T](value: sink T): Isolated[T] {.magic: "Isolate".} =
  26. ## Creates an isolated subgraph from the expression `value`.
  27. ## Isolation is checked at compile time.
  28. ##
  29. ## Please read https://github.com/nim-lang/RFCs/issues/244
  30. ## for more details.
  31. Isolated[T](value: value)
  32. func unsafeIsolate*[T](value: sink T): Isolated[T] =
  33. ## Creates an isolated subgraph from the expression `value`.
  34. ##
  35. ## .. warning:: The proc doesn't check whether `value` is isolated.
  36. ##
  37. Isolated[T](value: value)
  38. func extract*[T](src: var Isolated[T]): T =
  39. ## Returns the internal value of `src`.
  40. ## The value is moved from `src`.
  41. result = move(src.value)