Class: Farce::Envelope::Share

Inherits:
Farce::Envelope show all
Defined in:
lib/farce/envelope.rb

Overview

An envelope that wraps a Ractor-shareable value. Can be opened by any Ractor. Defeats the purpose of an envelope, but is provided for code that expects an envelope.

Constant Summary

Constants inherited from Farce::Envelope

AlreadyClaimed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Internal::Copyable

#duplicable?

Methods inherited from Farce::Envelope

#blank?, #claim!, #same_value?

Methods included from Abstract::Value

#blank?, #unwrap

Methods included from Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize(value) ⇒ Share

Returns a new instance of Share.

Parameters:

  • value (Object) —

    The value to wrap in the envelope. Must be shareable.

Raises:

  • (ArgumentError)


211
212
213
214
215
# File 'lib/farce/envelope.rb', line 211

def initialize(value, auto_unwrap = nil)
  raise ArgumentError, "value must be shareable" unless Ractor.shareable?(value)
  @value = value
  super
end

Instance Attribute Details

#value ⇒ BasicObject (readonly)

Returns the value of attribute value.



207
208
209
# File 'lib/farce/envelope.rb', line 207

def value
  @value
end

Class Method Details

.new(value) ⇒ Share

Returns A new envelope wrapping the given value.

Returns:

  • (Share) —

    A new envelope wrapping the given value.



204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/farce/envelope.rb', line 204

class Share < Farce::Envelope
  include Copyable

  attr_reader :value

  # @overload initialize(value)
  #   @param value [Object] The value to wrap in the envelope. Must be shareable.
  def initialize(value, auto_unwrap = nil)
    raise ArgumentError, "value must be shareable" unless Ractor.shareable?(value)
    @value = value
    super
  end

  # @api private
  def marshal_dump = [1, value, auto_unwrap]

  # @api private
  def marshal_load(data)
    value, manager = Internal::MarshalSupport.payload(data, 2)
    initialize(Ractor.make_shareable(value), manager)
  end

  # (see Envelope#claim)
  def claim = self

  # (see Envelope#claimed?)
  def claimed? = true

  # (see Envelope#owned?)
  def owned? = true
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.



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

def claim = self

#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.



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

def claimed? = true

#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.



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

def owned? = true