diff --git a/include/etl/span.h b/include/etl/span.h index a98985f8..698b8f11 100644 --- a/include/etl/span.h +++ b/include/etl/span.h @@ -41,6 +41,7 @@ SOFTWARE. #include "integral_limits.h" #include "memory.h" #include "array.h" +#include "byte.h" #include "private/dynamic_extent.h" @@ -944,6 +945,24 @@ namespace etl } }; #endif + + //************************************************************************* + /// Obtains a view to the object representation of the elements of the span s. + //************************************************************************* + + template + auto as_bytes(span s) ETL_NOEXCEPT + { + return span {reinterpret_cast(s.data()), s.size_bytes()}; + } + + template + auto as_writable_bytes(span s) ETL_NOEXCEPT + { + ETL_STATIC_ASSERT(not etl::is_const::value, "span must be of non-const type"); + return span {reinterpret_cast(s.data()), s.size_bytes()}; + } + } #endif diff --git a/test/test_span_dynamic_extent.cpp b/test/test_span_dynamic_extent.cpp index b1a325ef..17ac913a 100644 --- a/test/test_span_dynamic_extent.cpp +++ b/test/test_span_dynamic_extent.cpp @@ -1125,6 +1125,19 @@ namespace CHECK_FALSE(view8 != view8); } + //************************************************************************* + TEST(test_convert_span_any_to_span_byte) + { + /* mutable */ float data[2]{3.141592f, 2.71828f }; + + auto const const_bytes = etl::as_bytes(etl::span{data}); + auto const writable_bytes = etl::as_writable_bytes(etl::span{data}); + + CHECK_TRUE(const_bytes.size() == sizeof(data)); + CHECK_TRUE(writable_bytes.size() == sizeof(data)); + + } + #include "etl/private/diagnostic_pop.h" }; } diff --git a/test/test_span_fixed_extent.cpp b/test/test_span_fixed_extent.cpp index f251b3d7..b430a35d 100644 --- a/test/test_span_fixed_extent.cpp +++ b/test/test_span_fixed_extent.cpp @@ -1070,6 +1070,18 @@ namespace CHECK_FALSE(view6 != view7); } + TEST(test_convert_span_any_to_span_byte) + { + /* mutable */ float data[2]{3.141592f, 2.71828f}; + + auto const const_bytes = etl::as_bytes(etl::span{data}); + auto const writable_bytes = etl::as_writable_bytes(etl::span{data}); + + CHECK_TRUE(const_bytes.size() == sizeof(data)); + CHECK_TRUE(writable_bytes.size() == sizeof(data)); + + } + #include "etl/private/diagnostic_pop.h" }; }