import struct


def dump_tensor_protos(protos):
    """
    Serialize a list of :class:`TensorProto`, for communication between custom TensorFlow ops.

    Args:
        protos (list): list of :class:`TensorProto` instance

    Notes:
        The format is:

        [#tensors(int32)]
        (tensor1)[size of meta proto][serialized meta proto][size of buffer][buffer]
        (tensor2)...
    """

    s = struct.pack('=i', len(protos))
    for p in protos:
        tensor_content = p.tensor_content
        p.tensor_content = b'xxx'   # clear content
        buf = p.SerializeToString()
        s += struct.pack('=i', len(buf))
        s += buf
        s += struct.pack('=i', len(tensor_content))    # won't send stuff over 2G
        s += tensor_content
    return s