class FDB::Future

def on_ready(&block)

def on_ready(&block)
  def callback_wrapper(f, &block)
    begin
      yield f
    rescue Exception
      begin
        $stderr.puts "Discarding uncaught exception from user callback:"
        $stderr.puts "#{$@.first}: #{$!.message} (#{$!.class})", $@.drop(1).map{|s| "\t#{s}"}
      rescue Exception
      end
    end
  end
  entry = CallbackEntry.new
  FDB.cb_mutex.synchronize {
    pos = FDB.ffi_callbacks.length
    entry.index = pos
    FDB.ffi_callbacks << entry
  }
  entry.callback = FFI::Function.new(:void, [:pointer, :pointer]) do |ign1, ign2|
    FDB.cb_mutex.synchronize {
      FDB.ffi_callbacks[-1].index = entry.index
      FDB.ffi_callbacks[entry.index] = FDB.ffi_callbacks[-1]
      FDB.ffi_callbacks.pop
    }
    callback_wrapper(self, &block)
  end
  FDBC.fdb_future_set_callback(@fpointer, entry.callback, nil)
end