import struct


def encode_prev_length(length: int) -> bytes:
    """Special helper for zset previous entry lengths.

    If length < 253 then use 1 byte directly, otherwise
    set first byte to 254 and add 4 trailing bytes as an
    unsigned integer.
    """
    if length < 254:
        return struct.pack('B', length)
    return b'\xfe' + struct.pack("<I", length)