Imported Upstream version 1.46.0
[platform/upstream/nghttp2.git] / third-party / mruby / mrbgems / mruby-enum-chain / mrblib / chain.rb
index 98515ea..43d0926 100644 (file)
@@ -6,28 +6,30 @@ module Enumerable
   def chain(*args)
     Enumerator::Chain.new(self, *args)
   end
+end
 
+class Enumerator
   def +(other)
-    Enumerator::Chain.new(self, other)
+    Chain.new(self, other)
   end
-end
 
-class Enumerator
   class Chain
     include Enumerable
 
     def initialize(*args)
-      @enums = args
-    end
-
-    def initialize_copy(orig)
-      @enums = orig.__copy_enums
+      @enums = args.freeze
+      @pos = -1
     end
 
     def each(&block)
-      return to_enum unless block_given?
+      return to_enum unless block
 
-      @enums.each { |e| e.each(&block) }
+      i = 0
+      while i < @enums.size
+        @pos = i
+        @enums[i].each(&block)
+        i += 1
+      end
 
       self
     end
@@ -40,21 +42,21 @@ class Enumerator
     end
 
     def rewind
-      @enums.reverse_each do |e|
+      while 0 <= @pos && @pos < @enums.size
+        e = @enums[@pos]
         e.rewind if e.respond_to?(:rewind)
+        @pos -= 1
       end
 
       self
     end
 
-    def inspect
-      "#<#{self.class}: #{@enums.inspect}>"
+    def +(other)
+      self.class.new(self, other)
     end
 
-    def __copy_enums
-      @enums.each_with_object([]) do |e, a|
-        a << e.clone
-      end
+    def inspect
+      "#<#{self.class}: #{@enums.inspect}>"
     end
   end
 end