def drain_to(self, list, max_size=-1):
        """
        Transfers all available items to the given `list`_ and removes these items from this queue. If a max_size is
        specified, it transfers at most the given number of items. In case of a failure, an item can exist in both
        collections or none of them.

        This operation may be more efficient than polling elements repeatedly and putting into collection.

        :param list: (`list`_), the list where the items in this queue will be transferred.
        :param max_size: (int), the maximum number items to transfer (optional).
        :return: (int), number of transferred items.

        .. _list: https://docs.python.org/2/library/functions.html#list
        """
        def drain_result(f):
            resp = f.result()
            list.extend(resp)
            return len(resp)

        return self._encode_invoke(queue_drain_to_max_size_codec, max_size=max_size).continue_with(
            drain_result)