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 };