Bug 1842773 - Part 33: Structured clone support for resizable ArrayBuffers. r=sfink
[gecko.git] / mfbt / Span.h
blobd9ba1af22054e9e7dbd6bac0c773867e7dfdb10e
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2015 Microsoft Corporation. All rights reserved.
4 //
5 // This code is licensed under the MIT License (MIT).
6 //
7 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
8 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
10 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
11 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
12 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
13 // THE SOFTWARE.
15 ///////////////////////////////////////////////////////////////////////////////
17 // Adapted from
18 // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span
19 // and
20 // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util
22 #ifndef mozilla_Span_h
23 #define mozilla_Span_h
25 #include <array>
26 #include <cstddef>
27 #include <cstdint>
28 #include <iterator>
29 #include <limits>
30 #include <string>
31 #include <type_traits>
32 #include <utility>
34 #include "mozilla/Assertions.h"
35 #include "mozilla/Attributes.h"
36 #include "mozilla/Casting.h"
37 #include "mozilla/UniquePtr.h"
39 namespace mozilla {
41 template <typename T, size_t Length>
42 class Array;
44 // Stuff from gsl_util
46 // narrow_cast(): a searchable way to do narrowing casts of values
47 template <class T, class U>
48 inline constexpr T narrow_cast(U&& u) {
49 return static_cast<T>(std::forward<U>(u));
52 // end gsl_util
54 // [views.constants], constants
55 // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
56 // and reserving a magic value that realistically doesn't occur in
57 // compile-time-constant Span sizes makes things a lot less messy in terms of
58 // comparison between signed and unsigned.
59 constexpr const size_t dynamic_extent = std::numeric_limits<size_t>::max();
61 template <class ElementType, size_t Extent = dynamic_extent>
62 class Span;
64 // implementation details
65 namespace span_details {
67 template <class T>
68 struct is_span_oracle : std::false_type {};
70 template <class ElementType, size_t Extent>
71 struct is_span_oracle<mozilla::Span<ElementType, Extent>> : std::true_type {};
73 template <class T>
74 struct is_span : public is_span_oracle<std::remove_cv_t<T>> {};
76 template <class T>
77 struct is_std_array_oracle : std::false_type {};
79 template <class ElementType, size_t Extent>
80 struct is_std_array_oracle<std::array<ElementType, Extent>> : std::true_type {};
82 template <class T>
83 struct is_std_array : public is_std_array_oracle<std::remove_cv_t<T>> {};
85 template <size_t From, size_t To>
86 struct is_allowed_extent_conversion
87 : public std::integral_constant<bool, From == To ||
88 From == mozilla::dynamic_extent ||
89 To == mozilla::dynamic_extent> {};
91 template <class From, class To>
92 struct is_allowed_element_type_conversion
93 : public std::integral_constant<
94 bool, std::is_convertible_v<From (*)[], To (*)[]>> {};
96 struct SpanKnownBounds {};
98 template <class SpanT, bool IsConst>
99 class span_iterator {
100 using element_type_ = typename SpanT::element_type;
102 template <class ElementType, size_t Extent>
103 friend class ::mozilla::Span;
105 public:
106 using iterator_category = std::random_access_iterator_tag;
107 using value_type = std::remove_const_t<element_type_>;
108 using difference_type = ptrdiff_t;
110 using reference =
111 std::conditional_t<IsConst, const element_type_, element_type_>&;
112 using pointer = std::add_pointer_t<reference>;
114 constexpr span_iterator() : span_iterator(nullptr, 0, SpanKnownBounds{}) {}
116 constexpr span_iterator(const SpanT* span, typename SpanT::index_type index)
117 : span_(span), index_(index) {
118 MOZ_RELEASE_ASSERT(span == nullptr ||
119 (index_ >= 0 && index <= span_->Length()));
122 private:
123 // For whatever reason, the compiler doesn't like optimizing away the above
124 // MOZ_RELEASE_ASSERT when `span_iterator` is constructed for
125 // obviously-correct cases like `span.begin()` or `span.end()`. We provide
126 // this private constructor for such cases.
127 constexpr span_iterator(const SpanT* span, typename SpanT::index_type index,
128 SpanKnownBounds)
129 : span_(span), index_(index) {}
131 public:
132 // `other` is already correct by construction; we do not need to go through
133 // the release assert above. Put differently, this constructor is effectively
134 // a copy constructor and therefore needs no assertions.
135 friend class span_iterator<SpanT, true>;
136 constexpr MOZ_IMPLICIT span_iterator(const span_iterator<SpanT, false>& other)
137 : span_(other.span_), index_(other.index_) {}
139 constexpr span_iterator<SpanT, IsConst>& operator=(
140 const span_iterator<SpanT, IsConst>&) = default;
142 constexpr reference operator*() const {
143 MOZ_RELEASE_ASSERT(span_);
144 return (*span_)[index_];
147 constexpr pointer operator->() const {
148 MOZ_RELEASE_ASSERT(span_);
149 return &((*span_)[index_]);
152 constexpr span_iterator& operator++() {
153 ++index_;
154 return *this;
157 constexpr span_iterator operator++(int) {
158 auto ret = *this;
159 ++(*this);
160 return ret;
163 constexpr span_iterator& operator--() {
164 --index_;
165 return *this;
168 constexpr span_iterator operator--(int) {
169 auto ret = *this;
170 --(*this);
171 return ret;
174 constexpr span_iterator operator+(difference_type n) const {
175 auto ret = *this;
176 return ret += n;
179 constexpr span_iterator& operator+=(difference_type n) {
180 MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
181 (index_ + n) <= span_->Length());
182 index_ += n;
183 return *this;
186 constexpr span_iterator operator-(difference_type n) const {
187 auto ret = *this;
188 return ret -= n;
191 constexpr span_iterator& operator-=(difference_type n) { return *this += -n; }
193 constexpr difference_type operator-(const span_iterator& rhs) const {
194 MOZ_RELEASE_ASSERT(span_ == rhs.span_);
195 return index_ - rhs.index_;
198 constexpr reference operator[](difference_type n) const {
199 return *(*this + n);
202 constexpr friend bool operator==(const span_iterator& lhs,
203 const span_iterator& rhs) {
204 // Iterators from different spans are uncomparable. A diagnostic assertion
205 // should be enough to check this, though. To ensure that no iterators from
206 // different spans are ever considered equal, still compare them in release
207 // builds.
208 MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_);
209 return lhs.index_ == rhs.index_ && lhs.span_ == rhs.span_;
212 constexpr friend bool operator!=(const span_iterator& lhs,
213 const span_iterator& rhs) {
214 return !(lhs == rhs);
217 constexpr friend bool operator<(const span_iterator& lhs,
218 const span_iterator& rhs) {
219 MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_);
220 return lhs.index_ < rhs.index_;
223 constexpr friend bool operator<=(const span_iterator& lhs,
224 const span_iterator& rhs) {
225 return !(rhs < lhs);
228 constexpr friend bool operator>(const span_iterator& lhs,
229 const span_iterator& rhs) {
230 return rhs < lhs;
233 constexpr friend bool operator>=(const span_iterator& lhs,
234 const span_iterator& rhs) {
235 return !(rhs > lhs);
238 void swap(span_iterator& rhs) {
239 std::swap(index_, rhs.index_);
240 std::swap(span_, rhs.span_);
243 protected:
244 const SpanT* span_;
245 size_t index_;
248 template <class Span, bool IsConst>
249 inline constexpr span_iterator<Span, IsConst> operator+(
250 typename span_iterator<Span, IsConst>::difference_type n,
251 const span_iterator<Span, IsConst>& rhs) {
252 return rhs + n;
255 template <size_t Ext>
256 class extent_type {
257 public:
258 using index_type = size_t;
260 static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
262 constexpr extent_type() = default;
264 template <index_type Other>
265 constexpr MOZ_IMPLICIT extent_type(extent_type<Other> ext) {
266 static_assert(
267 Other == Ext || Other == dynamic_extent,
268 "Mismatch between fixed-size extent and size of initializing data.");
269 MOZ_RELEASE_ASSERT(ext.size() == Ext);
272 constexpr MOZ_IMPLICIT extent_type(index_type length) {
273 MOZ_RELEASE_ASSERT(length == Ext);
276 constexpr index_type size() const { return Ext; }
279 template <>
280 class extent_type<dynamic_extent> {
281 public:
282 using index_type = size_t;
284 template <index_type Other>
285 explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
287 explicit constexpr extent_type(index_type length) : size_(length) {}
289 constexpr index_type size() const { return size_; }
291 private:
292 index_type size_;
294 } // namespace span_details
297 * Span - slices for C++
299 * Span implements Rust's slice concept for C++. It's called "Span" instead of
300 * "Slice" to follow the naming used in C++ Core Guidelines.
302 * A Span wraps a pointer and a length that identify a non-owning view to a
303 * contiguous block of memory of objects of the same type. Various types,
304 * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
305 * mozilla::Range and contiguous standard-library containers, auto-convert
306 * into Spans when attempting to pass them as arguments to methods that take
307 * Spans. (Span itself autoconverts into mozilla::Range.)
309 * Like Rust's slices, Span provides safety against out-of-bounds access by
310 * performing run-time bound checks. However, unlike Rust's slices, Span
311 * cannot provide safety against use-after-free.
313 * (Note: Span is like Rust's slice only conceptually. Due to the lack of
314 * ABI guarantees, you should still decompose spans/slices to raw pointer
315 * and length parts when crossing the FFI. The Elements() and data() methods
316 * are guaranteed to return a non-null pointer even for zero-length spans,
317 * so the pointer can be used as a raw part of a Rust slice without further
318 * checks.)
320 * In addition to having constructors (with the support of deduction guides)
321 * that take various well-known types, a Span for an arbitrary type can be
322 * constructed from a pointer and a length or a pointer and another pointer
323 * pointing just past the last element.
325 * A Span<const char> or Span<const char16_t> can be obtained for const char*
326 * or const char16_t pointing to a zero-terminated string using the
327 * MakeStringSpan() function (which treats a nullptr argument equivalently
328 * to the empty string). Corresponding implicit constructor does not exist
329 * in order to avoid accidental construction in cases where const char* or
330 * const char16_t* do not point to a zero-terminated string.
332 * Span has methods that follow the Mozilla naming style and methods that
333 * don't. The methods that follow the Mozilla naming style are meant to be
334 * used directly from Mozilla code. The methods that don't are meant for
335 * integration with C++11 range-based loops and with meta-programming that
336 * expects the same methods that are found on the standard-library
337 * containers. For example, to decompose a Span into its parts in Mozilla
338 * code, use Elements() and Length() (as with nsTArray) instead of data()
339 * and size() (as with std::vector).
341 * The pointer and length wrapped by a Span cannot be changed after a Span has
342 * been created. When new values are required, simply create a new Span. Span
343 * has a method called Subspan() that works analogously to the Substring()
344 * method of XPCOM strings taking a start index and an optional length. As a
345 * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
346 * based on), Span has methods From(start), To(end) and FromTo(start, end)
347 * that correspond to Rust's &slice[start..], &slice[..end] and
348 * &slice[start..end], respectively. (That is, the end index is the index of
349 * the first element not to be included in the new subspan.)
351 * When indicating a Span that's only read from, const goes inside the type
352 * parameter. Don't put const in front of Span. That is:
353 * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
354 * Span<uint8_t> aWrittenTo);
356 * Any Span<const T> can be viewed as Span<const uint8_t> using the function
357 * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
358 * AsWritableBytes().
360 * Note that iterators from different Span instances are uncomparable, even if
361 * they refer to the same memory. This also applies to any spans derived via
362 * Subspan etc.
364 template <class ElementType, size_t Extent /* = dynamic_extent */>
365 class Span {
366 public:
367 // constants and types
368 using element_type = ElementType;
369 using value_type = std::remove_cv_t<element_type>;
370 using index_type = size_t;
371 using pointer = element_type*;
372 using reference = element_type&;
374 using iterator =
375 span_details::span_iterator<Span<ElementType, Extent>, false>;
376 using const_iterator =
377 span_details::span_iterator<Span<ElementType, Extent>, true>;
378 using reverse_iterator = std::reverse_iterator<iterator>;
379 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
381 constexpr static const index_type extent = Extent;
383 // [Span.cons], Span constructors, copy, assignment, and destructor
384 // "Dependent" is needed to make "std::enable_if_t<(Dependent ||
385 // Extent == 0 || Extent == dynamic_extent)>" SFINAE,
386 // since
387 // "std::enable_if_t<(Extent == 0 || Extent == dynamic_extent)>" is
388 // ill-formed when Extent is neither of the extreme values.
390 * Constructor with no args.
392 template <bool Dependent = false,
393 class = std::enable_if_t<(Dependent || Extent == 0 ||
394 Extent == dynamic_extent)>>
395 constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {}
398 * Constructor for nullptr.
400 constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
403 * Constructor for pointer and length.
405 constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
408 * Constructor for start pointer and pointer past end.
410 constexpr Span(pointer aStartPtr, pointer aEndPtr)
411 : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
414 * Constructor for pair of Span iterators.
416 template <typename OtherElementType, size_t OtherExtent, bool IsConst>
417 constexpr Span(
418 span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst>
419 aBegin,
420 span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst>
421 aEnd)
422 : storage_(aBegin == aEnd ? nullptr : &*aBegin, aEnd - aBegin) {}
425 * Constructor for {iterator,size_t}
427 template <typename OtherElementType, size_t OtherExtent, bool IsConst>
428 constexpr Span(
429 span_details::span_iterator<Span<OtherElementType, OtherExtent>, IsConst>
430 aBegin,
431 index_type aLength)
432 : storage_(!aLength ? nullptr : &*aBegin, aLength) {}
435 * Constructor for C array.
437 template <size_t N>
438 constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
439 : storage_(&aArr[0], span_details::extent_type<N>()) {}
441 // Implicit constructors for char* and char16_t* pointers are deleted in order
442 // to avoid accidental construction in cases where a pointer does not point to
443 // a zero-terminated string. A Span<const char> or Span<const char16_t> can be
444 // obtained for const char* or const char16_t pointing to a zero-terminated
445 // string using the MakeStringSpan() function.
446 // (This must be a template because otherwise it will prevent the previous
447 // array constructor to match because an array decays to a pointer. This only
448 // exists to point to the above explanation, since there's no other
449 // constructor that would match.)
450 template <
451 typename T,
452 typename = std::enable_if_t<
453 std::is_pointer_v<T> &&
454 (std::is_same_v<std::remove_const_t<std::decay_t<T>>, char> ||
455 std::is_same_v<std::remove_const_t<std::decay_t<T>>, char16_t>)>>
456 Span(T& aStr) = delete;
459 * Constructor for std::array.
461 template <size_t N,
462 class ArrayElementType = std::remove_const_t<element_type>>
463 constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
464 : storage_(&aArr[0], span_details::extent_type<N>()) {}
467 * Constructor for const std::array.
469 template <size_t N>
470 constexpr MOZ_IMPLICIT Span(
471 const std::array<std::remove_const_t<element_type>, N>& aArr)
472 : storage_(&aArr[0], span_details::extent_type<N>()) {}
475 * Constructor for mozilla::Array.
477 template <size_t N,
478 class ArrayElementType = std::remove_const_t<element_type>>
479 constexpr MOZ_IMPLICIT Span(mozilla::Array<ArrayElementType, N>& aArr)
480 : storage_(&aArr[0], span_details::extent_type<N>()) {}
483 * Constructor for const mozilla::Array.
485 template <size_t N>
486 constexpr MOZ_IMPLICIT Span(
487 const mozilla::Array<std::remove_const_t<element_type>, N>& aArr)
488 : storage_(&aArr[0], span_details::extent_type<N>()) {}
491 * Constructor for mozilla::UniquePtr holding an array and length.
493 template <class ArrayElementType = std::add_pointer<element_type>,
494 class DeleterType>
495 constexpr Span(const mozilla::UniquePtr<ArrayElementType, DeleterType>& aPtr,
496 index_type aLength)
497 : storage_(aPtr.get(), aLength) {}
499 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
500 // requirement on Container to be a contiguous sequence container.
502 * Constructor for standard-library containers.
504 template <
505 class Container,
506 class Dummy = std::enable_if_t<
507 !std::is_const_v<Container> &&
508 !span_details::is_span<Container>::value &&
509 !span_details::is_std_array<Container>::value &&
510 std::is_convertible_v<typename Container::pointer, pointer> &&
511 std::is_convertible_v<typename Container::pointer,
512 decltype(std::declval<Container>().data())>,
513 Container>>
514 constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr)
515 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
518 * Constructor for standard-library containers (const version).
520 template <
521 class Container,
522 class = std::enable_if_t<
523 std::is_const_v<element_type> &&
524 !span_details::is_span<Container>::value &&
525 std::is_convertible_v<typename Container::pointer, pointer> &&
526 std::is_convertible_v<typename Container::pointer,
527 decltype(std::declval<Container>().data())>>>
528 constexpr MOZ_IMPLICIT Span(const Container& cont)
529 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
531 // NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for
532 // the requirement on Container to be a contiguous sequence container.
534 * Constructor for contiguous Mozilla containers.
536 template <
537 class Container,
538 class = std::enable_if_t<
539 !std::is_const_v<Container> &&
540 !span_details::is_span<Container>::value &&
541 !span_details::is_std_array<Container>::value &&
542 std::is_convertible_v<typename Container::value_type*, pointer> &&
543 std::is_convertible_v<
544 typename Container::value_type*,
545 decltype(std::declval<Container>().Elements())>>>
546 constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr)
547 : Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
550 * Constructor for contiguous Mozilla containers (const version).
552 template <
553 class Container,
554 class = std::enable_if_t<
555 std::is_const_v<element_type> &&
556 !span_details::is_span<Container>::value &&
557 std::is_convertible_v<typename Container::value_type*, pointer> &&
558 std::is_convertible_v<
559 typename Container::value_type*,
560 decltype(std::declval<Container>().Elements())>>>
561 constexpr MOZ_IMPLICIT Span(const Container& cont, void* = nullptr)
562 : Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
565 * Constructor from other Span.
567 constexpr Span(const Span& other) = default;
570 * Constructor from other Span.
572 constexpr Span(Span&& other) = default;
575 * Constructor from other Span with conversion of element type.
577 template <
578 class OtherElementType, size_t OtherExtent,
579 class = std::enable_if_t<span_details::is_allowed_extent_conversion<
580 OtherExtent, Extent>::value &&
581 span_details::is_allowed_element_type_conversion<
582 OtherElementType, element_type>::value>>
583 constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
584 : storage_(other.data(),
585 span_details::extent_type<OtherExtent>(other.size())) {}
588 * Constructor from other Span with conversion of element type.
590 template <
591 class OtherElementType, size_t OtherExtent,
592 class = std::enable_if_t<span_details::is_allowed_extent_conversion<
593 OtherExtent, Extent>::value &&
594 span_details::is_allowed_element_type_conversion<
595 OtherElementType, element_type>::value>>
596 constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
597 : storage_(other.data(),
598 span_details::extent_type<OtherExtent>(other.size())) {}
600 ~Span() = default;
601 constexpr Span& operator=(const Span& other) = default;
603 constexpr Span& operator=(Span&& other) = default;
605 // [Span.sub], Span subviews
607 * Subspan with first N elements with compile-time N.
609 template <size_t Count>
610 constexpr Span<element_type, Count> First() const {
611 MOZ_RELEASE_ASSERT(Count <= size());
612 return {data(), Count};
616 * Subspan with last N elements with compile-time N.
618 template <size_t Count>
619 constexpr Span<element_type, Count> Last() const {
620 const size_t len = size();
621 MOZ_RELEASE_ASSERT(Count <= len);
622 return {data() + (len - Count), Count};
626 * Subspan with compile-time start index and length.
628 template <size_t Offset, size_t Count = dynamic_extent>
629 constexpr Span<element_type, Count> Subspan() const {
630 const size_t len = size();
631 MOZ_RELEASE_ASSERT(Offset <= len &&
632 (Count == dynamic_extent || (Offset + Count <= len)));
633 return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
637 * Subspan with first N elements with run-time N.
639 constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
640 MOZ_RELEASE_ASSERT(aCount <= size());
641 return {data(), aCount};
645 * Subspan with last N elements with run-time N.
647 constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const {
648 const size_t len = size();
649 MOZ_RELEASE_ASSERT(aCount <= len);
650 return {data() + (len - aCount), aCount};
654 * Subspan with run-time start index and length.
656 constexpr Span<element_type, dynamic_extent> Subspan(
657 index_type aStart, index_type aLength = dynamic_extent) const {
658 const size_t len = size();
659 MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
660 (aStart + aLength <= len)));
661 return {data() + aStart,
662 aLength == dynamic_extent ? len - aStart : aLength};
666 * Subspan with run-time start index. (Rust's &foo[start..])
668 constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
669 return Subspan(aStart);
673 * Subspan with run-time exclusive end index. (Rust's &foo[..end])
675 constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
676 return Subspan(0, aEnd);
679 /// std::span-compatible method name
680 constexpr auto subspan(index_type aStart,
681 index_type aLength = dynamic_extent) const {
682 return Subspan(aStart, aLength);
684 /// std::span-compatible method name
685 constexpr auto from(index_type aStart) const { return From(aStart); }
686 /// std::span-compatible method name
687 constexpr auto to(index_type aEnd) const { return To(aEnd); }
690 * Subspan with run-time start index and exclusive end index.
691 * (Rust's &foo[start..end])
693 constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart,
694 index_type aEnd) const {
695 MOZ_RELEASE_ASSERT(aStart <= aEnd);
696 return Subspan(aStart, aEnd - aStart);
699 // [Span.obs], Span observers
701 * Number of elements in the span.
703 constexpr index_type Length() const { return size(); }
706 * Number of elements in the span (standard-libray duck typing version).
708 constexpr index_type size() const { return storage_.size(); }
711 * Size of the span in bytes.
713 constexpr index_type LengthBytes() const { return size_bytes(); }
716 * Size of the span in bytes (standard-library naming style version).
718 constexpr index_type size_bytes() const {
719 return size() * narrow_cast<index_type>(sizeof(element_type));
723 * Checks if the the length of the span is zero.
725 constexpr bool IsEmpty() const { return empty(); }
728 * Checks if the the length of the span is zero (standard-libray duck
729 * typing version).
731 constexpr bool empty() const { return size() == 0; }
733 // [Span.elem], Span element access
734 constexpr reference operator[](index_type idx) const {
735 MOZ_RELEASE_ASSERT(idx < storage_.size());
736 return data()[idx];
740 * Access element of span by index (standard-library duck typing version).
742 constexpr reference at(index_type idx) const { return this->operator[](idx); }
744 constexpr reference operator()(index_type idx) const {
745 return this->operator[](idx);
749 * Pointer to the first element of the span. The return value is never
750 * nullptr, not ever for zero-length spans, so it can be passed as-is
751 * to std::slice::from_raw_parts() in Rust.
753 constexpr pointer Elements() const { return data(); }
756 * Pointer to the first element of the span (standard-libray duck typing
757 * version). The return value is never nullptr, not ever for zero-length
758 * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
760 constexpr pointer data() const { return storage_.data(); }
762 // [Span.iter], Span iterator support
763 iterator begin() const { return {this, 0, span_details::SpanKnownBounds{}}; }
764 iterator end() const {
765 return {this, Length(), span_details::SpanKnownBounds{}};
768 const_iterator cbegin() const {
769 return {this, 0, span_details::SpanKnownBounds{}};
771 const_iterator cend() const {
772 return {this, Length(), span_details::SpanKnownBounds{}};
775 reverse_iterator rbegin() const { return reverse_iterator{end()}; }
776 reverse_iterator rend() const { return reverse_iterator{begin()}; }
778 const_reverse_iterator crbegin() const {
779 return const_reverse_iterator{cend()};
781 const_reverse_iterator crend() const {
782 return const_reverse_iterator{cbegin()};
785 template <size_t SplitPoint>
786 constexpr std::pair<Span<ElementType, SplitPoint>,
787 Span<ElementType, Extent - SplitPoint>>
788 SplitAt() const {
789 static_assert(Extent != dynamic_extent);
790 static_assert(SplitPoint <= Extent);
791 return {First<SplitPoint>(), Last<Extent - SplitPoint>()};
794 constexpr std::pair<Span<ElementType, dynamic_extent>,
795 Span<ElementType, dynamic_extent>>
796 SplitAt(const index_type aSplitPoint) const {
797 MOZ_RELEASE_ASSERT(aSplitPoint <= Length());
798 return {First(aSplitPoint), Last(Length() - aSplitPoint)};
801 constexpr Span<std::add_const_t<ElementType>, Extent> AsConst() const {
802 return {Elements(), Length()};
805 private:
806 // this implementation detail class lets us take advantage of the
807 // empty base class optimization to pay for only storage of a single
808 // pointer in the case of fixed-size Spans
809 template <class ExtentType>
810 class storage_type : public ExtentType {
811 public:
812 template <class OtherExtentType>
813 constexpr storage_type(pointer elements, OtherExtentType ext)
814 : ExtentType(ext)
815 // Replace nullptr with aligned bogus pointer for Rust slice
816 // compatibility. See
817 // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
819 data_(elements ? elements
820 : reinterpret_cast<pointer>(alignof(element_type))) {
821 const size_t extentSize = ExtentType::size();
822 MOZ_RELEASE_ASSERT((!elements && extentSize == 0) ||
823 (elements && extentSize != dynamic_extent));
826 constexpr pointer data() const { return data_; }
828 private:
829 pointer data_;
832 storage_type<span_details::extent_type<Extent>> storage_;
835 template <typename T, size_t OtherExtent, bool IsConst>
836 Span(span_details::span_iterator<Span<T, OtherExtent>, IsConst> aBegin,
837 span_details::span_iterator<Span<T, OtherExtent>, IsConst> aEnd)
838 -> Span<std::conditional_t<IsConst, std::add_const_t<T>, T>>;
840 template <typename T, size_t Extent>
841 Span(T (&)[Extent]) -> Span<T, Extent>;
843 template <class Container>
844 Span(Container&) -> Span<typename Container::value_type>;
846 template <class Container>
847 Span(const Container&) -> Span<const typename Container::value_type>;
849 template <typename T, size_t Extent>
850 Span(mozilla::Array<T, Extent>&) -> Span<T, Extent>;
852 template <typename T, size_t Extent>
853 Span(const mozilla::Array<T, Extent>&) -> Span<const T, Extent>;
855 // [Span.comparison], Span comparison operators
856 template <class ElementType, size_t FirstExtent, size_t SecondExtent>
857 inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l,
858 const Span<ElementType, SecondExtent>& r) {
859 return (l.size() == r.size()) &&
860 std::equal(l.data(), l.data() + l.size(), r.data());
863 template <class ElementType, size_t Extent>
864 inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
865 const Span<ElementType, Extent>& r) {
866 return !(l == r);
869 template <class ElementType, size_t Extent>
870 inline constexpr bool operator<(const Span<ElementType, Extent>& l,
871 const Span<ElementType, Extent>& r) {
872 return std::lexicographical_compare(l.data(), l.data() + l.size(), r.data(),
873 r.data() + r.size());
876 template <class ElementType, size_t Extent>
877 inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
878 const Span<ElementType, Extent>& r) {
879 return !(l > r);
882 template <class ElementType, size_t Extent>
883 inline constexpr bool operator>(const Span<ElementType, Extent>& l,
884 const Span<ElementType, Extent>& r) {
885 return r < l;
888 template <class ElementType, size_t Extent>
889 inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
890 const Span<ElementType, Extent>& r) {
891 return !(l < r);
894 namespace span_details {
895 // if we only supported compilers with good constexpr support then
896 // this pair of classes could collapse down to a constexpr function
898 // we should use a narrow_cast<> to go to size_t, but older compilers may not
899 // see it as constexpr and so will fail compilation of the template
900 template <class ElementType, size_t Extent>
901 struct calculate_byte_size
902 : std::integral_constant<size_t,
903 static_cast<size_t>(sizeof(ElementType) *
904 static_cast<size_t>(Extent))> {
907 template <class ElementType>
908 struct calculate_byte_size<ElementType, dynamic_extent>
909 : std::integral_constant<size_t, dynamic_extent> {};
910 } // namespace span_details
912 // [Span.objectrep], views of object representation
914 * View span as Span<const uint8_t>.
916 template <class ElementType, size_t Extent>
917 Span<const uint8_t,
918 span_details::calculate_byte_size<ElementType, Extent>::value>
919 AsBytes(Span<ElementType, Extent> s) {
920 return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
924 * View span as Span<uint8_t>.
926 template <class ElementType, size_t Extent,
927 class = std::enable_if_t<!std::is_const_v<ElementType>>>
928 Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
929 AsWritableBytes(Span<ElementType, Extent> s) {
930 return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
934 * View a span of uint8_t as a span of char.
936 inline Span<const char> AsChars(Span<const uint8_t> s) {
937 return {reinterpret_cast<const char*>(s.data()), s.size()};
941 * View a writable span of uint8_t as a span of char.
943 inline Span<char> AsWritableChars(Span<uint8_t> s) {
944 return {reinterpret_cast<char*>(s.data()), s.size()};
948 * Create span from a zero-terminated C string. nullptr is
949 * treated as the empty string.
951 constexpr Span<const char> MakeStringSpan(const char* aZeroTerminated) {
952 if (!aZeroTerminated) {
953 return Span<const char>();
955 return Span<const char>(aZeroTerminated,
956 std::char_traits<char>::length(aZeroTerminated));
960 * Create span from a zero-terminated UTF-16 C string. nullptr is
961 * treated as the empty string.
963 constexpr Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
964 if (!aZeroTerminated) {
965 return Span<const char16_t>();
967 return Span<const char16_t>(
968 aZeroTerminated, std::char_traits<char16_t>::length(aZeroTerminated));
971 } // namespace mozilla
973 #endif // mozilla_Span_h