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.



127
128
129
130
131
132
# File 'lib/farce/envelope.rb', line 127

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.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/farce/envelope.rb', line 121

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.



135
136
137
138
# File 'lib/farce/envelope.rb', line 135

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.



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

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.



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

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