Module: Farce::Abstract::Port Abstract

Includes:
Internal::Noncopyable, Shareable::Native, Shareable::Unfreezable, Internal::MarshalSupport::Reject
Included in:
Port, Strict::Port
Defined in:
lib/farce/abstract/port.rb

Overview

This module is abstract.

Common interface and ownership tracking for Farce ports.

Note:

This is a module so both implementations can inherit directly from the runtime's port backend.

Instance Method Summary collapse

Methods included from Shareable

#ractor_shareable?

Methods included from Internal::Noncopyable

#duplicable?

Instance Method Details

#<< ⇒ self

Forwards all arguments to #send.

Returns:

  • (self)


48
# File 'lib/farce/abstract/port.rb', line 48

def <<(...) = send(...)

#close ⇒ self

Closes the port. Only the owning Ractor can close it.

Returns:

  • (self)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/farce/abstract/port.rb', line 29

module Port
  include Internal::MarshalSupport::Reject
  include Internal::Noncopyable
  include Shareable::Native
  include Shareable::Unfreezable

  # @api private
  def initialize(...)
    # Native Ractor ports cannot hold Ruby instance variables.
    Internal::Storage.ractor[self] = true
    super
  end

  # Checks whether the current Ractor created this port.
  # @return [Boolean] whether the current Ractor owns the port
  def owned? = !!Internal::Storage.ractor[self]

  # Forwards all arguments to {#send}.
  # @return [self]
  def <<(...) = send(...)

  # Forwards all arguments to {#send}.
  # @return [self]
  def push(...) = send(...)

  # Forwards all arguments to {#receive}.
  # @return [BasicObject, nil] the received message, or nil on timeout
  def pop(...) = receive(...)

  # @api private
  def pretty_print(pp)
    string = inspect
    return pp.text(string) unless match = string.match(/\A#<([\w:]+)((?: \w+:#?\w+)+)>\z/)

    pp.group(1, "#<#{match[1]}", ">") do
      match[2].split.each do |pair|
        pp.breakable " "
        key, value = pair.split(":", 2)
        pp.text "#{key}:"
        pp.text value
      end
    end
  end
end

#closed? ⇒ Boolean

Returns whether the port is closed.

Returns:

  • (Boolean) —

    whether the port is closed



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/farce/abstract/port.rb', line 29

module Port
  include Internal::MarshalSupport::Reject
  include Internal::Noncopyable
  include Shareable::Native
  include Shareable::Unfreezable

  # @api private
  def initialize(...)
    # Native Ractor ports cannot hold Ruby instance variables.
    Internal::Storage.ractor[self] = true
    super
  end

  # Checks whether the current Ractor created this port.
  # @return [Boolean] whether the current Ractor owns the port
  def owned? = !!Internal::Storage.ractor[self]

  # Forwards all arguments to {#send}.
  # @return [self]
  def <<(...) = send(...)

  # Forwards all arguments to {#send}.
  # @return [self]
  def push(...) = send(...)

  # Forwards all arguments to {#receive}.
  # @return [BasicObject, nil] the received message, or nil on timeout
  def pop(...) = receive(...)

  # @api private
  def pretty_print(pp)
    string = inspect
    return pp.text(string) unless match = string.match(/\A#<([\w:]+)((?: \w+:#?\w+)+)>\z/)

    pp.group(1, "#<#{match[1]}", ">") do
      match[2].split.each do |pair|
        pp.breakable " "
        key, value = pair.split(":", 2)
        pp.text "#{key}:"
        pp.text value
      end
    end
  end
end

#owned? ⇒ Boolean

Checks whether the current Ractor created this port.

Returns:

  • (Boolean) —

    whether the current Ractor owns the port



44
# File 'lib/farce/abstract/port.rb', line 44

def owned? = !!Internal::Storage.ractor[self]

#pop ⇒ BasicObject?

Forwards all arguments to #receive.

Returns:

  • (BasicObject, nil) —

    the received message, or nil on timeout



56
# File 'lib/farce/abstract/port.rb', line 56

def pop(...) = receive(...)

#push ⇒ self

Forwards all arguments to #send.

Returns:

  • (self)


52
# File 'lib/farce/abstract/port.rb', line 52

def push(...) = send(...)

#receive(timeout: nil) ⇒ BasicObject?

Waits for a message or the timeout. Only the owning Ractor can receive.

Parameters:

  • timeout (Numeric, nil) (defaults to: nil) —

    the maximum seconds to wait, or nil to wait indefinitely

Returns:

  • (BasicObject, nil) —

    the received message, or nil on timeout

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/farce/abstract/port.rb', line 29

module Port
  include Internal::MarshalSupport::Reject
  include Internal::Noncopyable
  include Shareable::Native
  include Shareable::Unfreezable

  # @api private
  def initialize(...)
    # Native Ractor ports cannot hold Ruby instance variables.
    Internal::Storage.ractor[self] = true
    super
  end

  # Checks whether the current Ractor created this port.
  # @return [Boolean] whether the current Ractor owns the port
  def owned? = !!Internal::Storage.ractor[self]

  # Forwards all arguments to {#send}.
  # @return [self]
  def <<(...) = send(...)

  # Forwards all arguments to {#send}.
  # @return [self]
  def push(...) = send(...)

  # Forwards all arguments to {#receive}.
  # @return [BasicObject, nil] the received message, or nil on timeout
  def pop(...) = receive(...)

  # @api private
  def pretty_print(pp)
    string = inspect
    return pp.text(string) unless match = string.match(/\A#<([\w:]+)((?: \w+:#?\w+)+)>\z/)

    pp.group(1, "#<#{match[1]}", ">") do
      match[2].split.each do |pair|
        pp.breakable " "
        key, value = pair.split(":", 2)
        pp.text "#{key}:"
        pp.text value
      end
    end
  end
end

#send(message) ⇒ self

Sends a message through the port.

Parameters:

  • message (BasicObject) —

    the message to send

Returns:

  • (self)

Raises:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/farce/abstract/port.rb', line 29

module Port
  include Internal::MarshalSupport::Reject
  include Internal::Noncopyable
  include Shareable::Native
  include Shareable::Unfreezable

  # @api private
  def initialize(...)
    # Native Ractor ports cannot hold Ruby instance variables.
    Internal::Storage.ractor[self] = true
    super
  end

  # Checks whether the current Ractor created this port.
  # @return [Boolean] whether the current Ractor owns the port
  def owned? = !!Internal::Storage.ractor[self]

  # Forwards all arguments to {#send}.
  # @return [self]
  def <<(...) = send(...)

  # Forwards all arguments to {#send}.
  # @return [self]
  def push(...) = send(...)

  # Forwards all arguments to {#receive}.
  # @return [BasicObject, nil] the received message, or nil on timeout
  def pop(...) = receive(...)

  # @api private
  def pretty_print(pp)
    string = inspect
    return pp.text(string) unless match = string.match(/\A#<([\w:]+)((?: \w+:#?\w+)+)>\z/)

    pp.group(1, "#<#{match[1]}", ">") do
      match[2].split.each do |pair|
        pp.breakable " "
        key, value = pair.split(":", 2)
        pp.text "#{key}:"
        pp.text value
      end
    end
  end
end