Class: Farce::Envelope::Marshal

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

Overview

An envelope that serializes its contents with Ruby's Marshal protocol. The snapshot is created immediately and reconstructed once per Ractor that opens it. Farce containers rebuild their storage from logical snapshots. Contents must support Marshal. Nested shareable values may be reconstructed too.

Examples:

Transfer an Unshared map

source = Farce::Unshared::Map.new({ jobs: [] })
envelope = Farce::Envelope.new(source, mode: :marshal)
envelope.value[:jobs] << :ready
source[:jobs] # => []

Constant Summary

Constants inherited from Farce::Envelope

AlreadyClaimed

Instance Method Summary collapse

Methods included from Internal::Copyable

#duplicable?

Methods inherited from Farce::Envelope

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

Methods included from Abstract::Value

#blank?, #unwrap, #value

Methods included from Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize(value, auto_unwrap = nil) ⇒ Marshal

Returns a new instance of Marshal.

Parameters:

  • value (BasicObject) —

    the value to serialize

  • auto_unwrap (ModeManager, nil) (defaults to: nil) —

    the manager that may automatically open this envelope

Raises:

  • (TypeError) —

    if the value cannot be marshaled



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

def initialize(value, auto_unwrap = nil)
  @dump = ::Marshal.dump(value).freeze
  super
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.



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

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.



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

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.



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

def owned? = true