refactor: Remove unused Span alias

Also, fixup some wording.
This commit is contained in:
MarcoFalke 2024-12-18 17:12:13 +01:00
parent fade0b5e5e
commit fa0c6b7179
No known key found for this signature in database
6 changed files with 23 additions and 36 deletions

View File

@ -75,7 +75,7 @@ public:
*
* @param depgraph The original DepGraph that is being remapped.
*
* @param mapping A std::span such that mapping[i] gives the position in the new DepGraph
* @param mapping A span such that mapping[i] gives the position in the new DepGraph
* for position i in the old depgraph. Its size must be equal to
* depgraph.PositionRange(). The value of mapping[i] is ignored if
* position i is a hole in depgraph (i.e., if !depgraph.Positions()[i]).

View File

@ -33,7 +33,7 @@ void poly1305_finish(poly1305_context *st, unsigned char mac[16]) noexcept;
} // namespace poly1305_donna
/** C++ wrapper with std::byte std::span interface around poly1305_donna code. */
/** C++ wrapper with std::byte span interface around poly1305_donna code. */
class Poly1305
{
poly1305_donna::poly1305_context m_ctx;

View File

@ -263,7 +263,7 @@ public:
}
}
/** Fill a std::span with random bytes. */
/** Fill a span with random bytes. */
void fillrand(std::span<std::byte> span) noexcept
{
while (span.size() >= 8) {
@ -400,7 +400,7 @@ public:
return ReadLE64(buf.data());
}
/** Fill a byte std::span with random bytes. This overrides the RandomMixin version. */
/** Fill a byte span with random bytes. This overrides the RandomMixin version. */
void fillrand(std::span<std::byte> output) noexcept;
};

View File

@ -593,7 +593,7 @@ private:
* child. It can modify the state. Children of a given node will have downfn()
* called in order.
* - upfn is a callable (State&&, const Node&, std::span<Result>) -> std::optional<Result>,
* which given a node, its state, and a std::span of the results of its children,
* which given a node, its state, and a span of the results of its children,
* computes the result of the node. If std::nullopt is returned by upfn,
* TreeEvalMaybe() immediately returns std::nullopt.
* The return value of TreeEvalMaybe is the result of the root node.

View File

@ -11,38 +11,38 @@
#include <type_traits>
#include <utility>
/** A Span is an object that can refer to a contiguous sequence of objects.
/** A span is an object that can refer to a contiguous sequence of objects.
*
* Things to be aware of when writing code that deals with Spans:
* Things to be aware of when writing code that deals with spans:
*
* - Similar to references themselves, Spans are subject to reference lifetime
* - Similar to references themselves, spans are subject to reference lifetime
* issues. The user is responsible for making sure the objects pointed to by
* a Span live as long as the Span is used. For example:
* a span live as long as the span is used. For example:
*
* std::vector<int> vec{1,2,3,4};
* Span<int> sp(vec);
* std::span<int> sp(vec);
* vec.push_back(5);
* printf("%i\n", sp.front()); // UB!
*
* may exhibit undefined behavior, as increasing the size of a vector may
* invalidate references.
*
* - One particular pitfall is that Spans can be constructed from temporaries,
* but this is unsafe when the Span is stored in a variable, outliving the
* - One particular pitfall is that spans can be constructed from temporaries,
* but this is unsafe when the span is stored in a variable, outliving the
* temporary. For example, this will compile, but exhibits undefined behavior:
*
* Span<const int> sp(std::vector<int>{1, 2, 3});
* std::span<const int> sp(std::vector<int>{1, 2, 3});
* printf("%i\n", sp.front()); // UB!
*
* The lifetime of the vector ends when the statement it is created in ends.
* Thus the Span is left with a dangling reference, and using it is undefined.
* Thus the span is left with a dangling reference, and using it is undefined.
*
* - Due to Span's automatic creation from range-like objects (arrays, and data
* - Due to spans automatic creation from range-like objects (arrays, and data
* types that expose a data() and size() member function), functions that
* accept a Span as input parameter can be called with any compatible
* accept a span as input parameter can be called with any compatible
* range-like object. For example, this works:
*
* void Foo(Span<const int> arg);
* void Foo(std::span<const int> arg);
*
* Foo(std::vector<int>{1, 2, 3}); // Works
*
@ -50,10 +50,10 @@
* container, and only about having exactly a range of elements. However it
* may also be surprising to see automatic conversions in this case.
*
* When a function accepts a Span with a mutable element type, it will not
* When a function accepts a span with a mutable element type, it will not
* accept temporaries; only variables or other references. For example:
*
* void FooMut(Span<int> arg);
* void FooMut(std::span<int> arg);
*
* FooMut(std::vector<int>{1, 2, 3}); // Does not compile
* std::vector<int> baz{1, 2, 3};
@ -69,7 +69,6 @@
* result will be present in that variable after the call. Passing a temporary
* is useless in that context.
*/
#define Span std::span
/** Pop the last element off a span, and return a reference to that element. */
template <typename T>
@ -81,18 +80,6 @@ T& SpanPopBack(std::span<T>& span)
return back;
}
// From C++20 as_bytes and as_writeable_bytes
template <typename T>
Span<const std::byte> AsBytes(Span<T> s) noexcept
{
return {reinterpret_cast<const std::byte*>(s.data()), s.size_bytes()};
}
template <typename T>
Span<std::byte> AsWritableBytes(Span<T> s) noexcept
{
return {reinterpret_cast<std::byte*>(s.data()), s.size_bytes()};
}
template <typename V>
auto MakeByteSpan(const V& v) noexcept
{
@ -117,10 +104,10 @@ inline const unsigned char* UCharCast(const std::byte* c) { return reinterpret_c
template <typename B>
concept BasicByte = requires { UCharCast(std::span<B>{}.data()); };
// Helper function to safely convert a Span to a Span<[const] unsigned char>.
// Helper function to safely convert a span to a span<[const] unsigned char>.
template <typename T, size_t N> constexpr auto UCharSpanCast(std::span<T, N> s) { return std::span<std::remove_pointer_t<decltype(UCharCast(s.data()))>, N>{UCharCast(s.data()), s.size()}; }
/** Like the Span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
/** Like the std::span constructor, but for (const) unsigned char member types only. Only works for (un)signed char containers. */
template <typename V> constexpr auto MakeUCharSpan(const V& v) -> decltype(UCharSpanCast(std::span{v})) { return UCharSpanCast(std::span{v}); }
template <typename V> constexpr auto MakeWritableUCharSpan(V&& v) -> decltype(UCharSpanCast(std::span{std::forward<V>(v)})) { return UCharSpanCast(std::span{std::forward<V>(v)}); }

View File

@ -44,9 +44,9 @@ BOOST_AUTO_TEST_SUITE(span_tests)
// Make sure template std::span template deduction guides accurately enable calls to
// std::span constructor overloads that work, and disable calls to constructor overloads that
// don't work. This makes it is possible to use the std::span constructor in a SFINAE
// don't work. This makes it possible to use the std::span constructor in a SFINAE
// contexts like in the Spannable function above to detect whether types are or
// aren't compatible with Spans at compile time.
// aren't compatible with std::span at compile time.
BOOST_AUTO_TEST_CASE(span_constructor_sfinae)
{
BOOST_CHECK(Spannable(std::vector<int>{}));