def __getitem__(self, key):
        """Slice the underlying value.

        A field corresponding to :py:`key` is looked up in the layout. If the field's shape is
        a shape-castable object that has a :meth:`~.ShapeCastable.__call__` method, it is called and
        the result is returned. Otherwise, :meth:`~.ShapeCastable.as_shape` is called repeatedly on
        the shape until either an object with a :meth:`~.ShapeCastable.__call__` method is reached,
        or a :class:`.Shape` is returned. In the latter case, returns an unspecified Amaranth
        expression with the right shape.

        Arguments
        ---------
        key : :class:`str` or :class:`int` or :class:`.ValueCastable`
            Name or index of a field.

        Returns
        -------
        :class:`.Value` or :class:`.ValueCastable`, :ref:`assignable <lang-assignable>`
            A slice of the underlying value defined by the field.

        Raises
        ------
        :exc:`KeyError`
            If the layout does not define a field corresponding to :py:`key`.
        :exc:`TypeError`
            If :py:`key` is a value-castable object, but the layout of the view is not
            an :class:`ArrayLayout`.
        :exc:`TypeError`
            If :meth:`.ShapeCastable.__call__` does not return a value or a value-castable object.
        """
        if isinstance(self.__layout, ArrayLayout):
            elem_width = Shape.cast(self.__layout.elem_shape).width
            if isinstance(key, slice):
                start, stop, stride = key.indices(self.__layout.length)
                shape = ArrayLayout(self.__layout.elem_shape, len(range(start, stop, stride)))
                if stride == 1:
                    value = self.__target[start * elem_width:stop * elem_width]
                else:
                    value = Cat(self.__target[index * elem_width:(index + 1) * elem_width]
                                for index in range(start, stop, stride))
            elif isinstance(key, int):
                if key not in range(-self.__layout.length, self.__layout.length):
                    raise IndexError(f"Index {key} is out of range for array layout of length "
                                     f"{self.__layout.length}")
                if key < 0:
                    key += self.__layout.length
                shape = self.__layout.elem_shape
                value = self.__target[key * elem_width:(key + 1) * elem_width]
            elif isinstance(key, (Value, ValueCastable)):
                shape = self.__layout.elem_shape
                value = self.__target.word_select(key, elem_width)
            else:
                raise TypeError(
                    f"View with array layout may only be indexed with an integer or a value, "
                    f"not {key!r}")
        else:
            if isinstance(key, slice):
                raise TypeError(
                    "Non-array view cannot be indexed with a slice; did you mean to call "
                    "`.as_value()` first?")
            if isinstance(key, (Value, ValueCastable)):
                raise TypeError(
                    f"Only views with array layout, not {self.__layout!r}, may be indexed with "
                    f"a value")
            field = self.__layout[key]
            shape = field.shape
            value = self.__target[field.offset:field.offset + field.width]
        # Field guarantees that the shape-castable object is well-formed, so there is no need
        # to handle erroneous cases here.
        if isinstance(shape, ShapeCastable):
            value = shape(value)
            if not isinstance(value, (Value, ValueCastable)):
                raise TypeError(
                    f"{shape!r}.__call__() must return a value or a value-castable object, not "
                    f"{value!r}")
            return value
        if Shape.cast(shape).signed:
            return value.as_signed()
        else:
            return value
Slice the underlying value.

        A field corresponding to :py:`key` is looked up in the layout. If the field's shape is
        a shape-castable object that has a :meth:`~.ShapeCastable.__call__` method, it is called and
        the result is returned. Otherwise, :meth:`~.ShapeCastable.as_shape` is called repeatedly on
        the shape until either an object with a :meth:`~.ShapeCastable.__call__` method is reached,
        or a :class:`.Shape` is returned. In the latter case, returns an unspecified Amaranth
        expression with the right shape.

        Arguments
        ---------
        key : :class:`str` or :class:`int` or :class:`.ValueCastable`
            Name or index of a field.

        Returns
        -------
        :class:`.Value` or :class:`.ValueCastable`, :ref:`assignable <lang-assignable>`
            A slice of the underlying value defined by the field.

        Raises
        ------
        :exc:`KeyError`
            If the layout does not define a field corresponding to :py:`key`.
        :exc:`TypeError`
            If :py:`key` is a value-castable object, but the layout of the view is not
            an :class:`ArrayLayout`.
        :exc:`TypeError`
            If :meth:`.ShapeCastable.__call__` does not return a value or a value-castable object.
        
amaranth/lib/data.py