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.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
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
21 
22 #ifndef mozilla_Span_h
23 #define mozilla_Span_h
24 
25 #include "mozilla/Array.h"
26 #include "mozilla/Assertions.h"
27 #include "mozilla/Casting.h"
28 #include "mozilla/IntegerTypeTraits.h"
29 #include "mozilla/Move.h"
30 #include "mozilla/TypeTraits.h"
31 #include "mozilla/UniquePtr.h"
32 
33 #include <algorithm>
34 #include <array>
35 #include <cstring>
36 #include <iterator>
37 
38 // Classifications for reasons why constexpr was removed in C++14 to C++11
39 // conversion. Once we upgrade compilers, we can try defining each of these
40 // to constexpr to restore a category of constexprs at a time.
41 #if !defined(__clang__) && defined(__GNUC__) && __cpp_constexpr < 201304
42 #define MOZ_SPAN_ASSERTION_CONSTEXPR
43 #define MOZ_SPAN_GCC_CONSTEXPR
44 #define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
45 #define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
46 #define MOZ_SPAN_NON_CONST_CONSTEXPR
47 #else
48 #define MOZ_SPAN_ASSERTION_CONSTEXPR constexpr
49 #define MOZ_SPAN_GCC_CONSTEXPR constexpr
50 #define MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR constexpr
51 #define MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN constexpr
52 #define MOZ_SPAN_NON_CONST_CONSTEXPR constexpr
53 #endif
54 
55 #ifdef _MSC_VER
56 #pragma warning(push)
57 
58 // turn off some warnings that are noisy about our MOZ_RELEASE_ASSERT statements
59 #pragma warning(disable : 4127)  // conditional expression is constant
60 
61 // blanket turn off warnings from CppCoreCheck for now
62 // so people aren't annoyed by them when running the tool.
63 // more targeted suppressions will be added in a future update to the GSL
64 #pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495)
65 
66 #if _MSC_VER < 1910
67 #pragma push_macro("constexpr")
68 #define constexpr /*constexpr*/
69 
70 #endif  // _MSC_VER < 1910
71 #endif  // _MSC_VER
72 
73 namespace mozilla {
74 
75 // Stuff from gsl_util
76 
77 // narrow_cast(): a searchable way to do narrowing casts of values
78 template <class T, class U>
narrow_cast(U && u)79 inline constexpr T narrow_cast(U&& u) {
80   return static_cast<T>(mozilla::Forward<U>(u));
81 }
82 
83 // end gsl_util
84 
85 // [views.constants], constants
86 // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t
87 // and reserving a magic value that realistically doesn't occur in
88 // compile-time-constant Span sizes makes things a lot less messy in terms of
89 // comparison between signed and unsigned.
90 constexpr const size_t dynamic_extent = mozilla::MaxValue<size_t>::value;
91 
92 template <class ElementType, size_t Extent = dynamic_extent>
93 class Span;
94 
95 // implementation details
96 namespace span_details {
97 
strlen16(const char16_t * aZeroTerminated)98 inline size_t strlen16(const char16_t* aZeroTerminated) {
99   size_t len = 0;
100   while (*(aZeroTerminated++)) {
101     len++;
102   }
103   return len;
104 }
105 
106 // C++14 types that we don't have because we build as C++11.
107 template <class T>
108 using remove_cv_t = typename mozilla::RemoveCV<T>::Type;
109 template <class T>
110 using remove_const_t = typename mozilla::RemoveConst<T>::Type;
111 template <bool B, class T, class F>
112 using conditional_t = typename mozilla::Conditional<B, T, F>::Type;
113 template <class T>
114 using add_pointer_t = typename mozilla::AddPointer<T>::Type;
115 template <bool B, class T = void>
116 using enable_if_t = typename mozilla::EnableIf<B, T>::Type;
117 
118 template <class T>
119 struct is_span_oracle : mozilla::FalseType {};
120 
121 template <class ElementType, size_t Extent>
122 struct is_span_oracle<mozilla::Span<ElementType, Extent>> : mozilla::TrueType {
123 };
124 
125 template <class T>
126 struct is_span : public is_span_oracle<remove_cv_t<T>> {};
127 
128 template <class T>
129 struct is_std_array_oracle : mozilla::FalseType {};
130 
131 template <class ElementType, size_t Extent>
132 struct is_std_array_oracle<std::array<ElementType, Extent>>
133     : mozilla::TrueType {};
134 
135 template <class T>
136 struct is_std_array : public is_std_array_oracle<remove_cv_t<T>> {};
137 
138 template <size_t From, size_t To>
139 struct is_allowed_extent_conversion
140     : public mozilla::IntegralConstant<
141           bool, From == To || From == mozilla::dynamic_extent ||
142                     To == mozilla::dynamic_extent> {};
143 
144 template <class From, class To>
145 struct is_allowed_element_type_conversion
146     : public mozilla::IntegralConstant<
147           bool, mozilla::IsConvertible<From (*)[], To (*)[]>::value> {};
148 
149 template <class Span, bool IsConst>
150 class span_iterator {
151   using element_type_ = typename Span::element_type;
152 
153  public:
154   using iterator_category = std::random_access_iterator_tag;
155   using value_type = remove_const_t<element_type_>;
156   using difference_type = typename Span::index_type;
157 
158   using reference = conditional_t<IsConst, const element_type_, element_type_>&;
159   using pointer = add_pointer_t<reference>;
160 
161   constexpr span_iterator() : span_iterator(nullptr, 0) {}
162 
163   MOZ_SPAN_ASSERTION_CONSTEXPR span_iterator(const Span* span,
164                                              typename Span::index_type index)
165       : span_(span), index_(index) {
166     MOZ_RELEASE_ASSERT(span == nullptr ||
167                        (index_ >= 0 && index <= span_->Length()));
168   }
169 
170   friend class span_iterator<Span, true>;
171   constexpr MOZ_IMPLICIT span_iterator(const span_iterator<Span, false>& other)
172       : span_iterator(other.span_, other.index_) {}
173 
174   MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR span_iterator<Span, IsConst>&
175   operator=(const span_iterator<Span, IsConst>&) = default;
176 
177   MOZ_SPAN_GCC_CONSTEXPR reference operator*() const {
178     MOZ_RELEASE_ASSERT(span_);
179     return (*span_)[index_];
180   }
181 
182   constexpr pointer operator->() const {
183     MOZ_RELEASE_ASSERT(span_);
184     return &((*span_)[index_]);
185   }
186 
187   MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator++() {
188     MOZ_RELEASE_ASSERT(span_ && index_ >= 0 && index_ < span_->Length());
189     ++index_;
190     return *this;
191   }
192 
193   constexpr span_iterator operator++(int) {
194     auto ret = *this;
195     ++(*this);
196     return ret;
197   }
198 
199   MOZ_SPAN_NON_CONST_CONSTEXPR span_iterator& operator--() {
200     MOZ_RELEASE_ASSERT(span_ && index_ > 0 && index_ <= span_->Length());
201     --index_;
202     return *this;
203   }
204 
205   constexpr span_iterator operator--(int) {
206     auto ret = *this;
207     --(*this);
208     return ret;
209   }
210 
211   MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN span_iterator
212   operator+(difference_type n) const {
213     auto ret = *this;
214     return ret += n;
215   }
216 
217   MOZ_SPAN_GCC_CONSTEXPR span_iterator& operator+=(difference_type n) {
218     MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 &&
219                        (index_ + n) <= span_->Length());
220     index_ += n;
221     return *this;
222   }
223 
224   constexpr span_iterator operator-(difference_type n) const {
225     auto ret = *this;
226     return ret -= n;
227   }
228 
229   constexpr span_iterator& operator-=(difference_type n)
230 
231   {
232     return *this += -n;
233   }
234 
235   MOZ_SPAN_GCC_CONSTEXPR difference_type
236   operator-(const span_iterator& rhs) const {
237     MOZ_RELEASE_ASSERT(span_ == rhs.span_);
238     return index_ - rhs.index_;
239   }
240 
241   constexpr reference operator[](difference_type n) const {
242     return *(*this + n);
243   }
244 
245   constexpr friend bool operator==(const span_iterator& lhs,
246                                    const span_iterator& rhs) {
247     return lhs.span_ == rhs.span_ && lhs.index_ == rhs.index_;
248   }
249 
250   constexpr friend bool operator!=(const span_iterator& lhs,
251                                    const span_iterator& rhs) {
252     return !(lhs == rhs);
253   }
254 
255   MOZ_SPAN_GCC_CONSTEXPR friend bool operator<(const span_iterator& lhs,
256                                                const span_iterator& rhs) {
257     MOZ_RELEASE_ASSERT(lhs.span_ == rhs.span_);
258     return lhs.index_ < rhs.index_;
259   }
260 
261   constexpr friend bool operator<=(const span_iterator& lhs,
262                                    const span_iterator& rhs) {
263     return !(rhs < lhs);
264   }
265 
266   constexpr friend bool operator>(const span_iterator& lhs,
267                                   const span_iterator& rhs) {
268     return rhs < lhs;
269   }
270 
271   constexpr friend bool operator>=(const span_iterator& lhs,
272                                    const span_iterator& rhs) {
273     return !(rhs > lhs);
274   }
275 
276   void swap(span_iterator& rhs) {
277     std::swap(index_, rhs.index_);
278     std::swap(span_, rhs.span_);
279   }
280 
281  protected:
282   const Span* span_;
283   size_t index_;
284 };
285 
286 template <class Span, bool IsConst>
287 inline constexpr span_iterator<Span, IsConst> operator+(
288     typename span_iterator<Span, IsConst>::difference_type n,
289     const span_iterator<Span, IsConst>& rhs) {
290   return rhs + n;
291 }
292 
293 template <size_t Ext>
294 class extent_type {
295  public:
296   using index_type = size_t;
297 
298   static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size.");
299 
300   constexpr extent_type() {}
301 
302   template <index_type Other>
303   MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT
304   extent_type(extent_type<Other> ext) {
305     static_assert(
306         Other == Ext || Other == dynamic_extent,
307         "Mismatch between fixed-size extent and size of initializing data.");
308     MOZ_RELEASE_ASSERT(ext.size() == Ext);
309   }
310 
311   MOZ_SPAN_ASSERTION_CONSTEXPR MOZ_IMPLICIT extent_type(index_type length) {
312     MOZ_RELEASE_ASSERT(length == Ext);
313   }
314 
315   constexpr index_type size() const { return Ext; }
316 };
317 
318 template <>
319 class extent_type<dynamic_extent> {
320  public:
321   using index_type = size_t;
322 
323   template <index_type Other>
324   explicit constexpr extent_type(extent_type<Other> ext) : size_(ext.size()) {}
325 
326   explicit constexpr extent_type(index_type length) : size_(length) {}
327 
328   constexpr index_type size() const { return size_; }
329 
330  private:
331   index_type size_;
332 };
333 }  // namespace span_details
334 
335 /**
336  * Span - slices for C++
337  *
338  * Span implements Rust's slice concept for C++. It's called "Span" instead of
339  * "Slice" to follow the naming used in C++ Core Guidelines.
340  *
341  * A Span wraps a pointer and a length that identify a non-owning view to a
342  * contiguous block of memory of objects of the same type. Various types,
343  * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array,
344  * mozilla::Range and contiguous standard-library containers, auto-convert
345  * into Spans when attempting to pass them as arguments to methods that take
346  * Spans. MakeSpan() functions can be used for explicit conversion in other
347  * contexts. (Span itself autoconverts into mozilla::Range.)
348  *
349  * Like Rust's slices, Span provides safety against out-of-bounds access by
350  * performing run-time bound checks. However, unlike Rust's slices, Span
351  * cannot provide safety against use-after-free.
352  *
353  * (Note: Span is like Rust's slice only conceptually. Due to the lack of
354  * ABI guarantees, you should still decompose spans/slices to raw pointer
355  * and length parts when crossing the FFI. The Elements() and data() methods
356  * are guaranteed to return a non-null pointer even for zero-length spans,
357  * so the pointer can be used as a raw part of a Rust slice without further
358  * checks.)
359  *
360  * In addition to having constructors and MakeSpan() functions that take
361  * various well-known types, a Span for an arbitrary type can be constructed
362  * (via constructor or MakeSpan()) from a pointer and a length or a pointer
363  * and another pointer pointing just past the last element.
364  *
365  * A Span<const char> or Span<const char16_t> can be obtained for const char*
366  * or const char16_t pointing to a zero-terminated string using the
367  * MakeStringSpan() function. Corresponding implicit constructor does not exist
368  * in order to avoid accidental construction in cases where const char* or
369  * const char16_t* do not point to a zero-terminated string.
370  *
371  * Span has methods that follow the Mozilla naming style and methods that
372  * don't. The methods that follow the Mozilla naming style are meant to be
373  * used directly from Mozilla code. The methods that don't are meant for
374  * integration with C++11 range-based loops and with meta-programming that
375  * expects the same methods that are found on the standard-library
376  * containers. For example, to decompose a Span into its parts in Mozilla
377  * code, use Elements() and Length() (as with nsTArray) instead of data()
378  * and size() (as with std::vector).
379  *
380  * The pointer and length wrapped by a Span cannot be changed after a Span has
381  * been created. When new values are required, simply create a new Span. Span
382  * has a method called Subspan() that works analogously to the Substring()
383  * method of XPCOM strings taking a start index and an optional length. As a
384  * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is
385  * based on), Span has methods From(start), To(end) and FromTo(start, end)
386  * that correspond to Rust's &slice[start..], &slice[..end] and
387  * &slice[start..end], respectively. (That is, the end index is the index of
388  * the first element not to be included in the new subspan.)
389  *
390  * When indicating a Span that's only read from, const goes inside the type
391  * parameter. Don't put const in front of Span. That is:
392  * size_t ReadsFromOneSpanAndWritesToAnother(Span<const uint8_t> aReadFrom,
393  *                                           Span<uint8_t> aWrittenTo);
394  *
395  * Any Span<const T> can be viewed as Span<const uint8_t> using the function
396  * AsBytes(). Any Span<T> can be viewed as Span<uint8_t> using the function
397  * AsWritableBytes().
398  */
399 template <class ElementType, size_t Extent>
400 class Span {
401  public:
402   // constants and types
403   using element_type = ElementType;
404   using index_type = size_t;
405   using pointer = element_type*;
406   using reference = element_type&;
407 
408   using iterator =
409       span_details::span_iterator<Span<ElementType, Extent>, false>;
410   using const_iterator =
411       span_details::span_iterator<Span<ElementType, Extent>, true>;
412   using reverse_iterator = std::reverse_iterator<iterator>;
413   using const_reverse_iterator = std::reverse_iterator<const_iterator>;
414 
415   constexpr static const index_type extent = Extent;
416 
417   // [Span.cons], Span constructors, copy, assignment, and destructor
418   // "Dependent" is needed to make "span_details::enable_if_t<(Dependent ||
419   //   Extent == 0 || Extent == mozilla::MaxValue<size_t>::value)>" SFINAE,
420   // since "span_details::enable_if_t<(Extent == 0 || Extent ==
421   //   mozilla::MaxValue<size_t>::value)>" is ill-formed when Extent is neither
422   //   of the extreme values.
423   /**
424    * Constructor with no args.
425    */
426   template <bool Dependent = false,
427             class = span_details::enable_if_t<
428                 (Dependent || Extent == 0 ||
429                  Extent == mozilla::MaxValue<size_t>::value)>>
430   constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {}
431 
432   /**
433    * Constructor for nullptr.
434    */
435   constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {}
436 
437   /**
438    * Constructor for pointer and length.
439    */
440   constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {}
441 
442   /**
443    * Constructor for start pointer and pointer past end.
444    */
445   constexpr Span(pointer aStartPtr, pointer aEndPtr)
446       : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {}
447 
448   /**
449    * Constructor for C array.
450    */
451   template <size_t N>
452   constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N])
453       : storage_(&aArr[0], span_details::extent_type<N>()) {}
454 
455   // Implicit constructors for char* and char16_t* pointers are deleted in order
456   // to avoid accidental construction in cases where a pointer does not point to
457   // a zero-terminated string. A Span<const char> or Span<const char16_t> can be
458   // obtained for const char* or const char16_t pointing to a zero-terminated
459   // string using the MakeStringSpan() function.
460   Span(char* aStr) = delete;
461   Span(const char* aStr) = delete;
462   Span(char16_t* aStr) = delete;
463   Span(const char16_t* aStr) = delete;
464 
465   /**
466    * Constructor for std::array.
467    */
468   template <size_t N,
469             class ArrayElementType = span_details::remove_const_t<element_type>>
470   constexpr MOZ_IMPLICIT Span(std::array<ArrayElementType, N>& aArr)
471       : storage_(&aArr[0], span_details::extent_type<N>()) {}
472 
473   /**
474    * Constructor for const std::array.
475    */
476   template <size_t N>
477   constexpr MOZ_IMPLICIT Span(
478       const std::array<span_details::remove_const_t<element_type>, N>& aArr)
479       : storage_(&aArr[0], span_details::extent_type<N>()) {}
480 
481   /**
482    * Constructor for mozilla::Array.
483    */
484   template <size_t N,
485             class ArrayElementType = span_details::remove_const_t<element_type>>
486   constexpr MOZ_IMPLICIT Span(mozilla::Array<ArrayElementType, N>& aArr)
487       : storage_(&aArr[0], span_details::extent_type<N>()) {}
488 
489   /**
490    * Constructor for const mozilla::Array.
491    */
492   template <size_t N>
493   constexpr MOZ_IMPLICIT Span(
494       const mozilla::Array<span_details::remove_const_t<element_type>, N>& aArr)
495       : storage_(&aArr[0], span_details::extent_type<N>()) {}
496 
497   /**
498    * Constructor for mozilla::UniquePtr holding an array and length.
499    */
500   template <class ArrayElementType = std::add_pointer<element_type>>
501   constexpr Span(const mozilla::UniquePtr<ArrayElementType>& aPtr,
502                  index_type aLength)
503       : storage_(aPtr.get(), aLength) {}
504 
505   // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the
506   // requirement on Container to be a contiguous sequence container.
507   /**
508    * Constructor for standard-library containers.
509    */
510   template <
511       class Container,
512       class = span_details::enable_if_t<
513           !span_details::is_span<Container>::value &&
514           !span_details::is_std_array<Container>::value &&
515           mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
516           mozilla::IsConvertible<
517               typename Container::pointer,
518               decltype(mozilla::DeclVal<Container>().data())>::value>>
519   constexpr MOZ_IMPLICIT Span(Container& cont)
520       : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
521 
522   /**
523    * Constructor for standard-library containers (const version).
524    */
525   template <
526       class Container,
527       class = span_details::enable_if_t<
528           mozilla::IsConst<element_type>::value &&
529           !span_details::is_span<Container>::value &&
530           mozilla::IsConvertible<typename Container::pointer, pointer>::value &&
531           mozilla::IsConvertible<
532               typename Container::pointer,
533               decltype(mozilla::DeclVal<Container>().data())>::value>>
534   constexpr MOZ_IMPLICIT Span(const Container& cont)
535       : Span(cont.data(), ReleaseAssertedCast<index_type>(cont.size())) {}
536 
537   /**
538    * Constructor from other Span.
539    */
540   constexpr Span(const Span& other) = default;
541 
542   /**
543    * Constructor from other Span.
544    */
545   constexpr Span(Span&& other) = default;
546 
547   /**
548    * Constructor from other Span with conversion of element type.
549    */
550   template <class OtherElementType, size_t OtherExtent,
551             class = span_details::enable_if_t<
552                 span_details::is_allowed_extent_conversion<OtherExtent,
553                                                            Extent>::value &&
554                 span_details::is_allowed_element_type_conversion<
555                     OtherElementType, element_type>::value>>
556   constexpr MOZ_IMPLICIT Span(const Span<OtherElementType, OtherExtent>& other)
557       : storage_(other.data(),
558                  span_details::extent_type<OtherExtent>(other.size())) {}
559 
560   /**
561    * Constructor from other Span with conversion of element type.
562    */
563   template <class OtherElementType, size_t OtherExtent,
564             class = span_details::enable_if_t<
565                 span_details::is_allowed_extent_conversion<OtherExtent,
566                                                            Extent>::value &&
567                 span_details::is_allowed_element_type_conversion<
568                     OtherElementType, element_type>::value>>
569   constexpr MOZ_IMPLICIT Span(Span<OtherElementType, OtherExtent>&& other)
570       : storage_(other.data(),
571                  span_details::extent_type<OtherExtent>(other.size())) {}
572 
573   ~Span() = default;
574   MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(const Span& other) =
575       default;
576 
577   MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR Span& operator=(Span&& other) =
578       default;
579 
580   // [Span.sub], Span subviews
581   /**
582    * Subspan with first N elements with compile-time N.
583    */
584   template <size_t Count>
585   constexpr Span<element_type, Count> First() const {
586     MOZ_RELEASE_ASSERT(Count <= size());
587     return {data(), Count};
588   }
589 
590   /**
591    * Subspan with last N elements with compile-time N.
592    */
593   template <size_t Count>
594   constexpr Span<element_type, Count> Last() const {
595     const size_t len = size();
596     MOZ_RELEASE_ASSERT(Count <= len);
597     return {data() + (len - Count), Count};
598   }
599 
600   /**
601    * Subspan with compile-time start index and length.
602    */
603   template <size_t Offset, size_t Count = dynamic_extent>
604   constexpr Span<element_type, Count> Subspan() const {
605     const size_t len = size();
606     MOZ_RELEASE_ASSERT(Offset <= len &&
607                        (Count == dynamic_extent || (Offset + Count <= len)));
608     return {data() + Offset, Count == dynamic_extent ? len - Offset : Count};
609   }
610 
611   /**
612    * Subspan with first N elements with run-time N.
613    */
614   constexpr Span<element_type, dynamic_extent> First(index_type aCount) const {
615     MOZ_RELEASE_ASSERT(aCount <= size());
616     return {data(), aCount};
617   }
618 
619   /**
620    * Subspan with last N elements with run-time N.
621    */
622   constexpr Span<element_type, dynamic_extent> Last(index_type aCount) const {
623     const size_t len = size();
624     MOZ_RELEASE_ASSERT(aCount <= len);
625     return {data() + (len - aCount), aCount};
626   }
627 
628   /**
629    * Subspan with run-time start index and length.
630    */
631   constexpr Span<element_type, dynamic_extent> Subspan(
632       index_type aStart, index_type aLength = dynamic_extent) const {
633     const size_t len = size();
634     MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent ||
635                                          (aStart + aLength <= len)));
636     return {data() + aStart,
637             aLength == dynamic_extent ? len - aStart : aLength};
638   }
639 
640   /**
641    * Subspan with run-time start index. (Rust's &foo[start..])
642    */
643   constexpr Span<element_type, dynamic_extent> From(index_type aStart) const {
644     return Subspan(aStart);
645   }
646 
647   /**
648    * Subspan with run-time exclusive end index. (Rust's &foo[..end])
649    */
650   constexpr Span<element_type, dynamic_extent> To(index_type aEnd) const {
651     return Subspan(0, aEnd);
652   }
653 
654   /**
655    * Subspan with run-time start index and exclusive end index.
656    * (Rust's &foo[start..end])
657    */
658   constexpr Span<element_type, dynamic_extent> FromTo(index_type aStart,
659                                                       index_type aEnd) const {
660     MOZ_RELEASE_ASSERT(aStart <= aEnd);
661     return Subspan(aStart, aEnd - aStart);
662   }
663 
664   // [Span.obs], Span observers
665   /**
666    * Number of elements in the span.
667    */
668   constexpr index_type Length() const { return size(); }
669 
670   /**
671    * Number of elements in the span (standard-libray duck typing version).
672    */
673   constexpr index_type size() const { return storage_.size(); }
674 
675   /**
676    * Size of the span in bytes.
677    */
678   constexpr index_type LengthBytes() const { return size_bytes(); }
679 
680   /**
681    * Size of the span in bytes (standard-library naming style version).
682    */
683   constexpr index_type size_bytes() const {
684     return size() * narrow_cast<index_type>(sizeof(element_type));
685   }
686 
687   /**
688    * Checks if the the length of the span is zero.
689    */
690   constexpr bool IsEmpty() const { return empty(); }
691 
692   /**
693    * Checks if the the length of the span is zero (standard-libray duck
694    * typing version).
695    */
696   constexpr bool empty() const { return size() == 0; }
697 
698   // [Span.elem], Span element access
699   constexpr reference operator[](index_type idx) const {
700     MOZ_RELEASE_ASSERT(idx < storage_.size());
701     return data()[idx];
702   }
703 
704   /**
705    * Access element of span by index (standard-library duck typing version).
706    */
707   constexpr reference at(index_type idx) const { return this->operator[](idx); }
708 
709   constexpr reference operator()(index_type idx) const {
710     return this->operator[](idx);
711   }
712 
713   /**
714    * Pointer to the first element of the span. The return value is never
715    * nullptr, not ever for zero-length spans, so it can be passed as-is
716    * to std::slice::from_raw_parts() in Rust.
717    */
718   constexpr pointer Elements() const { return data(); }
719 
720   /**
721    * Pointer to the first element of the span (standard-libray duck typing
722    * version). The return value is never nullptr, not ever for zero-length
723    * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust.
724    */
725   constexpr pointer data() const { return storage_.data(); }
726 
727   // [Span.iter], Span iterator support
728   iterator begin() const { return {this, 0}; }
729   iterator end() const { return {this, Length()}; }
730 
731   const_iterator cbegin() const { return {this, 0}; }
732   const_iterator cend() const { return {this, Length()}; }
733 
734   reverse_iterator rbegin() const { return reverse_iterator{end()}; }
735   reverse_iterator rend() const { return reverse_iterator{begin()}; }
736 
737   const_reverse_iterator crbegin() const {
738     return const_reverse_iterator{cend()};
739   }
740   const_reverse_iterator crend() const {
741     return const_reverse_iterator{cbegin()};
742   }
743 
744  private:
745   // this implementation detail class lets us take advantage of the
746   // empty base class optimization to pay for only storage of a single
747   // pointer in the case of fixed-size Spans
748   template <class ExtentType>
749   class storage_type : public ExtentType {
750    public:
751     template <class OtherExtentType>
752     MOZ_SPAN_ASSERTION_CONSTEXPR storage_type(pointer elements,
753                                               OtherExtentType ext)
754         : ExtentType(ext)
755           // Replace nullptr with 0x1 for Rust slice compatibility. See
756           // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html
757           ,
758           data_(elements ? elements : reinterpret_cast<pointer>(0x1)) {
759       const size_t extentSize = ExtentType::size();
760       MOZ_RELEASE_ASSERT(
761           (!elements && extentSize == 0) ||
762           (elements && extentSize != mozilla::MaxValue<size_t>::value));
763     }
764 
765     constexpr pointer data() const { return data_; }
766 
767    private:
768     pointer data_;
769   };
770 
771   storage_type<span_details::extent_type<Extent>> storage_;
772 };
773 
774 // [Span.comparison], Span comparison operators
775 template <class ElementType, size_t FirstExtent, size_t SecondExtent>
776 inline constexpr bool operator==(const Span<ElementType, FirstExtent>& l,
777                                  const Span<ElementType, SecondExtent>& r) {
778   return (l.size() == r.size()) && std::equal(l.begin(), l.end(), r.begin());
779 }
780 
781 template <class ElementType, size_t Extent>
782 inline constexpr bool operator!=(const Span<ElementType, Extent>& l,
783                                  const Span<ElementType, Extent>& r) {
784   return !(l == r);
785 }
786 
787 template <class ElementType, size_t Extent>
788 inline constexpr bool operator<(const Span<ElementType, Extent>& l,
789                                 const Span<ElementType, Extent>& r) {
790   return std::lexicographical_compare(l.begin(), l.end(), r.begin(), r.end());
791 }
792 
793 template <class ElementType, size_t Extent>
794 inline constexpr bool operator<=(const Span<ElementType, Extent>& l,
795                                  const Span<ElementType, Extent>& r) {
796   return !(l > r);
797 }
798 
799 template <class ElementType, size_t Extent>
800 inline constexpr bool operator>(const Span<ElementType, Extent>& l,
801                                 const Span<ElementType, Extent>& r) {
802   return r < l;
803 }
804 
805 template <class ElementType, size_t Extent>
806 inline constexpr bool operator>=(const Span<ElementType, Extent>& l,
807                                  const Span<ElementType, Extent>& r) {
808   return !(l < r);
809 }
810 
811 namespace span_details {
812 // if we only supported compilers with good constexpr support then
813 // this pair of classes could collapse down to a constexpr function
814 
815 // we should use a narrow_cast<> to go to size_t, but older compilers may not
816 // see it as constexpr and so will fail compilation of the template
817 template <class ElementType, size_t Extent>
818 struct calculate_byte_size
819     : mozilla::IntegralConstant<size_t, static_cast<size_t>(
820                                             sizeof(ElementType) *
821                                             static_cast<size_t>(Extent))> {};
822 
823 template <class ElementType>
824 struct calculate_byte_size<ElementType, dynamic_extent>
825     : mozilla::IntegralConstant<size_t, dynamic_extent> {};
826 }  // namespace span_details
827 
828 // [Span.objectrep], views of object representation
829 /**
830  * View span as Span<const uint8_t>.
831  */
832 template <class ElementType, size_t Extent>
833 Span<const uint8_t,
834      span_details::calculate_byte_size<ElementType, Extent>::value>
835 AsBytes(Span<ElementType, Extent> s) {
836   return {reinterpret_cast<const uint8_t*>(s.data()), s.size_bytes()};
837 }
838 
839 /**
840  * View span as Span<uint8_t>.
841  */
842 template <
843     class ElementType, size_t Extent,
844     class = span_details::enable_if_t<!mozilla::IsConst<ElementType>::value>>
845 Span<uint8_t, span_details::calculate_byte_size<ElementType, Extent>::value>
846 AsWritableBytes(Span<ElementType, Extent> s) {
847   return {reinterpret_cast<uint8_t*>(s.data()), s.size_bytes()};
848 }
849 
850 //
851 // MakeSpan() - Utility functions for creating Spans
852 //
853 /**
854  * Create span from pointer and length.
855  */
856 template <class ElementType>
857 Span<ElementType> MakeSpan(ElementType* aPtr,
858                            typename Span<ElementType>::index_type aLength) {
859   return Span<ElementType>(aPtr, aLength);
860 }
861 
862 /**
863  * Create span from start pointer and pointer past end.
864  */
865 template <class ElementType>
866 Span<ElementType> MakeSpan(ElementType* aStartPtr, ElementType* aEndPtr) {
867   return Span<ElementType>(aStartPtr, aEndPtr);
868 }
869 
870 /**
871  * Create span from C array.
872  * MakeSpan() does not permit creating Span objects from string literals (const
873  * char or char16_t arrays) because the Span length would include the zero
874  * terminator, which may surprise callers. Use MakeStringSpan() to create a
875  * Span whose length that excludes the string literal's zero terminator or use
876  * the MakeSpan() overload that accepts a pointer and length and specify the
877  * string literal's full length.
878  */
879 template <class ElementType, size_t N,
880           class = span_details::enable_if_t<
881               !IsSame<ElementType, const char>::value &&
882               !IsSame<ElementType, const char16_t>::value>>
883 Span<ElementType> MakeSpan(ElementType (&aArr)[N]) {
884   return Span<ElementType>(aArr, N);
885 }
886 
887 /**
888  * Create span from mozilla::Array.
889  */
890 template <class ElementType, size_t N>
891 Span<ElementType> MakeSpan(mozilla::Array<ElementType, N>& aArr) {
892   return aArr;
893 }
894 
895 /**
896  * Create span from const mozilla::Array.
897  */
898 template <class ElementType, size_t N>
899 Span<const ElementType> MakeSpan(const mozilla::Array<ElementType, N>& arr) {
900   return arr;
901 }
902 
903 /**
904  * Create span from standard-library container.
905  */
906 template <class Container>
907 Span<typename Container::value_type> MakeSpan(Container& cont) {
908   return Span<typename Container::value_type>(cont);
909 }
910 
911 /**
912  * Create span from standard-library container (const version).
913  */
914 template <class Container>
915 Span<const typename Container::value_type> MakeSpan(const Container& cont) {
916   return Span<const typename Container::value_type>(cont);
917 }
918 
919 /**
920  * Create span from smart pointer and length.
921  */
922 template <class Ptr>
923 Span<typename Ptr::element_type> MakeSpan(Ptr& aPtr, size_t aLength) {
924   return Span<typename Ptr::element_type>(aPtr, aLength);
925 }
926 
927 /**
928  * Create span from C string.
929  */
930 inline Span<const char> MakeStringSpan(const char* aZeroTerminated) {
931   return Span<const char>(aZeroTerminated, std::strlen(aZeroTerminated));
932 }
933 
934 /**
935  * Create span from UTF-16 C string.
936  */
937 inline Span<const char16_t> MakeStringSpan(const char16_t* aZeroTerminated) {
938   return Span<const char16_t>(aZeroTerminated,
939                               span_details::strlen16(aZeroTerminated));
940 }
941 
942 }  // namespace mozilla
943 
944 #ifdef _MSC_VER
945 #if _MSC_VER < 1910
946 #undef constexpr
947 #pragma pop_macro("constexpr")
948 
949 #endif  // _MSC_VER < 1910
950 
951 #pragma warning(pop)
952 #endif  // _MSC_VER
953 
954 #undef MOZ_SPAN_ASSERTION_CONSTEXPR
955 #undef MOZ_SPAN_GCC_CONSTEXPR
956 #undef MOZ_SPAN_EXPLICITLY_DEFAULTED_CONSTEXPR
957 #undef MOZ_SPAN_CONSTEXPR_NOT_JUST_RETURN
958 #undef MOZ_SPAN_NON_CONST_CONSTEXPR
959 
960 #endif  // mozilla_Span_h
961