Installable::getCursors: Cleanup

- Clarify doc comments, Installables::getCursors returns non-empty
  vector
- Use vector::at in Installable::getCursor instead of checking for empty
  vector and throwing an exception with error message.
This commit is contained in:
dramforever 2023-01-30 23:56:27 +08:00
parent b26562c629
commit 377d5eb388
2 changed files with 9 additions and 6 deletions

View file

@ -379,10 +379,9 @@ Installable::getCursors(EvalState & state)
ref<eval_cache::AttrCursor> ref<eval_cache::AttrCursor>
Installable::getCursor(EvalState & state) Installable::getCursor(EvalState & state)
{ {
auto cursors = getCursors(state); /* Although getCursors should return at least one element, in case it doesn't,
if (cursors.empty()) bound check to avoid an undefined behavior for vector[0] */
throw Error("cannot find flake attribute '%s'", what()); return getCursors(state).at(0);
return cursors[0];
} }
static StorePath getDeriver( static StorePath getDeriver(

View file

@ -103,9 +103,13 @@ struct Installable
return {}; return {};
} }
/* Get a cursor to each value this Installable could refer to. However
if none exists, throw exception instead of returning empty vector. */
virtual std::vector<ref<eval_cache::AttrCursor>> virtual std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state); getCursors(EvalState & state);
/* Get the first and most preferred cursor this Installable could refer
to, or throw an exception if none exists. */
virtual ref<eval_cache::AttrCursor> virtual ref<eval_cache::AttrCursor>
getCursor(EvalState & state); getCursor(EvalState & state);
@ -193,8 +197,8 @@ struct InstallableFlake : InstallableValue
std::pair<Value *, PosIdx> toValue(EvalState & state) override; std::pair<Value *, PosIdx> toValue(EvalState & state) override;
/* Get a cursor to every attrpath in getActualAttrPaths() that /* Get a cursor to every attrpath in getActualAttrPaths()
exists. */ that exists. However if none exists, throw an exception. */
std::vector<ref<eval_cache::AttrCursor>> std::vector<ref<eval_cache::AttrCursor>>
getCursors(EvalState & state) override; getCursors(EvalState & state) override;