Skip to content

Commit 3bb28ba

Browse files
authored
Return result from get_backend_name (#9465)
1 parent f721779 commit 3bb28ba

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

runtime/executor/method_meta.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,15 @@ size_t MethodMeta::num_backends() const {
245245
return delegates ? delegates->size() : 0;
246246
}
247247

248-
const char* MethodMeta::get_backend_name(size_t index) const {
249-
const auto delegates = s_plan_->delegates();
250-
if (delegates && index < delegates->size()) {
251-
return delegates->Get(index)->id()->c_str();
252-
}
253-
return nullptr;
248+
Result<const char*> MethodMeta::get_backend_name(size_t index) const {
249+
const auto count = num_backends();
250+
ET_CHECK_OR_RETURN_ERROR(
251+
index < count,
252+
InvalidArgument,
253+
"Index %zu out of range. num_backends: %zu",
254+
index,
255+
count);
256+
return s_plan_->delegates()->Get(index)->id()->c_str();
254257
}
255258

256259
size_t MethodMeta::num_instructions() const {

runtime/executor/method_meta.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,10 +204,10 @@ class MethodMeta final {
204204
* Get the backend name at the given index.
205205
*
206206
* @param[in] index The index of the backend name.
207-
* @returns The backend name as a C-style string, or nullptr if the index is
208-
* invalid.
207+
* @returns A Result wrapping the backend name as a C-style string
208+
* on success, or an error if the index is invalid.
209209
*/
210-
const char* get_backend_name(size_t index) const;
210+
Result<const char*> get_backend_name(size_t index) const;
211211

212212
/**
213213
* Get the number of instructions in this method.

runtime/executor/test/backend_integration_test.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,18 +362,20 @@ TEST_P(BackendIntegrationTest, GetBackendNamesSuccess) {
362362
EXPECT_TRUE(method_meta->uses_backend(StubBackend::kName));
363363

364364
// Retrieve the number of backends.
365-
size_t num_backends = method_meta->num_backends();
365+
const size_t num_backends = method_meta->num_backends();
366366
EXPECT_GT(num_backends, 0u);
367367

368368
// Iterate through each backend and verify its name.
369369
for (size_t i = 0; i < num_backends; ++i) {
370-
const char* name = method_meta->get_backend_name(i);
371-
EXPECT_NE(name, nullptr);
370+
auto backend_name_result = method_meta->get_backend_name(i);
371+
ASSERT_TRUE(backend_name_result.ok());
372+
const char* name = backend_name_result.get();
372373
// For this test, we expect that the only backend is StubBackend.
373374
EXPECT_STREQ(name, StubBackend::kName);
374375
}
375-
// Check that an out-of-range index returns nullptr.
376-
EXPECT_EQ(method_meta->get_backend_name(num_backends), nullptr);
376+
// Check that an out-of-range index returns an error.
377+
auto out_of_range_result = method_meta->get_backend_name(num_backends);
378+
EXPECT_FALSE(out_of_range_result.ok());
377379
}
378380

379381
TEST_P(BackendIntegrationTest, FreeingProcessedBufferSucceeds) {

0 commit comments

Comments
 (0)