Module: AsyncFutures::IOSync

Defined in:
lib/async_futures/io_async.rb

Overview

Simple mixin for sync IO with an async interface.

Instance Method Summary collapse

Instance Method Details

#read_async(maxlen, *args, **kwargs) ⇒ Object

Return a completed future containing a string up to maxlen bytes long.

This exists for classes such as StringIO to maintain compatibility with classes with true nonblocking methods (such as IO).

There is no performance benefit to calling this instead of directly calling read. In fact, there may be a slight performance degradation because of the added overhead of instantiating and completing a Future object.

You should only use this method if you are dealing with a mix of IO, OpenSSL::SSLSocket, and/or StringIO objects and want to interact with them identically in a nonblocking manner. Or you may want to use this method with StringIO as a type of mock object for testing in place of real IO or OpenSSL::SSLSocket objects.



301
302
303
304
305
# File 'lib/async_futures/io_async.rb', line 301

def read_async(maxlen, *args, **kwargs) # rubocop:disable Lint/UnusedMethodArgument
  Future.new.tap do |ftr|
    ftr.complete(maxlen, &method(:read))
  end
end

#write_async(string, *args, **kwargs) ⇒ Object

Return a completed future containing an integer with the number of bytes written.

This exists for classes such as StringIO to maintain compatibility with classes with true nonblocking methods (such as IO).

There is no performance benefit to calling this instead of directly calling write. In fact, there may be a slight performance degradation because of the added overhead of instantiating and completing a Future object.

You should only use this method if you are dealing with a mix of IO, OpenSSL::SSLSocket, and/or StringIO objects and want to interact with them identically in a nonblocking manner. Or you may want to use this method with StringIO as a type of mock object for testing in place of real IO or OpenSSL::SSLSocket objects.



276
277
278
279
280
# File 'lib/async_futures/io_async.rb', line 276

def write_async(string, *args, **kwargs) # rubocop:disable Lint/UnusedMethodArgument
  Future.new.tap do |ftr|
    ftr.complete(string, &method(:write))
  end
end