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)


257
258
259
260
261
# File 'lib/farce/envelope.rb', line 257

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.



253
254
255
# File 'lib/farce/envelope.rb', line 253

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.



250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/farce/envelope.rb', line 250

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.



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

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.



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

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.



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

def owned? = true