def const(self, init):
        """Convert a constant initializer to a constant.

        Converts :py:`init`, which may be a sequence or a mapping of field values, to a constant.

        Returns
        -------
        :class:`Const`
            A constant that has the same value as a view with this layout that was initialized with
            an all-zero value and had every field assigned to the corresponding value in the order
            in which they appear in :py:`init`.
        """
        if isinstance(init, Const):
            if Layout.cast(init.shape()) != self:
                raise ValueError(f"Const layout {init.shape()!r} differs from shape layout "
                                 f"{self!r}")
            return init
        if init is None:
            iterator = iter(())
        elif isinstance(init, Mapping):
            iterator = init.items()
        elif isinstance(init, Sequence):
            iterator = enumerate(init)
        else:
            raise TypeError(f"Layout constant initializer must be a mapping or a sequence, not "
                            f"{init!r}")

        int_value = 0
        for key, key_value in iterator:
            try:
                field = self[key]
            except KeyError:
                raise ValueError(f"Layout constant initializer refers to key {key!r}, which is not "
                                 f"a part of the layout")
            cast_field_shape = Shape.cast(field.shape)
            if isinstance(field.shape, ShapeCastable):
                key_value = hdl.Const.cast(hdl.Const(key_value, field.shape))
            elif not isinstance(key_value, hdl.Const):
                key_value = hdl.Const(key_value, cast_field_shape)
            mask = ((1 << cast_field_shape.width) - 1) << field.offset
            int_value &= ~mask
            int_value |= (key_value.value << field.offset) & mask
        return Const(self, int_value)
Convert a constant initializer to a constant.

        Converts :py:`init`, which may be a sequence or a mapping of field values, to a constant.

        Returns
        -------
        :class:`Const`
            A constant that has the same value as a view with this layout that was initialized with
            an all-zero value and had every field assigned to the corresponding value in the order
            in which they appear in :py:`init`.
        
amaranth/lib/data.py