diff options
author | dramforever <dramforever@live.com> | 2023-01-30 23:56:27 +0800 |
---|---|---|
committer | dramforever <dramforever@live.com> | 2023-01-31 00:04:05 +0800 |
commit | 377d5eb38815d8290ab444c81c7826f46ba79844 (patch) | |
tree | ca4d2d06dbbd5744fb5815084e15a9d5752d822e | |
parent | b26562c629604b76c58b789ee520581cc5549433 (diff) |
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.
-rw-r--r-- | src/libcmd/installables.cc | 7 | ||||
-rw-r--r-- | src/libcmd/installables.hh | 8 |
2 files changed, 9 insertions, 6 deletions
diff --git a/src/libcmd/installables.cc b/src/libcmd/installables.cc index f8632a535..24f458f1a 100644 --- a/src/libcmd/installables.cc +++ b/src/libcmd/installables.cc @@ -379,10 +379,9 @@ Installable::getCursors(EvalState & state) ref<eval_cache::AttrCursor> Installable::getCursor(EvalState & state) { - auto cursors = getCursors(state); - if (cursors.empty()) - throw Error("cannot find flake attribute '%s'", what()); - return cursors[0]; + /* Although getCursors should return at least one element, in case it doesn't, + bound check to avoid an undefined behavior for vector[0] */ + return getCursors(state).at(0); } static StorePath getDeriver( diff --git a/src/libcmd/installables.hh b/src/libcmd/installables.hh index d489315df..da6a3addd 100644 --- a/src/libcmd/installables.hh +++ b/src/libcmd/installables.hh @@ -103,9 +103,13 @@ struct Installable 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>> 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> getCursor(EvalState & state); @@ -193,8 +197,8 @@ struct InstallableFlake : InstallableValue std::pair<Value *, PosIdx> toValue(EvalState & state) override; - /* Get a cursor to every attrpath in getActualAttrPaths() that - exists. */ + /* Get a cursor to every attrpath in getActualAttrPaths() + that exists. However if none exists, throw an exception. */ std::vector<ref<eval_cache::AttrCursor>> getCursors(EvalState & state) override; |