Make base::StringPiece implicitly convert to/from std::string_view
With absl::string_view turned into std::string_view,
abseil_string_conversions.h is now misnamed. However, those conversions
themselves are only needed because Chromium has forked a fundamental C++
vocabulary type.
As an intermediate state, until we make base::StringPiece into
std::string_view itself, add implicit conversions from/to the standard
type. This simplifies boundaries between external code, where the safe
pattern is currently tedious and the unsafe pattern (decomposing into
ptr/len) is much shorter.
However, this is imperfect. You may still need std::string_view in
places. E.g. implementing std::string_view-based interfaces, APIs that
take or return pointers, or cases when multiple implicit conversions are
chained together.
To that end, this reshuffles PRESUBMITs and guidelines:
- In Chromium, absl::string_view is now always std::string_view.
Whenever you would have said absl::string_view, say
std::string_view instead.
- However, for now, base::StringPiece is still distinct from
std::string_view and the guidelines still prefer base::StringPiece
over it. So, when you need std::string_view to deal with an
external API, use std::string_view. Otherwise, use
base::StringPiece.
(This is the same as the status quo, except absl::string_view is
replaced with std::string_view, and using the more standard spelling
when needed for 3p APIs is spelled out more explicitly. In other
cases, our non-standard spelling remains the preference.)
Existing uses of absl::string_view are left alone, but as they're the
exact same type, references can be freely and incrementally converted
to std::string_view.
Having two vocabulary types is still costly as developers must keep
track of the two, but it's better than three. The only way to truly
remove this cost is to merge base::StringPiece into std::string_view,
now that they are at parity in safety checks. This CL leaves that
decision for later, though its author is strongly of the opinion that we
should do it. :-)
As part of this, this removes the existing uses of
abseil_string_conversions.h, and cleans up some conversions and base
dependencies in the to-be-extracted net/der and net/cert/pki that are
no longer necessary.
Bug: 691162, 1370553
Change-Id: I0629029c157819ce673598caeb0f106917244f6c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4427593
Reviewed-by: Yuwei Huang <yuweih@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com>
Commit-Queue: David Benjamin <davidben@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1132183}
diff --git a/PRESUBMIT.py b/PRESUBMIT.py
index 3122159..6f3b72a 100644
--- a/PRESUBMIT.py
+++ b/PRESUBMIT.py
@@ -949,13 +949,27 @@
[_THIRD_PARTY_EXCEPT_BLINK], # Not an error in third_party folders.
),
BanRule(
- r'/\b(absl|std)::(u16)?string_view\b',
+ r'/\babsl::string_view\b',
(
- 'absl::string_view is banned and std::[u16]string_view are not allowed',
- ' yet (https://crbug.com/691162). Use base::StringPiece[16] instead.',
+ 'absl::string_view is a legacy spelling of std::string_view, which is ',
+ 'not allowed yet (https://crbug.com/691162). Use base::StringPiece ',
+ 'instead, unless std::string_view is needed to use with an external ',
+ 'API.',
+ ),
+ True,
+ [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
+ ),
+ BanRule(
+ r'/\bstd::(u16)?string_view\b',
+ (
+ 'std::[u16]string_view is not yet allowed (crbug.com/691162). Use ',
+ 'base::StringPiece[16] instead, unless std::[u16]string_view is ',
+ 'needed to use an external API.',
),
True,
[
+ # Needed to implement and test std::string_view interoperability.
+ r'base/strings/string_piece.*',
# Needed to use liburlpattern API.
r'third_party/blink/renderer/core/url_pattern/.*',
r'third_party/blink/renderer/modules/manifest/manifest_parser\.cc',
@@ -965,6 +979,12 @@
r'net/test/embedded_test_server/.*',
r'net/third_party/quiche/.*',
r'services/network/web_transport\.cc',
+ # This code is in the process of being extracted into an external
+ # library, where //base will be unavailable.
+ r'net/cert/pki/.*',
+ r'net/der/.*',
+ # Needed to use APIs from the above.
+ r'net/cert/.*',
# Not an error in third_party folders.
_THIRD_PARTY_EXCEPT_BLINK
],
@@ -1131,15 +1151,6 @@
[_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
),
BanRule(
- r'/\bstd::(u16)?string_view\b',
- (
- 'std::[u16]string_view is not yet allowed (crbug.com/691162). Use ',
- 'base::StringPiece[16] instead.',
- ),
- True,
- [_THIRD_PARTY_EXCEPT_BLINK], # Don't warn in third_party folders.
- ),
- BanRule(
r'/#include <(barrier|latch|semaphore|stop_token)>',
(
'The thread support library is banned. Use base/synchronization '