class Async::IO

Represents an asynchronous IO within a reactor.

def self.[] instance

def self.[] instance
	WRAPPERS[instance.class]
end

def async

def async
	while true
		begin
			result = yield
			
			case result
			when :wait_readable
				wait_readable
			when :wait_writable
				wait_writable
			else
				return result
			end
		rescue ::IO::WaitReadable
			wait_readable
		rescue ::IO::WaitWritable
			wait_writable
		end
	end
end

def wrap_blocking_method(new_name, method_name)

def wrap_blocking_method(new_name, method_name)
	# puts "#{self}\##{$1} -> #{method_name}"
	define_method(new_name) do |*args|
		async do
			@io.__send__(method_name, *args, exception: false)
		end
	end
end

def wrap_blocking_method(new_name, method_name)

def wrap_blocking_method(new_name, method_name)
	# puts "#{self}\##{$1} -> #{method_name}"
	define_method(new_name) do |*args|
		async do
			@io.__send__(method_name, *args)
		end
	end
end

def wraps(klass, *additional_methods)

def wraps(klass, *additional_methods)
	WRAPPERS[klass] = self
	
	klass.instance_methods(false).grep(/(.*)_nonblock/) do |method_name|
		wrap_blocking_method($1, method_name)
	end
	
	def_delegators :@io, *(additional_methods - instance_methods(false))
end