/////////////////////////////////////////////////////////////////////////////// // // Copyright (c) 2015 Microsoft Corporation. All rights reserved. // // This code is licensed under the MIT License (MIT). // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. // /////////////////////////////////////////////////////////////////////////////// // Adapted from // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/span // and // https://github.com/Microsoft/GSL/blob/3819df6e378ffccf0e29465afe99c3b324c2aa70/include/gsl/gsl_util #ifndef mozilla_Span_h #define mozilla_Span_h #include #include #include #include #include #include #include #include #include "mozilla/Assertions.h" #include "mozilla/Attributes.h" #include "mozilla/Casting.h" #include "mozilla/UniquePtr.h" namespace mozilla { template class Array; // Stuff from gsl_util // narrow_cast(): a searchable way to do narrowing casts of values template inline constexpr T narrow_cast(U&& u) { return static_cast(std::forward(u)); } // end gsl_util // [views.constants], constants // This was -1 in gsl::span, but using size_t for sizes instead of ptrdiff_t // and reserving a magic value that realistically doesn't occur in // compile-time-constant Span sizes makes things a lot less messy in terms of // comparison between signed and unsigned. constexpr const size_t dynamic_extent = std::numeric_limits::max(); template class Span; // implementation details namespace span_details { template struct is_span_oracle : std::false_type {}; template struct is_span_oracle> : std::true_type {}; template struct is_span : public is_span_oracle> {}; template struct is_std_array_oracle : std::false_type {}; template struct is_std_array_oracle> : std::true_type {}; template struct is_std_array : public is_std_array_oracle> {}; template struct is_allowed_extent_conversion : public std::integral_constant {}; template struct is_allowed_element_type_conversion : public std::integral_constant< bool, std::is_convertible_v> {}; struct SpanKnownBounds {}; template class span_iterator { using element_type_ = typename SpanT::element_type; template friend class ::mozilla::Span; public: using iterator_category = std::random_access_iterator_tag; using value_type = std::remove_const_t; using difference_type = ptrdiff_t; using reference = std::conditional_t&; using pointer = std::add_pointer_t; constexpr span_iterator() : span_iterator(nullptr, 0, SpanKnownBounds{}) {} constexpr span_iterator(const SpanT* span, typename SpanT::index_type index) : span_(span), index_(index) { MOZ_RELEASE_ASSERT(span == nullptr || (index_ >= 0 && index <= span_->Length())); } private: // For whatever reason, the compiler doesn't like optimizing away the above // MOZ_RELEASE_ASSERT when `span_iterator` is constructed for // obviously-correct cases like `span.begin()` or `span.end()`. We provide // this private constructor for such cases. constexpr span_iterator(const SpanT* span, typename SpanT::index_type index, SpanKnownBounds) : span_(span), index_(index) {} public: // `other` is already correct by construction; we do not need to go through // the release assert above. Put differently, this constructor is effectively // a copy constructor and therefore needs no assertions. friend class span_iterator; constexpr MOZ_IMPLICIT span_iterator(const span_iterator& other) : span_(other.span_), index_(other.index_) {} constexpr span_iterator& operator=( const span_iterator&) = default; constexpr reference operator*() const { MOZ_RELEASE_ASSERT(span_); return (*span_)[index_]; } constexpr pointer operator->() const { MOZ_RELEASE_ASSERT(span_); return &((*span_)[index_]); } constexpr span_iterator& operator++() { ++index_; return *this; } constexpr span_iterator operator++(int) { auto ret = *this; ++(*this); return ret; } constexpr span_iterator& operator--() { --index_; return *this; } constexpr span_iterator operator--(int) { auto ret = *this; --(*this); return ret; } constexpr span_iterator operator+(difference_type n) const { auto ret = *this; return ret += n; } constexpr span_iterator& operator+=(difference_type n) { MOZ_RELEASE_ASSERT(span_ && (index_ + n) >= 0 && (index_ + n) <= span_->Length()); index_ += n; return *this; } constexpr span_iterator operator-(difference_type n) const { auto ret = *this; return ret -= n; } constexpr span_iterator& operator-=(difference_type n) { return *this += -n; } constexpr difference_type operator-(const span_iterator& rhs) const { MOZ_RELEASE_ASSERT(span_ == rhs.span_); return index_ - rhs.index_; } constexpr reference operator[](difference_type n) const { return *(*this + n); } constexpr friend bool operator==(const span_iterator& lhs, const span_iterator& rhs) { // Iterators from different spans are uncomparable. A diagnostic assertion // should be enough to check this, though. To ensure that no iterators from // different spans are ever considered equal, still compare them in release // builds. MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_); return lhs.index_ == rhs.index_ && lhs.span_ == rhs.span_; } constexpr friend bool operator!=(const span_iterator& lhs, const span_iterator& rhs) { return !(lhs == rhs); } constexpr friend bool operator<(const span_iterator& lhs, const span_iterator& rhs) { MOZ_DIAGNOSTIC_ASSERT(lhs.span_ == rhs.span_); return lhs.index_ < rhs.index_; } constexpr friend bool operator<=(const span_iterator& lhs, const span_iterator& rhs) { return !(rhs < lhs); } constexpr friend bool operator>(const span_iterator& lhs, const span_iterator& rhs) { return rhs < lhs; } constexpr friend bool operator>=(const span_iterator& lhs, const span_iterator& rhs) { return !(rhs > lhs); } void swap(span_iterator& rhs) { std::swap(index_, rhs.index_); std::swap(span_, rhs.span_); } protected: const SpanT* span_; size_t index_; }; template inline constexpr span_iterator operator+( typename span_iterator::difference_type n, const span_iterator& rhs) { return rhs + n; } template class extent_type { public: using index_type = size_t; static_assert(Ext >= 0, "A fixed-size Span must be >= 0 in size."); constexpr extent_type() = default; template constexpr MOZ_IMPLICIT extent_type(extent_type ext) { static_assert( Other == Ext || Other == dynamic_extent, "Mismatch between fixed-size extent and size of initializing data."); MOZ_RELEASE_ASSERT(ext.size() == Ext); } constexpr MOZ_IMPLICIT extent_type(index_type length) { MOZ_RELEASE_ASSERT(length == Ext); } constexpr index_type size() const { return Ext; } }; template <> class extent_type { public: using index_type = size_t; template explicit constexpr extent_type(extent_type ext) : size_(ext.size()) {} explicit constexpr extent_type(index_type length) : size_(length) {} constexpr index_type size() const { return size_; } private: index_type size_; }; } // namespace span_details /** * Span - slices for C++ * * Span implements Rust's slice concept for C++. It's called "Span" instead of * "Slice" to follow the naming used in C++ Core Guidelines. * * A Span wraps a pointer and a length that identify a non-owning view to a * contiguous block of memory of objects of the same type. Various types, * including (pre-decay) C arrays, XPCOM strings, nsTArray, mozilla::Array, * mozilla::Range and contiguous standard-library containers, auto-convert * into Spans when attempting to pass them as arguments to methods that take * Spans. (Span itself autoconverts into mozilla::Range.) * * Like Rust's slices, Span provides safety against out-of-bounds access by * performing run-time bound checks. However, unlike Rust's slices, Span * cannot provide safety against use-after-free. * * (Note: Span is like Rust's slice only conceptually. Due to the lack of * ABI guarantees, you should still decompose spans/slices to raw pointer * and length parts when crossing the FFI. The Elements() and data() methods * are guaranteed to return a non-null pointer even for zero-length spans, * so the pointer can be used as a raw part of a Rust slice without further * checks.) * * In addition to having constructors (with the support of deduction guides) * that take various well-known types, a Span for an arbitrary type can be * constructed from a pointer and a length or a pointer and another pointer * pointing just past the last element. * * A Span or Span can be obtained for const char* * or const char16_t pointing to a zero-terminated string using the * MakeStringSpan() function (which treats a nullptr argument equivalently * to the empty string). Corresponding implicit constructor does not exist * in order to avoid accidental construction in cases where const char* or * const char16_t* do not point to a zero-terminated string. * * Span has methods that follow the Mozilla naming style and methods that * don't. The methods that follow the Mozilla naming style are meant to be * used directly from Mozilla code. The methods that don't are meant for * integration with C++11 range-based loops and with meta-programming that * expects the same methods that are found on the standard-library * containers. For example, to decompose a Span into its parts in Mozilla * code, use Elements() and Length() (as with nsTArray) instead of data() * and size() (as with std::vector). * * The pointer and length wrapped by a Span cannot be changed after a Span has * been created. When new values are required, simply create a new Span. Span * has a method called Subspan() that works analogously to the Substring() * method of XPCOM strings taking a start index and an optional length. As a * Mozilla extension (relative to Microsoft's gsl::span that mozilla::Span is * based on), Span has methods From(start), To(end) and FromTo(start, end) * that correspond to Rust's &slice[start..], &slice[..end] and * &slice[start..end], respectively. (That is, the end index is the index of * the first element not to be included in the new subspan.) * * When indicating a Span that's only read from, const goes inside the type * parameter. Don't put const in front of Span. That is: * size_t ReadsFromOneSpanAndWritesToAnother(Span aReadFrom, * Span aWrittenTo); * * Any Span can be viewed as Span using the function * AsBytes(). Any Span can be viewed as Span using the function * AsWritableBytes(). * * Note that iterators from different Span instances are uncomparable, even if * they refer to the same memory. This also applies to any spans derived via * Subspan etc. */ template class Span { public: // constants and types using element_type = ElementType; using value_type = std::remove_cv_t; using index_type = size_t; using pointer = element_type*; using reference = element_type&; using iterator = span_details::span_iterator, false>; using const_iterator = span_details::span_iterator, true>; using reverse_iterator = std::reverse_iterator; using const_reverse_iterator = std::reverse_iterator; constexpr static const index_type extent = Extent; // [Span.cons], Span constructors, copy, assignment, and destructor // "Dependent" is needed to make "std::enable_if_t<(Dependent || // Extent == 0 || Extent == dynamic_extent)>" SFINAE, // since // "std::enable_if_t<(Extent == 0 || Extent == dynamic_extent)>" is // ill-formed when Extent is neither of the extreme values. /** * Constructor with no args. */ template > constexpr Span() : storage_(nullptr, span_details::extent_type<0>()) {} /** * Constructor for nullptr. */ constexpr MOZ_IMPLICIT Span(std::nullptr_t) : Span() {} /** * Constructor for pointer and length. */ constexpr Span(pointer aPtr, index_type aLength) : storage_(aPtr, aLength) {} /** * Constructor for start pointer and pointer past end. */ constexpr Span(pointer aStartPtr, pointer aEndPtr) : storage_(aStartPtr, std::distance(aStartPtr, aEndPtr)) {} /** * Constructor for pair of Span iterators. */ template constexpr Span( span_details::span_iterator, IsConst> aBegin, span_details::span_iterator, IsConst> aEnd) : storage_(aBegin == aEnd ? nullptr : &*aBegin, aEnd - aBegin) {} /** * Constructor for {iterator,size_t} */ template constexpr Span( span_details::span_iterator, IsConst> aBegin, index_type aLength) : storage_(!aLength ? nullptr : &*aBegin, aLength) {} /** * Constructor for C array. */ template constexpr MOZ_IMPLICIT Span(element_type (&aArr)[N]) : storage_(&aArr[0], span_details::extent_type()) {} // Implicit constructors for char* and char16_t* pointers are deleted in order // to avoid accidental construction in cases where a pointer does not point to // a zero-terminated string. A Span or Span can be // obtained for const char* or const char16_t pointing to a zero-terminated // string using the MakeStringSpan() function. // (This must be a template because otherwise it will prevent the previous // array constructor to match because an array decays to a pointer. This only // exists to point to the above explanation, since there's no other // constructor that would match.) template < typename T, typename = std::enable_if_t< std::is_pointer_v && (std::is_same_v>, char> || std::is_same_v>, char16_t>)>> Span(T& aStr) = delete; /** * Constructor for std::array. */ template > constexpr MOZ_IMPLICIT Span(std::array& aArr) : storage_(&aArr[0], span_details::extent_type()) {} /** * Constructor for const std::array. */ template constexpr MOZ_IMPLICIT Span( const std::array, N>& aArr) : storage_(&aArr[0], span_details::extent_type()) {} /** * Constructor for mozilla::Array. */ template > constexpr MOZ_IMPLICIT Span(mozilla::Array& aArr) : storage_(&aArr[0], span_details::extent_type()) {} /** * Constructor for const mozilla::Array. */ template constexpr MOZ_IMPLICIT Span( const mozilla::Array, N>& aArr) : storage_(&aArr[0], span_details::extent_type()) {} /** * Constructor for mozilla::UniquePtr holding an array and length. */ template , class DeleterType> constexpr Span(const mozilla::UniquePtr& aPtr, index_type aLength) : storage_(aPtr.get(), aLength) {} // NB: the SFINAE here uses .data() as a incomplete/imperfect proxy for the // requirement on Container to be a contiguous sequence container. /** * Constructor for standard-library containers. */ template < class Container, class Dummy = std::enable_if_t< !std::is_const_v && !span_details::is_span::value && !span_details::is_std_array::value && std::is_convertible_v && std::is_convertible_v().data())>, Container>> constexpr MOZ_IMPLICIT Span(Container& cont, Dummy* = nullptr) : Span(cont.data(), ReleaseAssertedCast(cont.size())) {} /** * Constructor for standard-library containers (const version). */ template < class Container, class = std::enable_if_t< std::is_const_v && !span_details::is_span::value && std::is_convertible_v && std::is_convertible_v().data())>>> constexpr MOZ_IMPLICIT Span(const Container& cont) : Span(cont.data(), ReleaseAssertedCast(cont.size())) {} // NB: the SFINAE here uses .Elements() as a incomplete/imperfect proxy for // the requirement on Container to be a contiguous sequence container. /** * Constructor for contiguous Mozilla containers. */ template < class Container, class = std::enable_if_t< !std::is_const_v && !span_details::is_span::value && !span_details::is_std_array::value && std::is_convertible_v && std::is_convertible_v< typename Container::value_type*, decltype(std::declval().Elements())>>> constexpr MOZ_IMPLICIT Span(Container& cont, void* = nullptr) : Span(cont.Elements(), ReleaseAssertedCast(cont.Length())) {} /** * Constructor for contiguous Mozilla containers (const version). */ template < class Container, class = std::enable_if_t< std::is_const_v && !span_details::is_span::value && std::is_convertible_v && std::is_convertible_v< typename Container::value_type*, decltype(std::declval().Elements())>>> constexpr MOZ_IMPLICIT Span(const Container& cont, void* = nullptr) : Span(cont.Elements(), ReleaseAssertedCast(cont.Length())) {} /** * Constructor from other Span. */ constexpr Span(const Span& other) = default; /** * Constructor from other Span. */ constexpr Span(Span&& other) = default; /** * Constructor from other Span with conversion of element type. */ template < class OtherElementType, size_t OtherExtent, class = std::enable_if_t::value && span_details::is_allowed_element_type_conversion< OtherElementType, element_type>::value>> constexpr MOZ_IMPLICIT Span(const Span& other) : storage_(other.data(), span_details::extent_type(other.size())) {} /** * Constructor from other Span with conversion of element type. */ template < class OtherElementType, size_t OtherExtent, class = std::enable_if_t::value && span_details::is_allowed_element_type_conversion< OtherElementType, element_type>::value>> constexpr MOZ_IMPLICIT Span(Span&& other) : storage_(other.data(), span_details::extent_type(other.size())) {} ~Span() = default; constexpr Span& operator=(const Span& other) = default; constexpr Span& operator=(Span&& other) = default; // [Span.sub], Span subviews /** * Subspan with first N elements with compile-time N. */ template constexpr Span First() const { MOZ_RELEASE_ASSERT(Count <= size()); return {data(), Count}; } /** * Subspan with last N elements with compile-time N. */ template constexpr Span Last() const { const size_t len = size(); MOZ_RELEASE_ASSERT(Count <= len); return {data() + (len - Count), Count}; } /** * Subspan with compile-time start index and length. */ template constexpr Span Subspan() const { const size_t len = size(); MOZ_RELEASE_ASSERT(Offset <= len && (Count == dynamic_extent || (Offset + Count <= len))); return {data() + Offset, Count == dynamic_extent ? len - Offset : Count}; } /** * Subspan with first N elements with run-time N. */ constexpr Span First(index_type aCount) const { MOZ_RELEASE_ASSERT(aCount <= size()); return {data(), aCount}; } /** * Subspan with last N elements with run-time N. */ constexpr Span Last(index_type aCount) const { const size_t len = size(); MOZ_RELEASE_ASSERT(aCount <= len); return {data() + (len - aCount), aCount}; } /** * Subspan with run-time start index and length. */ constexpr Span Subspan( index_type aStart, index_type aLength = dynamic_extent) const { const size_t len = size(); MOZ_RELEASE_ASSERT(aStart <= len && (aLength == dynamic_extent || (aStart + aLength <= len))); return {data() + aStart, aLength == dynamic_extent ? len - aStart : aLength}; } /** * Subspan with run-time start index. (Rust's &foo[start..]) */ constexpr Span From(index_type aStart) const { return Subspan(aStart); } /** * Subspan with run-time exclusive end index. (Rust's &foo[..end]) */ constexpr Span To(index_type aEnd) const { return Subspan(0, aEnd); } /// std::span-compatible method name constexpr auto subspan(index_type aStart, index_type aLength = dynamic_extent) const { return Subspan(aStart, aLength); } /// std::span-compatible method name constexpr auto from(index_type aStart) const { return From(aStart); } /// std::span-compatible method name constexpr auto to(index_type aEnd) const { return To(aEnd); } /** * Subspan with run-time start index and exclusive end index. * (Rust's &foo[start..end]) */ constexpr Span FromTo(index_type aStart, index_type aEnd) const { MOZ_RELEASE_ASSERT(aStart <= aEnd); return Subspan(aStart, aEnd - aStart); } // [Span.obs], Span observers /** * Number of elements in the span. */ constexpr index_type Length() const { return size(); } /** * Number of elements in the span (standard-libray duck typing version). */ constexpr index_type size() const { return storage_.size(); } /** * Size of the span in bytes. */ constexpr index_type LengthBytes() const { return size_bytes(); } /** * Size of the span in bytes (standard-library naming style version). */ constexpr index_type size_bytes() const { return size() * narrow_cast(sizeof(element_type)); } /** * Checks if the the length of the span is zero. */ constexpr bool IsEmpty() const { return empty(); } /** * Checks if the the length of the span is zero (standard-libray duck * typing version). */ constexpr bool empty() const { return size() == 0; } // [Span.elem], Span element access constexpr reference operator[](index_type idx) const { MOZ_RELEASE_ASSERT(idx < storage_.size()); return data()[idx]; } /** * Access element of span by index (standard-library duck typing version). */ constexpr reference at(index_type idx) const { return this->operator[](idx); } constexpr reference operator()(index_type idx) const { return this->operator[](idx); } /** * Pointer to the first element of the span. The return value is never * nullptr, not ever for zero-length spans, so it can be passed as-is * to std::slice::from_raw_parts() in Rust. */ constexpr pointer Elements() const { return data(); } /** * Pointer to the first element of the span (standard-libray duck typing * version). The return value is never nullptr, not ever for zero-length * spans, so it can be passed as-is to std::slice::from_raw_parts() in Rust. */ constexpr pointer data() const { return storage_.data(); } // [Span.iter], Span iterator support iterator begin() const { return {this, 0, span_details::SpanKnownBounds{}}; } iterator end() const { return {this, Length(), span_details::SpanKnownBounds{}}; } const_iterator cbegin() const { return {this, 0, span_details::SpanKnownBounds{}}; } const_iterator cend() const { return {this, Length(), span_details::SpanKnownBounds{}}; } reverse_iterator rbegin() const { return reverse_iterator{end()}; } reverse_iterator rend() const { return reverse_iterator{begin()}; } const_reverse_iterator crbegin() const { return const_reverse_iterator{cend()}; } const_reverse_iterator crend() const { return const_reverse_iterator{cbegin()}; } template constexpr std::pair, Span> SplitAt() const { static_assert(Extent != dynamic_extent); static_assert(SplitPoint <= Extent); return {First(), Last()}; } constexpr std::pair, Span> SplitAt(const index_type aSplitPoint) const { MOZ_RELEASE_ASSERT(aSplitPoint <= Length()); return {First(aSplitPoint), Last(Length() - aSplitPoint)}; } constexpr Span, Extent> AsConst() const { return {Elements(), Length()}; } private: // this implementation detail class lets us take advantage of the // empty base class optimization to pay for only storage of a single // pointer in the case of fixed-size Spans template class storage_type : public ExtentType { public: template constexpr storage_type(pointer elements, OtherExtentType ext) : ExtentType(ext) // Replace nullptr with aligned bogus pointer for Rust slice // compatibility. See // https://doc.rust-lang.org/std/slice/fn.from_raw_parts.html , data_(elements ? elements : reinterpret_cast(alignof(element_type))) { const size_t extentSize = ExtentType::size(); MOZ_RELEASE_ASSERT((!elements && extentSize == 0) || (elements && extentSize != dynamic_extent)); } constexpr pointer data() const { return data_; } private: pointer data_; }; storage_type> storage_; }; template Span(span_details::span_iterator, IsConst> aBegin, span_details::span_iterator, IsConst> aEnd) -> Span, T>>; template Span(T (&)[Extent]) -> Span; template Span(Container&) -> Span; template Span(const Container&) -> Span; template Span(mozilla::Array&) -> Span; template Span(const mozilla::Array&) -> Span; // [Span.comparison], Span comparison operators template inline constexpr bool operator==(const Span& l, const Span& r) { return (l.size() == r.size()) && std::equal(l.data(), l.data() + l.size(), r.data()); } template inline constexpr bool operator!=(const Span& l, const Span& r) { return !(l == r); } template inline constexpr bool operator<(const Span& l, const Span& r) { return std::lexicographical_compare(l.data(), l.data() + l.size(), r.data(), r.data() + r.size()); } template inline constexpr bool operator<=(const Span& l, const Span& r) { return !(l > r); } template inline constexpr bool operator>(const Span& l, const Span& r) { return r < l; } template inline constexpr bool operator>=(const Span& l, const Span& r) { return !(l < r); } namespace span_details { // if we only supported compilers with good constexpr support then // this pair of classes could collapse down to a constexpr function // we should use a narrow_cast<> to go to size_t, but older compilers may not // see it as constexpr and so will fail compilation of the template template struct calculate_byte_size : std::integral_constant(sizeof(ElementType) * static_cast(Extent))> { }; template struct calculate_byte_size : std::integral_constant {}; } // namespace span_details // [Span.objectrep], views of object representation /** * View span as Span. */ template Span::value> AsBytes(Span s) { return {reinterpret_cast(s.data()), s.size_bytes()}; } /** * View span as Span. */ template >> Span::value> AsWritableBytes(Span s) { return {reinterpret_cast(s.data()), s.size_bytes()}; } /** * View a span of uint8_t as a span of char. */ inline Span AsChars(Span s) { return {reinterpret_cast(s.data()), s.size()}; } /** * View a writable span of uint8_t as a span of char. */ inline Span AsWritableChars(Span s) { return {reinterpret_cast(s.data()), s.size()}; } /** * Create span from a zero-terminated C string. nullptr is * treated as the empty string. */ constexpr Span MakeStringSpan(const char* aZeroTerminated) { if (!aZeroTerminated) { return Span(); } return Span(aZeroTerminated, std::char_traits::length(aZeroTerminated)); } /** * Create span from a zero-terminated UTF-16 C string. nullptr is * treated as the empty string. */ constexpr Span MakeStringSpan(const char16_t* aZeroTerminated) { if (!aZeroTerminated) { return Span(); } return Span( aZeroTerminated, std::char_traits::length(aZeroTerminated)); } } // namespace mozilla #endif // mozilla_Span_h