class RLP::LazyList

A RLP encoded list which decodes itself when necessary.

Both indexing with positive indices and iterating are supported. Getting the length is possible as well but requires full horizontal encoding.

Attributes

sedes[RW]
sedes_options[RW]

Public Class Methods

new(rlp, start, next_start, sedes: nil, sedes_options: nil) click to toggle source

@param rlp [String] the rlp string in which the list is encoded @param start [Integer] the position of the first payload byte of the

encoded list

@param next_start [Integer] the position of the last payload byte of the

encoded list

@param sedes [Object] a sedes object which deserializes each element of the

list, or `nil` for on deserialization

@param sedes_options [Hash] keyword arguments which will be passed on to

the deserializer
# File lib/rlp/lazy_list.rb, line 26
def initialize(rlp, start, next_start, sedes: nil, sedes_options: nil)
  @rlp = rlp
  @start = start
  @next_start = next_start
  @index = start
  @elements = []
  @size = nil
  @sedes = sedes
  @sedes_options = sedes_options
end

Public Instance Methods

[](i) click to toggle source
# File lib/rlp/lazy_list.rb, line 63
def [](i)
  fetch(i, nil)
end
each(&block) click to toggle source
# File lib/rlp/lazy_list.rb, line 58
def each(&block)
  @elements.each(&block)
  loop { block.call(next_item) }
end
fetch(*args) click to toggle source
# File lib/rlp/lazy_list.rb, line 67
def fetch(*args)
  i = args[0]

  loop do
    raise StopIteration if @elements.size > i
    next_item
  end

  @elements.fetch(*args)
end
length()
Alias for: size
next_item() click to toggle source
# File lib/rlp/lazy_list.rb, line 37
def next_item
  if @index == @next_start
    @size = @elements.size
    raise StopIteration
  elsif @index < @next_start
    item, @index = consume_item_lazy @rlp, @index

    if @sedes
      # FIXME: lazy man's kwargs
      item = @sedes_options.empty? ?
               @sedes.deserialize(item) :
               @sedes.deserialize(item, **@sedes_options)
    end

    @elements.push item
    item
  else
    raise "Assertion failed: index cannot be larger than next start"
  end
end
size() click to toggle source
# File lib/rlp/lazy_list.rb, line 78
def size
  unless @size
    loop { next_item }
  end
  @size
end
Also aliased as: length