Add helpers for converting between std::span<uint8_t> and <char> This helps in the work of spanifying interfaces. Bug: webrtc:42225170 Change-Id: I8064b7fb9a9d5611f92568c2082b36dd34886672 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/467480 Reviewed-by: Evan Shrubsole <eshr@webrtc.org> Commit-Queue: Harald Alvestrand <hta@webrtc.org> Cr-Commit-Position: refs/heads/main@{#47571}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 92555c2..15a55bd 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn
@@ -117,6 +117,11 @@ sources = [ "strong_alias.h" ] } +rtc_source_set("span_helpers") { + sources = [ "span_helpers.h" ] + deps = [ "//third_party/abseil-cpp/absl/strings:string_view" ] +} + rtc_source_set("swap_queue") { visibility = [ "*" ] sources = [ "swap_queue.h" ] @@ -154,6 +159,7 @@ deps = [ ":buffer", ":byte_order", + ":span_helpers", "//third_party/abseil-cpp/absl/base:core_headers", "//third_party/abseil-cpp/absl/strings:string_view", ] @@ -1601,6 +1607,7 @@ ":digest", ":logging", ":safe_conversions", + ":span_helpers", ":ssl_header", ":stringutils", ":timeutils", @@ -1673,6 +1680,7 @@ ":safe_conversions", ":socket", ":socket_address", + ":span_helpers", ":ssl", ":ssl_header", ":stream",
diff --git a/rtc_base/byte_buffer.h b/rtc_base/byte_buffer.h index 64648d0..02a0772 100644 --- a/rtc_base/byte_buffer.h +++ b/rtc_base/byte_buffer.h
@@ -21,6 +21,7 @@ #include "absl/strings/string_view.h" #include "rtc_base/buffer.h" #include "rtc_base/byte_order.h" +#include "rtc_base/span_helpers.h" // Reads/Writes from/to buffer using network byte order (big endian) namespace webrtc { @@ -49,7 +50,7 @@ // when the underlying type changes to uint8_t. // TODO(bugs.webrtc.org/15665): Delete when users are converted. absl::string_view DataAsStringView() const { - return absl::string_view(reinterpret_cast<const char*>(Data()), Length()); + return AsStringView(DataView()); } const char* DataAsCharPointer() const { return reinterpret_cast<const char*>(Data()); @@ -207,5 +208,4 @@ } // namespace webrtc - #endif // RTC_BASE_BYTE_BUFFER_H_
diff --git a/rtc_base/openssl_stream_adapter.cc b/rtc_base/openssl_stream_adapter.cc index ccc8823..a40c4ec 100644 --- a/rtc_base/openssl_stream_adapter.cc +++ b/rtc_base/openssl_stream_adapter.cc
@@ -44,6 +44,7 @@ #include "rtc_base/openssl_adapter.h" #include "rtc_base/openssl_digest.h" #include "rtc_base/openssl_utility.h" +#include "rtc_base/span_helpers.h" #include "rtc_base/ssl_certificate.h" #include "rtc_base/ssl_identity.h" #include "rtc_base/ssl_stream_adapter.h" @@ -218,8 +219,8 @@ BIO_clear_retry_flags(b); size_t read; int error; - StreamResult result = stream->Read( - std::span(reinterpret_cast<uint8_t*>(out), outl), read, error); + StreamResult result = + stream->Read(AsWritableUint8Span(std::span(out, outl)), read, error); if (result == SR_SUCCESS) { return checked_cast<int>(read); } else if (result == SR_BLOCK) { @@ -236,8 +237,8 @@ BIO_clear_retry_flags(b); size_t written; int error; - StreamResult result = stream->Write( - std::span(reinterpret_cast<const uint8_t*>(in), inl), written, error); + StreamResult result = + stream->Write(AsUint8Span(std::span(in, inl)), written, error); if (result == SR_SUCCESS) { return checked_cast<int>(written); } else if (result == SR_BLOCK) {
diff --git a/rtc_base/span_helpers.h b/rtc_base/span_helpers.h new file mode 100644 index 0000000..6de382c --- /dev/null +++ b/rtc_base/span_helpers.h
@@ -0,0 +1,53 @@ +/* + * Copyright 2026 The WebRTC Project Authors. All rights reserved. + * + * Use of this source code is governed by a BSD-style license + * that can be found in the LICENSE file in the root of the source + * tree. An additional intellectual property rights grant can be found + * in the file PATENTS. All contributing project authors may + * be found in the AUTHORS file in the root of the source tree. + */ + +#ifndef RTC_BASE_SPAN_HELPERS_H_ +#define RTC_BASE_SPAN_HELPERS_H_ + +#include <cstdint> +#include <span> + +namespace webrtc { + +// Converters between std::span<uint8_t> and std::span<char>. +// These are deliberately not templated; we don't think that we need +// more generic versions of these, and the need for them could be a code +// smell indicating the need for defining whether variables are intended +// to be text (char) or binary blobs (uint8_t). + +// If there turns out to be a need for many more variants, templates can +// be introduced in the future. +inline std::span<char> AsWritableCharSpan(std::span<uint8_t> span) { + return std::span<char>(reinterpret_cast<char*>(span.data()), span.size()); +} + +inline std::span<const char> AsCharSpan(std::span<const uint8_t> span) { + return std::span<const char>(reinterpret_cast<const char*>(span.data()), + span.size()); +} + +inline std::span<uint8_t> AsWritableUint8Span(std::span<char> span) { + return std::span<uint8_t>(reinterpret_cast<uint8_t*>(span.data()), + span.size()); +} + +inline std::span<const uint8_t> AsUint8Span(std::span<const char> span) { + return std::span<const uint8_t>(reinterpret_cast<const uint8_t*>(span.data()), + span.size()); +} + +inline absl::string_view AsStringView(std::span<const uint8_t> span) { + return absl::string_view(reinterpret_cast<const char*>(span.data()), + span.size()); +} + +} // namespace webrtc + +#endif // RTC_BASE_SPAN_HELPERS_H_
diff --git a/rtc_base/ssl_fingerprint.cc b/rtc_base/ssl_fingerprint.cc index 6911d8b..c8a2f02 100644 --- a/rtc_base/ssl_fingerprint.cc +++ b/rtc_base/ssl_fingerprint.cc
@@ -23,6 +23,7 @@ #include "rtc_base/logging.h" #include "rtc_base/message_digest.h" #include "rtc_base/rtc_certificate.h" +#include "rtc_base/span_helpers.h" #include "rtc_base/ssl_certificate.h" #include "rtc_base/ssl_identity.h" #include "rtc_base/string_encode.h" @@ -73,8 +74,7 @@ return nullptr; return std::make_unique<SSLFingerprint>( - algorithm, - std::span<const uint8_t>(reinterpret_cast<uint8_t*>(value), value_len)); + algorithm, AsUint8Span(std::span(value, value_len))); } std::unique_ptr<SSLFingerprint> SSLFingerprint::CreateFromCertificate(