From 7036a9b6f7d7df230b060252cc56f5f9a09eb43b Mon Sep 17 00:00:00 2001 From: asynts Date: Sun, 26 Jul 2020 18:08:40 +0200 Subject: [PATCH] AK: Define conversion from Span to Span correctly. I accidently wrote `Span>` when I meant `Span::Type>`. Changing that wouldn't be enough though, this constructor can only be defined if T is not const, otherwise it would redefine the copy constructor. This can be avoided by overloading the cast operator. --- AK/Span.h | 10 +++++----- AK/Tests/Span.cpp | 8 ++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/AK/Span.h b/AK/Span.h index bf46986cd0..e974ce8807 100644 --- a/AK/Span.h +++ b/AK/Span.h @@ -52,11 +52,6 @@ public: , m_size(other.m_size) { } - ALWAYS_INLINE Span(const Span>& other) - : m_values(other.m_values) - , m_size(other.m_size) - { - } ALWAYS_INLINE const T* data() const { return m_values; } ALWAYS_INLINE T* data() { return m_values; } @@ -115,6 +110,11 @@ public: m_values = other.m_values; } + ALWAYS_INLINE operator Span() const + { + return { data(), size() }; + } + protected: T* m_values { nullptr }; size_t m_size { 0 }; diff --git a/AK/Tests/Span.cpp b/AK/Tests/Span.cpp index ef0c48a9b0..301a1f9c28 100644 --- a/AK/Tests/Span.cpp +++ b/AK/Tests/Span.cpp @@ -36,6 +36,14 @@ TEST_CASE(default_constructor_is_empty) EXPECT(span.is_empty()); } +TEST_CASE(implicit_converson_to_const) +{ + Bytes bytes0; + ReadonlyBytes bytes1 { bytes0 }; + [[maybe_unused]] ReadonlyBytes bytes2 = bytes0; + [[maybe_unused]] ReadonlyBytes bytes3 = static_cast(bytes2); +} + TEST_CASE(span_works_with_constant_types) { const u8 buffer[4] { 1, 2, 3, 4 };