xref: /aosp_15_r20/external/cronet/base/memory/raw_span.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef BASE_MEMORY_RAW_SPAN_H_
6 #define BASE_MEMORY_RAW_SPAN_H_
7 
8 #include "base/containers/span.h"
9 #include "base/memory/raw_ptr.h"
10 
11 namespace base {
12 
13 // raw_span<T> is a type that provides the spatial safety of span<T> along
14 // with the temporal safety of raw_ptr<T>. This is intended to be a safer
15 // replacement for classes that store pointer + size fields. As is the case
16 // with raw_ptr<>, raw_span<> should be used for class members only, with
17 // ordinary span<> used for function arguments and the like. Note that
18 // raw_span<> will implicitly convert to span<> for ease of use in these
19 // cases.
20 
21 template <typename T, RawPtrTraits Traits = RawPtrTraits::kEmpty>
22 using raw_span =
23     span<T, dynamic_extent, raw_ptr<T, Traits | AllowPtrArithmetic>>;
24 
25 template <typename T>
ExtractAsDanglingSpan(raw_span<T> & arg)26 span<T> ExtractAsDanglingSpan(raw_span<T>& arg) {
27   span<T> result = std::exchange(arg, raw_span<T>());
28   return result;
29 }
30 
31 }  // namespace base
32 
33 #endif  // BASE_MEMORY_RAW_SPAN_H_
34