Class: Farce::Strict::Atom

Inherits:
Abstract::Atom show all
Includes:
Farce::Shareable::Delegated
Defined in:
lib/farce/strict/atom.rb

Overview

A Ractor-shareable atomic reference that strongly retains shareable values directly. Values are stored without envelope wrapping or transfer modes.

Examples:

Updating a counter atomically

counter = Farce::Strict::Atom.new(0)
counter.update { |count| count + 1 } # => 1
counter.compare_and_set(1, 2)       # => true
counter.value                      # => 2

Keeping an immutable snapshot

snapshot = [1, 2, 3].freeze
atom = Farce::Strict::Atom.new(snapshot)
atom.value.equal?(snapshot) # => true
atom.update { |items| (items + [4]).freeze } # => [1, 2, 3, 4]

Instance Method Summary collapse

Methods included from Farce::Shareable

#ractor_shareable?

Methods inherited from Abstract::Atom

#blank?, #compare_and_set, #compare_by_identity?, #get, #store, #store_if_absent, #swap, #update, #upsert, #value, #value=, #wait_until, #wait_until_changed, #wait_until_match, #wait_until_non_nil, #wait_until_value, #wait_while, #wait_while_match, #wait_while_value

Methods included from Internal::ValueSerialization

#as_extended_json, #as_json, #to_bson, #to_bson_normalized_value, #to_cbor, #to_json, #to_msgpack

Methods included from Abstract::Value

#blank?, #unwrap, #value

Methods included from Internal::Copyable

#duplicable?

Constructor Details

#initialize(value = nil, compare_by_identity: false) ⇒ Atom

Returns a new instance of Atom.

Parameters:

  • value (BasicObject, nil) (defaults to: nil) —

    the initial shareable value

  • compare_by_identity (Boolean) (defaults to: false) —

    whether comparisons use object identity instead of equality



26
27
28
29
# File 'lib/farce/strict/atom.rb', line 26

def initialize(value = nil, compare_by_identity: false)
  @atom = Internal::StrictAtom.new(value, compare_by_identity:)
  super()
end