Class: Farce::Envelope::Move

Inherits:
Farce::Envelope show all
Includes:
Shareable::Unfreezable, Internal::MarshalSupport::Reject
Defined in:
lib/farce/envelope.rb

Overview

An envelope that moves its contents. Can only be opened by a single Ractor. The value will be moved once when the envelope is created, and then once when the envelope is opened.

Constant Summary

Constants inherited from Farce::Envelope

AlreadyClaimed

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods inherited from Farce::Envelope

#blank?, #claim!, #same_value?, #value

Methods included from Abstract::Value

#blank?, #unwrap, #value

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize(value) ⇒ Move

Returns a new instance of Move.

Parameters:

  • value (Object) —

    The value to wrap in the envelope.



173
174
175
176
177
178
# File 'lib/farce/envelope.rb', line 173

def initialize(value, auto_unwrap = nil)
  @vault = VAULT_ATOM.store_if_absent { Internal::Vault.new }
  @owner = Internal::Atom.new
  super
  @vault.move_in(self, value)
end

Class Method Details

.new(value) ⇒ Move, Share

Returns A new envelope wrapping the given value.

Returns:

  • (Move, Share) —

    A new envelope wrapping the given value.



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/farce/envelope.rb', line 167

class Move < Farce::Envelope
  include Internal::MarshalSupport::Reject
  include Shareable::Unfreezable

  # @overload initialize(value)
  #   @param [Object] value The value to wrap in the envelope.
  def initialize(value, auto_unwrap = nil)
    @vault = VAULT_ATOM.store_if_absent { Internal::Vault.new }
    @owner = Internal::Atom.new
    super
    @vault.move_in(self, value)
  end

  # (see Envelope#claim)
  def claim
    owner = @owner.store_if_absent { Ractor.current }
    self if owner == Ractor.current
  end

  # (see Envelope#claimed?)
  def claimed? = !@owner.value.nil?

  # (see Envelope#owned?)
  def owned? = @owner.value == Ractor.current

  private def retrieve = @vault.move_out(self)
end

Instance Method Details

#claim ⇒ Envelope?

Attempts to claim the envelope for the current Ractor.

Returns:

  • (Envelope, nil) —

    The envelope if the claim was successful, or nil if the envelope has already been claimed by another Ractor.



181
182
183
184
# File 'lib/farce/envelope.rb', line 181

def claim
  owner = @owner.store_if_absent { Ractor.current }
  self if owner == Ractor.current
end

#claimed? ⇒ Boolean

Returns true if the envelope has been claimed by any Ractor, false otherwise.

Returns:

  • (Boolean) —

    true if the envelope has been claimed by any Ractor, false otherwise.



187
# File 'lib/farce/envelope.rb', line 187

def claimed? = !@owner.value.nil?

#owned? ⇒ Boolean

Returns true if the envelope has been claimed by the current Ractor, false otherwise.

Returns:

  • (Boolean) —

    true if the envelope has been claimed by the current Ractor, false otherwise.



190
# File 'lib/farce/envelope.rb', line 190

def owned? = @owner.value == Ractor.current