def cast(obj):
        """Cast :py:`obj` to an Amaranth value.

        Many :ref:`value-like <lang-valuelike>` objects can be cast to a value:

        * a :class:`Value` instance, where the result is itself;
        * a :class:`bool` or :class:`int` instance, where the result is :py:`Const(obj)`;
        * an :class:`enum.IntEnum` instance, or a :class:`enum.Enum` instance whose members are
          all integers, where the result is a :class:`Const(obj, enum_shape)` where :py:`enum_shape`
          is a shape that can represent every member of the enumeration;
        * a :class:`ValueCastable` instance, where the result is obtained by repeatedly calling
          :meth:`obj.as_value() <ValueCastable.as_value>`.

        Raises
        ------
        TypeError
            If :py:`obj` cannot be converted to a :class:`Value`.
        RecursionError
            If :py:`obj` is a :class:`ValueCastable` object that casts to itself.
        """
        while True:
            if isinstance(obj, Value):
                return obj
            elif isinstance(obj, ValueCastable):
                new_obj = obj.as_value()
            elif isinstance(obj, Enum):
                return Const(obj.value, Shape.cast(type(obj)))
            elif isinstance(obj, int):
                return Const(obj)
            else:
                raise TypeError(f"Object {obj!r} cannot be converted to an Amaranth value")
            if new_obj is obj:
                raise RecursionError(f"Value-castable object {obj!r} casts to itself")
            obj = new_obj
Cast :py:`obj` to an Amaranth value.

        Many :ref:`value-like <lang-valuelike>` objects can be cast to a value:

        * a :class:`Value` instance, where the result is itself;
        * a :class:`bool` or :class:`int` instance, where the result is :py:`Const(obj)`;
        * an :class:`enum.IntEnum` instance, or a :class:`enum.Enum` instance whose members are
          all integers, where the result is a :class:`Const(obj, enum_shape)` where :py:`enum_shape`
          is a shape that can represent every member of the enumeration;
        * a :class:`ValueCastable` instance, where the result is obtained by repeatedly calling
          :meth:`obj.as_value() <ValueCastable.as_value>`.

        Raises
        ------
        TypeError
            If :py:`obj` cannot be converted to a :class:`Value`.
        RecursionError
            If :py:`obj` is a :class:`ValueCastable` object that casts to itself.
        
amaranth/hdl/_ast.py