Skip to content

Commit

Permalink
Everywhere: Use ReadonlySpan<T> instead of Span<T const>
Browse files Browse the repository at this point in the history
  • Loading branch information
MacDue authored and linusg committed Feb 8, 2023
1 parent 1c92e6e commit 63b1103
Show file tree
Hide file tree
Showing 102 changed files with 206 additions and 206 deletions.
4 changes: 2 additions & 2 deletions AK/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Array {

[[nodiscard]] constexpr size_t size() const { return Size; }

[[nodiscard]] constexpr Span<T const> span() const { return { __data, Size }; }
[[nodiscard]] constexpr ReadonlySpan<T> span() const { return { __data, Size }; }
[[nodiscard]] constexpr Span<T> span() { return { __data, Size }; }

[[nodiscard]] constexpr T const& at(size_t index) const
Expand Down Expand Up @@ -76,7 +76,7 @@ struct Array {
[[nodiscard]] constexpr ConstIterator end() const { return ConstIterator::end(*this); }
[[nodiscard]] constexpr Iterator end() { return Iterator::end(*this); }

[[nodiscard]] constexpr operator Span<T const>() const { return span(); }
[[nodiscard]] constexpr operator ReadonlySpan<T>() const { return span(); }
[[nodiscard]] constexpr operator Span<T>() { return span(); }

constexpr size_t fill(T const& value)
Expand Down
4 changes: 2 additions & 2 deletions AK/ByteBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ class ByteBuffer {
[[nodiscard]] Bytes bytes() { return { data(), size() }; }
[[nodiscard]] ReadonlyBytes bytes() const { return { data(), size() }; }

[[nodiscard]] AK::Span<u8> span() { return { data(), size() }; }
[[nodiscard]] AK::Span<u8 const> span() const { return { data(), size() }; }
[[nodiscard]] AK::Bytes span() { return { data(), size() }; }
[[nodiscard]] AK::ReadonlyBytes span() const { return { data(), size() }; }

[[nodiscard]] u8* offset_pointer(size_t offset) { return data() + offset; }
[[nodiscard]] u8 const* offset_pointer(size_t offset) const { return data() + offset; }
Expand Down
2 changes: 1 addition & 1 deletion AK/FixedArray.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class FixedArray {
ConstIterator end() const { return ConstIterator::end(*this); }

Span<T> span() { return { data(), size() }; }
Span<T const> span() const { return { data(), size() }; }
ReadonlySpan<T> span() const { return { data(), size() }; }

private:
struct Storage {
Expand Down
18 changes: 9 additions & 9 deletions AK/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,13 +264,13 @@ class FormatBuilder {

class TypeErasedFormatParams {
public:
Span<TypeErasedParameter const> parameters() const { return m_parameters; }
ReadonlySpan<TypeErasedParameter> parameters() const { return m_parameters; }

void set_parameters(Span<TypeErasedParameter const> parameters) { m_parameters = parameters; }
void set_parameters(ReadonlySpan<TypeErasedParameter> parameters) { m_parameters = parameters; }
size_t take_next_index() { return m_next_index++; }

private:
Span<TypeErasedParameter const> m_parameters;
ReadonlySpan<TypeErasedParameter> m_parameters;
size_t m_next_index { 0 };
};

Expand Down Expand Up @@ -359,14 +359,14 @@ struct Formatter<StringView> : StandardFormatter {

template<typename T>
requires(HasFormatter<T>)
struct Formatter<Span<T const>> : StandardFormatter {
struct Formatter<ReadonlySpan<T>> : StandardFormatter {
Formatter() = default;
explicit Formatter(StandardFormatter formatter)
: StandardFormatter(move(formatter))
{
}

ErrorOr<void> format(FormatBuilder& builder, Span<T const> value)
ErrorOr<void> format(FormatBuilder& builder, ReadonlySpan<T> value)
{
if (m_mode == Mode::Pointer) {
Formatter<FlatPtr> formatter { *this };
Expand Down Expand Up @@ -406,19 +406,19 @@ struct Formatter<Span<T const>> : StandardFormatter {

template<typename T>
requires(HasFormatter<T>)
struct Formatter<Span<T>> : Formatter<Span<T const>> {
struct Formatter<Span<T>> : Formatter<ReadonlySpan<T>> {
ErrorOr<void> format(FormatBuilder& builder, Span<T> value)
{
return Formatter<Span<T const>>::format(builder, value);
return Formatter<ReadonlySpan<T>>::format(builder, value);
}
};

template<typename T, size_t inline_capacity>
requires(HasFormatter<T>)
struct Formatter<Vector<T, inline_capacity>> : Formatter<Span<T const>> {
struct Formatter<Vector<T, inline_capacity>> : Formatter<ReadonlySpan<T>> {
ErrorOr<void> format(FormatBuilder& builder, Vector<T, inline_capacity> const& value)
{
return Formatter<Span<T const>>::format(builder, value.span());
return Formatter<ReadonlySpan<T>>::format(builder, value.span());
}
};

Expand Down
4 changes: 2 additions & 2 deletions AK/MemMem.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ constexpr void const* bitap_bitwise(void const* haystack, size_t haystack_length
}

template<typename HaystackIterT>
inline Optional<size_t> memmem(HaystackIterT const& haystack_begin, HaystackIterT const& haystack_end, Span<u8 const> needle)
inline Optional<size_t> memmem(HaystackIterT const& haystack_begin, HaystackIterT const& haystack_end, ReadonlyBytes needle)
requires(requires { (*haystack_begin).data(); (*haystack_begin).size(); })
{
auto prepare_kmp_partial_table = [&] {
Expand Down Expand Up @@ -123,7 +123,7 @@ inline Optional<size_t> memmem_optional(void const* haystack, size_t haystack_le
}

// Fallback to KMP.
Array<Span<u8 const>, 1> spans { Span<u8 const> { (u8 const*)haystack, haystack_length } };
Array<ReadonlyBytes, 1> spans { ReadonlyBytes { (u8 const*)haystack, haystack_length } };
return memmem(spans.begin(), spans.end(), { (u8 const*)needle, needle_length });
}

Expand Down
4 changes: 2 additions & 2 deletions AK/Span.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ class Span : public Detail::Span<T> {
return false;
}

[[nodiscard]] bool constexpr starts_with(Span<T const> other) const
[[nodiscard]] bool constexpr starts_with(ReadonlySpan<T> other) const
{
if (size() < other.size())
return false;
Expand Down Expand Up @@ -250,7 +250,7 @@ class Span : public Detail::Span<T> {
return TypedTransfer<T>::compare(data(), other.data(), size());
}

ALWAYS_INLINE constexpr operator Span<T const>() const
ALWAYS_INLINE constexpr operator ReadonlySpan<T>() const
{
return { data(), size() };
}
Expand Down
8 changes: 4 additions & 4 deletions AK/UFixedBigInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,13 @@ requires(sizeof(T) >= sizeof(u64) && IsUnsigned<T>) class UFixedBigInt {
return m_high;
}

Span<u8> bytes()
Bytes bytes()
{
return Span<u8>(reinterpret_cast<u8*>(this), sizeof(R));
return Bytes { reinterpret_cast<u8*>(this), sizeof(R) };
}
Span<u8 const> bytes() const
ReadonlyBytes bytes() const
{
return Span<u8 const>(reinterpret_cast<u8 const*>(this), sizeof(R));
return ReadonlyBytes { reinterpret_cast<u8 const*>(this), sizeof(R) };
}

template<Unsigned U>
Expand Down
4 changes: 2 additions & 2 deletions AK/Utf16View.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Utf16View {
Utf16View() = default;
~Utf16View() = default;

explicit Utf16View(Span<u16 const> code_units)
explicit Utf16View(ReadonlySpan<u16> code_units)
: m_code_units(code_units)
{
}
Expand Down Expand Up @@ -116,7 +116,7 @@ class Utf16View {

size_t calculate_length_in_code_points() const;

Span<u16 const> m_code_units;
ReadonlySpan<u16> m_code_units;
mutable Optional<size_t> m_length_in_code_points;
};

Expand Down
6 changes: 3 additions & 3 deletions AK/Vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ requires(!IsRvalueReference<T>) class Vector {
m_size = other.size();
}

explicit Vector(Span<T const> other)
explicit Vector(ReadonlySpan<T> other)
requires(!IsLvalueReference<T>)
{
ensure_capacity(other.size());
Expand All @@ -112,10 +112,10 @@ requires(!IsRvalueReference<T>) class Vector {
}

Span<StorageType> span() { return { data(), size() }; }
Span<StorageType const> span() const { return { data(), size() }; }
ReadonlySpan<StorageType> span() const { return { data(), size() }; }

operator Span<StorageType>() { return span(); }
operator Span<StorageType const>() const { return span(); }
operator ReadonlySpan<StorageType>() const { return span(); }

bool is_empty() const { return size() == 0; }
ALWAYS_INLINE size_t size() const { return m_size; }
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Credentials.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Kernel {

ErrorOr<NonnullRefPtr<Credentials>> Credentials::create(UserID uid, GroupID gid, UserID euid, GroupID egid, UserID suid, GroupID sgid, Span<GroupID const> extra_gids, SessionID sid, ProcessGroupID pgid)
ErrorOr<NonnullRefPtr<Credentials>> Credentials::create(UserID uid, GroupID gid, UserID euid, GroupID egid, UserID suid, GroupID sgid, ReadonlySpan<GroupID> extra_gids, SessionID sid, ProcessGroupID pgid)
{
auto extra_gids_array = TRY(FixedArray<GroupID>::create(extra_gids));
return adopt_nonnull_ref_or_enomem(new (nothrow) Credentials(uid, gid, euid, egid, suid, sgid, move(extra_gids_array), sid, pgid));
Expand Down
4 changes: 2 additions & 2 deletions Kernel/Credentials.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Kernel {

class Credentials final : public AtomicRefCounted<Credentials> {
public:
static ErrorOr<NonnullRefPtr<Credentials>> create(UserID uid, GroupID gid, UserID euid, GroupID egid, UserID suid, GroupID sgid, Span<GroupID const> extra_gids, SessionID sid, ProcessGroupID pgid);
static ErrorOr<NonnullRefPtr<Credentials>> create(UserID uid, GroupID gid, UserID euid, GroupID egid, UserID suid, GroupID sgid, ReadonlySpan<GroupID> extra_gids, SessionID sid, ProcessGroupID pgid);
~Credentials();

bool is_superuser() const { return euid() == 0; }
Expand All @@ -25,7 +25,7 @@ class Credentials final : public AtomicRefCounted<Credentials> {
GroupID gid() const { return m_gid; }
UserID suid() const { return m_suid; }
GroupID sgid() const { return m_sgid; }
Span<GroupID const> extra_gids() const { return m_extra_gids.span(); }
ReadonlySpan<GroupID> extra_gids() const { return m_extra_gids.span(); }
SessionID sid() const { return m_sid; };
ProcessGroupID pgid() const { return m_pgid; }

Expand Down
6 changes: 3 additions & 3 deletions Kernel/FileSystem/InodeMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct InodeMetadata {
bool may_write(Credentials const&, UseEffectiveIDs = UseEffectiveIDs::Yes) const;
bool may_execute(Credentials const&, UseEffectiveIDs = UseEffectiveIDs::Yes) const;

bool may_read(UserID u, GroupID g, Span<GroupID const> eg) const
bool may_read(UserID u, GroupID g, ReadonlySpan<GroupID> eg) const
{
if (u == 0)
return true;
Expand All @@ -59,7 +59,7 @@ struct InodeMetadata {
return (mode & S_IROTH) == S_IROTH;
}

bool may_write(UserID u, GroupID g, Span<GroupID const> eg) const
bool may_write(UserID u, GroupID g, ReadonlySpan<GroupID> eg) const
{
if (u == 0)
return true;
Expand All @@ -70,7 +70,7 @@ struct InodeMetadata {
return (mode & S_IWOTH) == S_IWOTH;
}

bool may_execute(UserID u, GroupID g, Span<GroupID const> eg) const
bool may_execute(UserID u, GroupID g, ReadonlySpan<GroupID> eg) const
{
if (u == 0)
return true;
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Interrupts/GenericInterruptHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ void GenericInterruptHandler::change_interrupt_number(u8 number)
register_generic_interrupt_handler(InterruptManagement::acquire_mapped_interrupt_number(interrupt_number()), *this);
}

Span<u32 const> GenericInterruptHandler::per_cpu_call_counts() const
ReadonlySpan<u32> GenericInterruptHandler::per_cpu_call_counts() const
{
return m_per_cpu_call_counts.span().slice(0, Processor::count());
}
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Interrupts/GenericInterruptHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class GenericInterruptHandler {

u8 interrupt_number() const { return m_interrupt_number; }

Span<u32 const> per_cpu_call_counts() const;
ReadonlySpan<u32> per_cpu_call_counts() const;

virtual size_t sharing_devices_count() const = 0;
virtual bool is_shared_handler() const = 0;
Expand Down
6 changes: 3 additions & 3 deletions Kernel/Memory/SharedFramebufferVMObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Span<RefPtr<PhysicalPage>> SharedFramebufferVMObject::real_framebuffer_physical_
{
return m_real_framebuffer_vmobject->physical_pages();
}
Span<RefPtr<PhysicalPage> const> SharedFramebufferVMObject::real_framebuffer_physical_pages() const
ReadonlySpan<RefPtr<PhysicalPage>> SharedFramebufferVMObject::real_framebuffer_physical_pages() const
{
return m_real_framebuffer_vmobject->physical_pages();
}
Expand All @@ -70,7 +70,7 @@ Span<RefPtr<PhysicalPage>> SharedFramebufferVMObject::fake_sink_framebuffer_phys
return m_physical_pages.span();
}

Span<RefPtr<PhysicalPage> const> SharedFramebufferVMObject::fake_sink_framebuffer_physical_pages() const
ReadonlySpan<RefPtr<PhysicalPage>> SharedFramebufferVMObject::fake_sink_framebuffer_physical_pages() const
{
return m_physical_pages.span();
}
Expand All @@ -92,7 +92,7 @@ void SharedFramebufferVMObject::switch_to_real_framebuffer_writes(Badge<Kernel::
});
}

Span<RefPtr<PhysicalPage> const> SharedFramebufferVMObject::physical_pages() const
ReadonlySpan<RefPtr<PhysicalPage>> SharedFramebufferVMObject::physical_pages() const
{
SpinlockLocker locker(m_writes_state_lock);
if (m_writes_are_faked)
Expand Down
10 changes: 5 additions & 5 deletions Kernel/Memory/SharedFramebufferVMObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class SharedFramebufferVMObject final : public VMObject {
}
virtual StringView class_name() const override { return "FakeWritesFramebufferVMObject"sv; }
virtual ErrorOr<NonnullLockRefPtr<VMObject>> try_clone() override { return Error::from_errno(ENOTIMPL); }
virtual Span<RefPtr<PhysicalPage> const> physical_pages() const override { return m_parent_object->fake_sink_framebuffer_physical_pages(); }
virtual ReadonlySpan<RefPtr<PhysicalPage>> physical_pages() const override { return m_parent_object->fake_sink_framebuffer_physical_pages(); }
virtual Span<RefPtr<PhysicalPage>> physical_pages() override { return m_parent_object->fake_sink_framebuffer_physical_pages(); }
NonnullLockRefPtr<SharedFramebufferVMObject> m_parent_object;
};
Expand All @@ -46,7 +46,7 @@ class SharedFramebufferVMObject final : public VMObject {
}
virtual StringView class_name() const override { return "RealWritesFramebufferVMObject"sv; }
virtual ErrorOr<NonnullLockRefPtr<VMObject>> try_clone() override { return Error::from_errno(ENOTIMPL); }
virtual Span<RefPtr<PhysicalPage> const> physical_pages() const override { return m_parent_object->real_framebuffer_physical_pages(); }
virtual ReadonlySpan<RefPtr<PhysicalPage>> physical_pages() const override { return m_parent_object->real_framebuffer_physical_pages(); }
virtual Span<RefPtr<PhysicalPage>> physical_pages() override { return m_parent_object->real_framebuffer_physical_pages(); }
NonnullLockRefPtr<SharedFramebufferVMObject> m_parent_object;
};
Expand All @@ -60,14 +60,14 @@ class SharedFramebufferVMObject final : public VMObject {
void switch_to_fake_sink_framebuffer_writes(Badge<Kernel::DisplayConnector>);
void switch_to_real_framebuffer_writes(Badge<Kernel::DisplayConnector>);

virtual Span<RefPtr<PhysicalPage> const> physical_pages() const override;
virtual ReadonlySpan<RefPtr<PhysicalPage>> physical_pages() const override;
virtual Span<RefPtr<PhysicalPage>> physical_pages() override;

Span<RefPtr<PhysicalPage>> fake_sink_framebuffer_physical_pages();
Span<RefPtr<PhysicalPage> const> fake_sink_framebuffer_physical_pages() const;
ReadonlySpan<RefPtr<PhysicalPage>> fake_sink_framebuffer_physical_pages() const;

Span<RefPtr<PhysicalPage>> real_framebuffer_physical_pages();
Span<RefPtr<PhysicalPage> const> real_framebuffer_physical_pages() const;
ReadonlySpan<RefPtr<PhysicalPage>> real_framebuffer_physical_pages() const;

FakeWritesFramebufferVMObject const& fake_writes_framebuffer_vmobject() const { return *m_fake_writes_framebuffer_vmobject; }
FakeWritesFramebufferVMObject& fake_writes_framebuffer_vmobject() { return *m_fake_writes_framebuffer_vmobject; }
Expand Down
2 changes: 1 addition & 1 deletion Kernel/Memory/VMObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class VMObject

size_t page_count() const { return m_physical_pages.size(); }

virtual Span<RefPtr<PhysicalPage> const> physical_pages() const { return m_physical_pages.span(); }
virtual ReadonlySpan<RefPtr<PhysicalPage>> physical_pages() const { return m_physical_pages.span(); }
virtual Span<RefPtr<PhysicalPage>> physical_pages() { return m_physical_pages.span(); }

size_t size() const { return m_physical_pages.size() * PAGE_SIZE; }
Expand Down
2 changes: 1 addition & 1 deletion Ladybird/HelperProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <AK/String.h>
#include <QCoreApplication>

ErrorOr<void> spawn_helper_process(StringView process_name, Span<StringView> arguments, Core::System::SearchInPath search_in_path, Optional<Span<StringView const>> environment)
ErrorOr<void> spawn_helper_process(StringView process_name, ReadonlySpan<StringView> arguments, Core::System::SearchInPath search_in_path, Optional<ReadonlySpan<StringView>> environment)
{
auto paths = TRY(get_paths_for_helper_process(process_name));
VERIFY(!paths.is_empty());
Expand Down
2 changes: 1 addition & 1 deletion Ladybird/HelperProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
#include <AK/StringView.h>
#include <LibCore/System.h>

ErrorOr<void> spawn_helper_process(StringView process_name, Span<StringView> arguments, Core::System::SearchInPath, Optional<Span<StringView const>> environment = {});
ErrorOr<void> spawn_helper_process(StringView process_name, ReadonlySpan<StringView> arguments, Core::System::SearchInPath, Optional<ReadonlySpan<StringView>> environment = {});
ErrorOr<Vector<String>> get_paths_for_helper_process(StringView process_name);
Original file line number Diff line number Diff line change
Expand Up @@ -2215,7 +2215,7 @@ ErrorOr<Vector<CalendarRangePattern>> get_calendar_range12_formats(StringView lo
return result;
}
static ErrorOr<Span<@string_index_type@ const>> find_calendar_symbols(StringView locale, StringView calendar, CalendarSymbol symbol, CalendarPatternStyle style)
static ErrorOr<ReadonlySpan<@string_index_type@>> find_calendar_symbols(StringView locale, StringView calendar, CalendarSymbol symbol, CalendarPatternStyle style)
{
if (auto const* data = TRY(find_calendar_data(locale, calendar)); data != nullptr) {
auto const& symbols_list = s_calendar_symbol_lists[data->symbols];
Expand Down Expand Up @@ -2243,7 +2243,7 @@ static ErrorOr<Span<@string_index_type@ const>> find_calendar_symbols(StringView
return s_symbol_lists.at(symbol_list_index);
}
return Span<@string_index_type@ const> {};
return ReadonlySpan<@string_index_type@> {};
}
ErrorOr<Optional<StringView>> get_calendar_era_symbol(StringView locale, StringView calendar, CalendarPatternStyle style, Era value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1127,7 +1127,7 @@ struct TextLayout {
generate_available_values(generator, "get_available_currencies"sv, cldr.currencies);

generator.append(R"~~~(
Span<StringView const> get_available_keyword_values(StringView key)
ReadonlySpan<StringView> get_available_keyword_values(StringView key)
{
auto key_value = key_from_string(key);
if (!key_value.has_value())
Expand Down Expand Up @@ -1521,7 +1521,7 @@ Optional<StringView> get_locale_@enum_snake@_mapping(StringView locale, StringVi
generate_value_to_string(generator, "{}_to_string"sv, "CharacterOrder"sv, "character_order"sv, format_identifier, cldr.character_orders);

generator.append(R"~~~(
static Span<@string_index_type@ const> find_keyword_indices(StringView locale, StringView key)
static ReadonlySpan<@string_index_type@> find_keyword_indices(StringView locale, StringView key)
{
auto locale_value = locale_from_string(locale);
if (!locale_value.has_value())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -915,7 +915,7 @@ static Optional<NumberSystem> keyword_to_number_system(KeywordNumbers keyword)
}
}
Optional<Span<u32 const>> get_digits_for_number_system(StringView system)
Optional<ReadonlySpan<u32>> get_digits_for_number_system(StringView system)
{
auto number_system_keyword = keyword_nu_from_string(system);
if (!number_system_keyword.has_value())
Expand Down
Loading

0 comments on commit 63b1103

Please sign in to comment.