pw_bytes: ByteSpan and ConstByteSpan aliases - Introduce ByteSpan and ConstByteSpan aliases for std::span<std::byte> and std::span<const std::byte>. - Use the new aliases in pw_bytes/byte_builder.h. Change-Id: Ifaf5e19c9b34cf7874b366300aa1e582ae1e5587 Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/12944 Reviewed-by: Keir Mierle <keir@google.com> Commit-Queue: Wyatt Hepler <hepler@google.com>
diff --git a/pw_bytes/BUILD b/pw_bytes/BUILD index 8bfa841..baa3389 100644 --- a/pw_bytes/BUILD +++ b/pw_bytes/BUILD
@@ -29,6 +29,7 @@ ], hdrs = [ "public/pw_bytes/byte_builder.h", + "public/pw_bytes/span.h", ], includes = ["public"], deps = [ @@ -45,4 +46,4 @@ ":pw_bytes", "//pw_unit_test", ], -) \ No newline at end of file +)
diff --git a/pw_bytes/BUILD.gn b/pw_bytes/BUILD.gn index b7aa5ab..ab0ca28 100644 --- a/pw_bytes/BUILD.gn +++ b/pw_bytes/BUILD.gn
@@ -21,16 +21,20 @@ import("$dir_pw_unit_test/test.gni") config("default_config") { include_dirs = [ "public" ] + visibility = [ ":*" ] } pw_source_set("pw_bytes") { public_configs = [ ":default_config" ] - public = [ "public/pw_bytes/byte_builder.h" ] + public = [ + "public/pw_bytes/byte_builder.h", + "public/pw_bytes/span.h", + ] sources = [ "byte_builder.cc" ] public_deps = [ - "$dir_pw_preprocessor", - "$dir_pw_span", - "$dir_pw_status", + dir_pw_preprocessor, + dir_pw_span, + dir_pw_status, ] }
diff --git a/pw_bytes/byte_builder_test.cc b/pw_bytes/byte_builder_test.cc index 7d8ce2d..ab4dba9 100644 --- a/pw_bytes/byte_builder_test.cc +++ b/pw_bytes/byte_builder_test.cc
@@ -12,7 +12,7 @@ // License for the specific language governing permissions and limitations under // the License. -#include "pw_bytes//byte_builder.h" +#include "pw_bytes/byte_builder.h" #include <array> #include <cstddef> @@ -30,7 +30,7 @@ namespace { TEST(ByteBuilder, EmptyBuffer_SizeAndMaxSizeAreCorrect) { - ByteBuilder bb(span<byte>{}); + ByteBuilder bb(ByteSpan{}); EXPECT_TRUE(bb.empty()); EXPECT_EQ(0u, bb.size()); @@ -54,7 +54,7 @@ } TEST(ByteBuilder, EmptyBuffer_Append) { - ByteBuilder bb(span<byte>{}); + ByteBuilder bb(ByteSpan{}); EXPECT_TRUE(bb.empty()); auto bytesTestLiteral = MakeBytes(0x04, 0x05);
diff --git a/pw_bytes/public/pw_bytes/byte_builder.h b/pw_bytes/public/pw_bytes/byte_builder.h index fb84347..49af984 100644 --- a/pw_bytes/public/pw_bytes/byte_builder.h +++ b/pw_bytes/public/pw_bytes/byte_builder.h
@@ -18,8 +18,8 @@ #include <cstddef> #include <cstring> +#include "pw_bytes/span.h" #include "pw_preprocessor/compiler.h" -#include "pw_span/span.h" #include "pw_status/status.h" #include "pw_status/status_with_size.h" @@ -134,7 +134,7 @@ using const_iterator = iterator; // Creates an empty ByteBuilder. - constexpr ByteBuilder(span<std::byte> buffer) : buffer_(buffer), size_(0) {} + constexpr ByteBuilder(ByteSpan buffer) : buffer_(buffer), size_(0) {} // Disallow copy/assign to avoid confusion about where the bytes is actually // stored. ByteBuffers may be copied into one another. @@ -213,7 +213,7 @@ ByteBuilder& append(const void* bytes, size_t count); // Appends bytes from a byte span that calls the pointer/length version. - ByteBuilder& append(span<std::byte> bytes) { + ByteBuilder& append(ConstByteSpan bytes) { return append(bytes.data(), bytes.size()); } @@ -280,7 +280,7 @@ protected: // Functions to support ByteBuffer copies. - constexpr ByteBuilder(const span<std::byte>& buffer, const ByteBuilder& other) + constexpr ByteBuilder(const ByteSpan& buffer, const ByteBuilder& other) : buffer_(buffer), size_(other.size_), status_(other.status_) {} void CopySizeAndStatus(const ByteBuilder& other) { @@ -295,7 +295,7 @@ } size_t ResizeForAppend(size_t bytes_to_append); - const span<std::byte> buffer_; + const ByteSpan buffer_; size_t size_; Status status_;
diff --git a/pw_bytes/public/pw_bytes/span.h b/pw_bytes/public/pw_bytes/span.h new file mode 100644 index 0000000..b3ebf56 --- /dev/null +++ b/pw_bytes/public/pw_bytes/span.h
@@ -0,0 +1,26 @@ +// Copyright 2020 The Pigweed Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); you may not +// use this file except in compliance with the License. You may obtain a copy of +// the License at +// +// https://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +// License for the specific language governing permissions and limitations under +// the License. +#pragma once + +#include <cstddef> +#include <span> + +namespace pw { + +// Aliases for spans of bytes. +using ByteSpan = std::span<std::byte>; + +using ConstByteSpan = std::span<const std::byte>; + +} // namespace pw