Class: Farce::Strict::Port

Inherits:
Internal::Port
  • Object
show all
Includes:
Abstract::Port
Defined in:
lib/farce/strict/port.rb

Overview

A Ractor-shareable port that accepts shareable messages directly. Messages retain their identity and envelopes are returned unopened. Only the creating Ractor can receive messages or close the port.

Examples:

Receive an already prepared message

port    = Farce::Strict::Port.new
message = [:ready, 42].freeze
port << message
port.receive.equal?(message) # => true
port.close

Instance Method Summary collapse

Methods included from Abstract::Port

#<<, #close, #closed?, #owned?, #pop, #push, #receive

Methods included from Farce::Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Constructor Details

#initialize ⇒ BasicObject

Creates a port without transfer modes.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/farce/strict/port.rb', line 20

class Port < Internal::Port
  include Abstract::Port

  # @!visibility private
  # (see #initialize)
  # CRuby 3.4's backend also accepts an existing port or Ractor.
  # Keep construction argument-free on every runtime.
  def self.new = super # rubocop:disable Lint/UselessMethodDefinition

  # (see Abstract::Port#send)
  # @raise [Farce::Ractor::IsolationError] if the message is not Ractor-shareable
  def send(message)
    raise Ractor::IsolationError, "value must be Ractor-shareable" unless Ractor.shareable?(message)
    super
  end

  # @return [String] a string representation of the port
  def inspect = super.sub(/\A#<.+? (?=(?:to|id):#?\d+)/, "#<Farce::Strict::Port ")
end

Instance Method Details

#inspect ⇒ String

Returns a string representation of the port.

Returns:

  • (String) —

    a string representation of the port



37
# File 'lib/farce/strict/port.rb', line 37

def inspect = super.sub(/\A#<.+? (?=(?:to|id):#?\d+)/, "#<Farce::Strict::Port ")

#send(message) ⇒ self

Sends a message through the port.

Parameters:

  • message (BasicObject) —

    the message to send

Returns:

  • (self)

Raises:



31
32
33
34
# File 'lib/farce/strict/port.rb', line 31

def send(message)
  raise Ractor::IsolationError, "value must be Ractor-shareable" unless Ractor.shareable?(message)
  super
end