Skip to content

Support Span to construct from a single element similar to ArrayRef #11757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions runtime/core/span.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ class Span final {
template <size_t N>
/* implicit */ constexpr Span(T (&Arr)[N]) : data_(Arr), length_(N) {}

/// Construct a Span from a single element reference.
/* implicit */ constexpr Span(T& single_element)
: data_(&single_element), length_(1) {}

/// @returns a pointer to the start of the underlying element buffer.
iterator begin() const noexcept {
return data_;
Expand Down
16 changes: 16 additions & 0 deletions runtime/core/test/span_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,19 @@ TEST(SpanTest, TriviallyCopyable) {
EXPECT_EQ(span.size(), span_copy.size());
EXPECT_TRUE(std::is_trivially_copyable<Span<int64_t>>::value);
}

TEST(SpanTest, SingleElementConstructor) {
int64_t single_value = 42;
Span<int64_t> span = single_value;

EXPECT_EQ(span.size(), 1);
EXPECT_EQ(span.data(), &single_value);
EXPECT_EQ(span[0], 42);
EXPECT_EQ(*span.begin(), 42);
EXPECT_EQ(span.end(), span.begin() + 1);

// Test that modifying through span affects original value
span[0] = 100;
EXPECT_EQ(single_value, 100);
EXPECT_EQ(span[0], 100);
}
Loading