Switch env_var to using maybe_t

This eliminates the "missing" notion of env_var_t. Instead
env_get returns a maybe_t<env_var_t>, which forces callers to
handle the possibility that the variable is missing.
This commit is contained in:
ridiculousfish
2017-08-28 00:25:41 -07:00
parent 18203a081c
commit 3d40292c00
23 changed files with 202 additions and 242 deletions

View File

@@ -79,11 +79,11 @@ static int load(const wcstring &name) {
static void autoload_names(std::unordered_set<wcstring> &names, int get_hidden) {
size_t i;
const env_var_t path_var = env_get(L"fish_function_path");
const auto path_var = env_get(L"fish_function_path");
if (path_var.missing_or_empty()) return;
wcstring_list_t path_list;
path_var.to_list(path_list);
path_var->to_list(path_list);
for (i = 0; i < path_list.size(); i++) {
const wcstring &ndir_str = path_list.at(i);
@@ -110,7 +110,8 @@ static void autoload_names(std::unordered_set<wcstring> &names, int get_hidden)
static std::map<wcstring, env_var_t> snapshot_vars(const wcstring_list_t &vars) {
std::map<wcstring, env_var_t> result;
for (wcstring_list_t::const_iterator it = vars.begin(), end = vars.end(); it != end; ++it) {
result.insert(std::make_pair(*it, env_get(*it)));
auto var = env_get(*it);
if (var) result.insert(std::make_pair(*it, std::move(*var)));
}
return result;
}
@@ -338,14 +339,6 @@ void function_prepare_environment(const wcstring &name, const wchar_t *const *ar
}
for (auto it = inherited_vars.begin(), end = inherited_vars.end(); it != end; ++it) {
// Note: Prior to my rewrite to address issue #4200 this code did the equivalent of this:
// if (it->second.missing()) {
// env_set_empty(it->first, ENV_LOCAL | ENV_USER);
// } else {
// It should be impossible for the var to be missing since we're inheriting it from an outer
// scope. So we now die horribly if it is missing.
assert(!it->second.missing());
wcstring_list_t vals = it->second.as_list(); // we need a copy
env_set(it->first, ENV_LOCAL | ENV_USER, vals); // because this mutates the list
env_set(it->first, ENV_LOCAL | ENV_USER, it->second.as_list());
}
}