Bug 1799258 - Do color-management on Windows+DComp via IDCompositionFilterEffects...
[gecko.git] / mfbt / Span.h
blob8a8f2041bdaa5b42dfbc7aa61ab7f522c2d3a75c
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 = typename SpanT::index_type;
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 constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
495 index_type aLength)
496 : storage_(aPtr.get(), aLength) {}
498 // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
499 // requirement on Container to be a contiguous sequence container.
501 * Constructor for standard-library containers.
503 template <
504 class Container,
505 class Dummy = std::enable_if_t<
506 !std::is_const_v<Container> &&
507 !span_details::is_span<Container>::value &&
508 !span_details::is_std_array<Container>::value &&
509 std::is_convertible_v<typename Container::pointer, pointer> &&
510 std::is_convertible_v<typename Container::pointer,
511 decltype(std::declval<Container>().data())>,
512 Container>>
513 constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr)
514 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
517 * Constructor for standard-library containers (const version).
519 template <
520 class Container,
521 class = std::enable_if_t<
522 std::is_const_v<element_type> &&
523 !span_details::is_span<Container>::value &&
524 std::is_convertible_v<typename Container::pointer, pointer> &&
525 std::is_convertible_v<typename Container::pointer,
526 decltype(std::declval<Container>().data())>>>
527 constexpr MOZ_IMPLICIT Span(const Container& cont)
528 : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
530 // NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for
531 // the requirement on Container to be a contiguous sequence container.
533 * Constructor for contiguous Mozilla containers.
535 template <
536 class Container,
537 class = std::enable_if_t<
538 !std::is_const_v<Container> &&
539 !span_details::is_span<Container>::value &&
540 !span_details::is_std_array<Container>::value &&
541 std::is_convertible_v<typename Container::value_type*, pointer> &&
542 std::is_convertible_v<
543 typename Container::value_type*,
544 decltype(std::declval<Container>().Elements())>>>
545 constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr)
546 : Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
549 * Constructor for contiguous Mozilla containers (const version).
551 template <
552 class Container,
553 class = std::enable_if_t<
554 std::is_const_v<element_type> &&
555 !span_details::is_span<Container>::value &&
556 std::is_convertible_v<typename Container::value_type*, pointer> &&
557 std::is_convertible_v<
558 typename Container::value_type*,
559 decltype(std::declval<Container>().Elements())>>>
560 constexpr MOZ_IMPLICIT Span(const Container& cont, void* = nullptr)
561 : Span(cont.Elements(), ReleaseAssertedCast<index_type>(cont.Length())) {}
564 * Constructor from other Span.
566 constexpr Span(const Span& other) = default;
569 * Constructor from other Span.
571 constexpr Span(Span&& other) = default;
574 * Constructor from other Span with conversion of element type.
576 template <
577 class OtherElementType, size_t OtherExtent,
578 class = std::enable_if_t<span_details::is_allowed_extent_conversion<
579 OtherExtent, Extent>::value &&
580 span_details::is_allowed_element_type_conversion<
581 OtherElementType, element_type>::value>>
582 constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
583 : storage_(other.data(),
584 span_details::extent_type<OtherExtent>(other.size())) {}
587 * Constructor from other Span with conversion of element type.
589 template <
590 class OtherElementType, size_t OtherExtent,
591 class = std::enable_if_t<span_details::is_allowed_extent_conversion<
592 OtherExtent, Extent>::value &&
593 span_details::is_allowed_element_type_conversion<
594 OtherElementType, element_type>::value>>
595 constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
596 : storage_(other.data(),
597 span_details::extent_type<OtherExtent>(other.size())) {}
599 ~Span() = default;
600 constexpr Span& operator=(const Span& other) = default;
602 constexpr Span& operator=(Span&& other) = default;
604 // [Span.sub], Span subviews
606 * Subspan with first N elements with compile-time N.
608 template <size_t Count>
609 constexpr Span<element_type, Count> First() const {
610 MOZ_RELEASE_ASSERT(Count <= size());
611 return {data(), Count};
615 * Subspan with last N elements with compile-time N.
617 template <size_t Count>
618 constexpr Span<element_type, Count> Last() const {
619 const size_t len = size();
620 MOZ_RELEASE_ASSERT(Count <= len);
621 return {data() + (len - Count), Count};
625 * Subspan with compile-time start index and length.
627 template <size_t Offset, size_t Count = dynamic_extent>
628 constexpr Span<element_type, Count> Subspan() const {
629 const size_t len = size();
630 MOZ_RELEASE_ASSERT(Offset <= len &&
631 (Count == dynamic_extent || (Offset + Count <= len)));
632 return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
636 * Subspan with first N elements with run-time N.
638 constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
639 MOZ_RELEASE_ASSERT(aCount <= size());
640 return {data(), aCount};
644 * Subspan with last N elements with run-time N.
646 constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const {
647 const size_t len = size();
648 MOZ_RELEASE_ASSERT(aCount <= len);
649 return {data() + (len - aCount), aCount};
653 * Subspan with run-time start index and length.
655 constexpr Span<element_type, dynamic_extent> Subspan(
656 index_type aStart, index_type aLength = dynamic_extent) const {
657 const size_t len = size();
658 MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
659 (aStart + aLength <= len)));
660 return {data() + aStart,
661 aLength == dynamic_extent ? len - aStart : aLength};
665 * Subspan with run-time start index. (Rust's &foo[start..])
667 constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
668 return Subspan(aStart);
672 * Subspan with run-time exclusive end index. (Rust's &foo[..end])
674 constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
675 return Subspan(0, aEnd);
678 /// std::span-compatible method name
679 constexpr auto subspan(index_type aStart,
680 index_type aLength = dynamic_extent) const {
681 return Subspan(aStart, aLength);
683 /// std::span-compatible method name
684 constexpr auto from(index_type aStart) const { return From(aStart); }
685 /// std::span-compatible method name
686 constexpr auto to(index_type aEnd) const { return To(aEnd); }
689 * Subspan with run-time start index and exclusive end index.
690 * (Rust's &foo[start..end])
692 constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart,
693 index_type aEnd) const {
694 MOZ_RELEASE_ASSERT(aStart <= aEnd);
695 return Subspan(aStart, aEnd - aStart);
698 // [Span.obs], Span observers
700 * Number of elements in the span.
702 constexpr index_type Length() const { return size(); }
705 * Number of elements in the span (standard-libray duck typing version).
707 constexpr index_type size() const { return storage_.size(); }
710 * Size of the span in bytes.
712 constexpr index_type LengthBytes() const { return size_bytes(); }
715 * Size of the span in bytes (standard-library naming style version).
717 constexpr index_type size_bytes() const {
718 return size() * narrow_cast<index_type>(sizeof(element_type));
722 * Checks if the the length of the span is zero.
724 constexpr bool IsEmpty() const { return empty(); }
727 * Checks if the the length of the span is zero (standard-libray duck
728 * typing version).
730 constexpr bool empty() const { return size() == 0; }
732 // [Span.elem], Span element access
733 constexpr reference operator[](index_type idx) const {
734 MOZ_RELEASE_ASSERT(idx < storage_.size());
735 return data()[idx];
739 * Access element of span by index (standard-library duck typing version).
741 constexpr reference at(index_type idx) const { return this->operator[](idx); }
743 constexpr reference operator()(index_type idx) const {
744 return this->operator[](idx);
748 * Pointer to the first element of the span. The return value is never
749 * nullptr, not ever for zero-length spans, so it can be passed as-is
750 * to std::slice::from_raw_parts() in Rust.
752 constexpr pointer Elements() const { return data(); }
755 * Pointer to the first element of the span (standard-libray duck typing
756 * version). The return value is never nullptr, not ever for zero-length
757 * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
759 constexpr pointer data() const { return storage_.data(); }
761 // [Span.iter], Span iterator support
762 iterator begin() const { return {this, 0, span_details::SpanKnownBounds{}}; }
763 iterator end() const {
764 return {this, Length(), span_details::SpanKnownBounds{}};
767 const_iterator cbegin() const {
768 return {this, 0, span_details::SpanKnownBounds{}};
770 const_iterator cend() const {
771 return {this, Length(), span_details::SpanKnownBounds{}};
774 reverse_iterator rbegin() const { return reverse_iterator{end()}; }
775 reverse_iterator rend() const { return reverse_iterator{begin()}; }
777 const_reverse_iterator crbegin() const {
778 return const_reverse_iterator{cend()};
780 const_reverse_iterator crend() const {
781 return const_reverse_iterator{cbegin()};
784 template <size_t SplitPoint>
785 constexpr std::pair<Span<ElementType, SplitPoint>,
786 Span<ElementType, Extent - SplitPoint>>
787 SplitAt() const {
788 static_assert(Extent != dynamic_extent);
789 static_assert(SplitPoint <= Extent);
790 return {First<SplitPoint>(), Last<Extent - SplitPoint>()};
793 constexpr std::pair<Span<ElementType, dynamic_extent>,
794 Span<ElementType, dynamic_extent>>
795 SplitAt(const index_type aSplitPoint) const {
796 MOZ_RELEASE_ASSERT(aSplitPoint <= Length());
797 return {First(aSplitPoint), Last(Length() - aSplitPoint)};
800 constexpr Span<std::add_const_t<ElementType>, Extent> AsConst() const {
801 return {Elements(), Length()};
804 private:
805 // this implementation detail class lets us take advantage of the
806 // empty base class optimization to pay for only storage of a single
807 // pointer in the case of fixed-size Spans
808 template <class ExtentType>
809 class storage_type : public ExtentType {
810 public:
811 template <class OtherExtentType>
812 constexpr storage_type(pointer elements, OtherExtentType ext)
813 : ExtentType(ext)
814 // Replace nullptr with aligned bogus pointer for Rust slice
815 // compatibility. See
816 // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
818 data_(elements ? elements
819 : reinterpret_cast<pointer>(alignof(element_type))) {
820 const size_t extentSize = ExtentType::size();
821 MOZ_RELEASE_ASSERT((!elements && extentSize == 0) ||
822 (elements && extentSize != dynamic_extent));
825 constexpr pointer data() const { return data_; }
827 private:
828 pointer data_;
831 storage_type<span_details::extent_type<Extent>> storage_;
834 template <typename T, size_t OtherExtent, bool IsConst>
835 Span(span_details::span_iterator<Span<T, OtherExtent>, IsConst> aBegin,
836 span_details::span_iterator<Span<T, OtherExtent>, IsConst> aEnd)
837 -> Span<std::conditional_t<IsConst, std::add_const_t<T>, T>>;
839 template <typename T, size_t Extent>
840 Span(T (&)[Extent]) -> Span<T, Extent>;
842 template <class Container>
843 Span(Container&) -> Span<typename Container::value_type>;
845 template <class Container>
846 Span(const Container&) -> Span<const typename Container::value_type>;
848 template <typename T, size_t Extent>
849 Span(mozilla::Array<T, Extent>&) -> Span<T, Extent>;
851 template <typename T, size_t Extent>
852 Span(const mozilla::Array<T, Extent>&) -> Span<const T, Extent>;
854 // [Span.comparison], Span comparison operators
855 template <class ElementType, size_t FirstExtent, size_t SecondExtent>
856 inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l,
857 const Span<ElementType, SecondExtent>& r) {
858 return (l.size() == r.size()) &&
859 std::equal(l.data(), l.data() + l.size(), r.data());
862 template <class ElementType, size_t Extent>
863 inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
864 const Span<ElementType, Extent>& r) {
865 return !(l == r);
868 template <class ElementType, size_t Extent>
869 inline constexpr bool operator<(const Span<ElementType, Extent>& l,
870 const Span<ElementType, Extent>& r) {
871 return std::lexicographical_compare(l.data(), l.data() + l.size(), r.data(),
872 r.data() + r.size());
875 template <class ElementType, size_t Extent>
876 inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
877 const Span<ElementType, Extent>& r) {
878 return !(l > r);
881 template <class ElementType, size_t Extent>
882 inline constexpr bool operator>(const Span<ElementType, Extent>& l,
883 const Span<ElementType, Extent>& r) {
884 return r < l;
887 template <class ElementType, size_t Extent>
888 inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
889 const Span<ElementType, Extent>& r) {
890 return !(l < r);
893 namespace span_details {
894 // if we only supported compilers with good constexpr support then
895 // this pair of classes could collapse down to a constexpr function
897 // we should use a narrow_cast<> to go to size_t, but older compilers may not
898 // see it as constexpr and so will fail compilation of the template
899 template <class ElementType, size_t Extent>
900 struct calculate_byte_size
901 : std::integral_constant<size_t,
902 static_cast<size_t>(sizeof(ElementType) *
903 static_cast<size_t>(Extent))> {
906 template <class ElementType>
907 struct calculate_byte_size<ElementType, dynamic_extent>
908 : std::integral_constant<size_t, dynamic_extent> {};
909 } // namespace span_details
911 // [Span.objectrep], views of object representation
913 * View span as Span<const uint8_t>.
915 template <class ElementType, size_t Extent>
916 Span<const uint8_t,
917 span_details::calculate_byte_size<ElementType, Extent>::value>
918 AsBytes(Span<ElementType, Extent> s) {
919 return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
923 * View span as Span<uint8_t>.
925 template <class ElementType, size_t Extent,
926 class = std::enable_if_t<!std::is_const_v<ElementType>>>
927 Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
928 AsWritableBytes(Span<ElementType, Extent> s) {
929 return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
933 * View a span of uint8_t as a span of char.
935 inline Span<const char> AsChars(Span<const uint8_t> s) {
936 return {reinterpret_cast<const char*>(s.data()), s.size()};
940 * View a writable span of uint8_t as a span of char.
942 inline Span<char> AsWritableChars(Span<uint8_t> s) {
943 return {reinterpret_cast<char*>(s.data()), s.size()};
947 * Create span from a zero-terminated C string. nullptr is
948 * treated as the empty string.
950 constexpr Span<const char> MakeStringSpan(const char* aZeroTerminated) {
951 if (!aZeroTerminated) {
952 return Span<const char>();
954 return Span<const char>(aZeroTerminated,
955 std::char_traits<char>::length(aZeroTerminated));
959 * Create span from a zero-terminated UTF-16 C string. nullptr is
960 * treated as the empty string.
962 constexpr Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
963 if (!aZeroTerminated) {
964 return Span<const char16_t>();
966 return Span<const char16_t>(
967 aZeroTerminated, std::char_traits<char16_t>::length(aZeroTerminated));
970 } // namespace mozilla
972 #endif // mozilla_Span_h