Skip to content

Commit

Permalink
Introduce OutputName and OutputNameView type aliases
Browse files Browse the repository at this point in the history
Hopefully they make the code easier to understand!
  • Loading branch information
Ericson2314 committed Aug 25, 2023
1 parent 0a6ac13 commit 2f5d3da
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 33 deletions.
2 changes: 1 addition & 1 deletion src/libexpr/primops.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1843,7 +1843,7 @@ static void prim_outputOf(EvalState & state, const PosIdx pos, Value * * args, V
{
SingleDerivedPath drvPath = state.coerceToSingleDerivedPath(pos, *args[0], "while evaluating the first argument to builtins.outputOf");

std::string_view outputName = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument to builtins.outputOf");
OutputNameView outputName = state.forceStringNoCtx(*args[1], pos, "while evaluating the second argument to builtins.outputOf");

state.mkSingleDerivedPathString(
SingleDerivedPath::Built {
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/local-derivation-goal.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2955,7 +2955,7 @@ bool LocalDerivationGoal::isReadDesc(int fd)
}


StorePath LocalDerivationGoal::makeFallbackPath(std::string_view outputName)
StorePath LocalDerivationGoal::makeFallbackPath(OutputNameView outputName)
{
return worker.store.makeStorePath(
"rewrite:" + std::string(drvPath.to_string()) + ":name:" + std::string(outputName),
Expand Down
2 changes: 1 addition & 1 deletion src/libstore/build/local-derivation-goal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ struct LocalDerivationGoal : public DerivationGoal
* @todo Add option to randomize, so we can audit whether our
* rewrites caught everything
*/
StorePath makeFallbackPath(std::string_view outputName);
StorePath makeFallbackPath(OutputNameView outputName);
};

}
12 changes: 6 additions & 6 deletions src/libstore/derivations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace nix {

std::optional<StorePath> DerivationOutput::path(const Store & store, std::string_view drvName, std::string_view outputName) const
std::optional<StorePath> DerivationOutput::path(const Store & store, std::string_view drvName, OutputNameView outputName) const
{
return std::visit(overloaded {
[](const DerivationOutput::InputAddressed & doi) -> std::optional<StorePath> {
Expand All @@ -36,7 +36,7 @@ std::optional<StorePath> DerivationOutput::path(const Store & store, std::string
}


StorePath DerivationOutput::CAFixed::path(const Store & store, std::string_view drvName, std::string_view outputName) const
StorePath DerivationOutput::CAFixed::path(const Store & store, std::string_view drvName, OutputNameView outputName) const
{
return store.makeFixedOutputPathFromCA(
outputPathName(drvName, outputName),
Expand Down Expand Up @@ -466,7 +466,7 @@ bool isDerivation(std::string_view fileName)
}


std::string outputPathName(std::string_view drvName, std::string_view outputName) {
std::string outputPathName(std::string_view drvName, OutputNameView outputName) {
std::string res { drvName };
if (outputName != "out") {
res += "-";
Expand Down Expand Up @@ -810,7 +810,7 @@ void writeDerivation(Sink & out, const Store & store, const BasicDerivation & dr
}


std::string hashPlaceholder(const std::string_view outputName)
std::string hashPlaceholder(const OutputNameView outputName)
{
// FIXME: memoize?
return "/" + hashString(htSHA256, concatStrings("nix-output:", outputName)).to_string(Base32, false);
Expand Down Expand Up @@ -963,7 +963,7 @@ void Derivation::checkInvariants(Store & store, const StorePath & drvPath) const
const Hash impureOutputHash = hashString(htSHA256, "impure");

nlohmann::json DerivationOutput::toJSON(
const Store & store, std::string_view drvName, std::string_view outputName) const
const Store & store, std::string_view drvName, OutputNameView outputName) const
{
nlohmann::json res = nlohmann::json::object();
std::visit(overloaded {
Expand All @@ -990,7 +990,7 @@ nlohmann::json DerivationOutput::toJSON(


DerivationOutput DerivationOutput::fromJSON(
const Store & store, std::string_view drvName, std::string_view outputName,
const Store & store, std::string_view drvName, OutputNameView outputName,
const nlohmann::json & _json,
const ExperimentalFeatureSettings & xpSettings)
{
Expand Down
12 changes: 6 additions & 6 deletions src/libstore/derivations.hh
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct DerivationOutput
* @param drvName The name of the derivation this is an output of, without the `.drv`.
* @param outputName The name of this output.
*/
StorePath path(const Store & store, std::string_view drvName, std::string_view outputName) const;
StorePath path(const Store & store, std::string_view drvName, OutputNameView outputName) const;

GENERATE_CMP(CAFixed, me->ca);
};
Expand Down Expand Up @@ -132,19 +132,19 @@ struct DerivationOutput
* the safer interface provided by
* BasicDerivation::outputsAndOptPaths
*/
std::optional<StorePath> path(const Store & store, std::string_view drvName, std::string_view outputName) const;
std::optional<StorePath> path(const Store & store, std::string_view drvName, OutputNameView outputName) const;

nlohmann::json toJSON(
const Store & store,
std::string_view drvName,
std::string_view outputName) const;
OutputNameView outputName) const;
/**
* @param xpSettings Stop-gap to avoid globals during unit tests.
*/
static DerivationOutput fromJSON(
const Store & store,
std::string_view drvName,
std::string_view outputName,
OutputNameView outputName,
const nlohmann::json & json,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
};
Expand Down Expand Up @@ -405,7 +405,7 @@ bool isDerivation(std::string_view fileName);
* This is usually <drv-name>-<output-name>, but is just <drv-name> when
* the output name is "out".
*/
std::string outputPathName(std::string_view drvName, std::string_view outputName);
std::string outputPathName(std::string_view drvName, OutputNameView outputName);


/**
Expand Down Expand Up @@ -499,7 +499,7 @@ void writeDerivation(Sink & out, const Store & store, const BasicDerivation & dr
* own outputs without needing to use the hash of a derivation in
* itself, making the hash near-impossible to calculate.
*/
std::string hashPlaceholder(const std::string_view outputName);
std::string hashPlaceholder(const OutputNameView outputName);

extern const Hash impureOutputHash;

Expand Down
4 changes: 2 additions & 2 deletions src/libstore/derived-path.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void drvRequireExperiment(

SingleDerivedPath::Built SingleDerivedPath::Built::parse(
const Store & store, ref<SingleDerivedPath> drv,
std::string_view output,
OutputNameView output,
const ExperimentalFeatureSettings & xpSettings)
{
drvRequireExperiment(*drv, xpSettings);
Expand All @@ -179,7 +179,7 @@ SingleDerivedPath::Built SingleDerivedPath::Built::parse(

DerivedPath::Built DerivedPath::Built::parse(
const Store & store, ref<SingleDerivedPath> drv,
std::string_view outputsS,
OutputNameView outputsS,
const ExperimentalFeatureSettings & xpSettings)
{
drvRequireExperiment(*drv, xpSettings);
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/derived-path.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct SingleDerivedPath;
*/
struct SingleDerivedPathBuilt {
ref<SingleDerivedPath> drvPath;
std::string output;
OutputName output;

/**
* Get the store path this is ultimately derived from (by realising
Expand Down Expand Up @@ -71,7 +71,7 @@ struct SingleDerivedPathBuilt {
*/
static SingleDerivedPathBuilt parse(
const Store & store, ref<SingleDerivedPath> drvPath,
std::string_view outputs,
OutputNameView outputs,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;

Expand Down
4 changes: 2 additions & 2 deletions src/libstore/downstream-placeholder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ std::string DownstreamPlaceholder::render() const

DownstreamPlaceholder DownstreamPlaceholder::unknownCaOutput(
const StorePath & drvPath,
std::string_view outputName,
OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings)
{
xpSettings.require(Xp::CaDerivations);
Expand All @@ -25,7 +25,7 @@ DownstreamPlaceholder DownstreamPlaceholder::unknownCaOutput(

DownstreamPlaceholder DownstreamPlaceholder::unknownDerivation(
const DownstreamPlaceholder & placeholder,
std::string_view outputName,
OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings)
{
xpSettings.require(Xp::DynamicDerivations);
Expand Down
4 changes: 2 additions & 2 deletions src/libstore/downstream-placeholder.hh
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public:
*/
static DownstreamPlaceholder unknownCaOutput(
const StorePath & drvPath,
std::string_view outputName,
OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);

/**
Expand All @@ -72,7 +72,7 @@ public:
*/
static DownstreamPlaceholder unknownDerivation(
const DownstreamPlaceholder & drvPlaceholder,
std::string_view outputName,
OutputNameView outputName,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);

/**
Expand Down
26 changes: 19 additions & 7 deletions src/libstore/outputs-spec.hh
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,36 @@

namespace nix {

/**
* An (owned) output name. Just a type alias used to make code more
* readible.
*/
typedef std::string OutputName;

/**
* A borrowed output name. Just a type alias used to make code more
* readible.
*/
typedef std::string_view OutputNameView;

struct OutputsSpec {
/**
* A non-empty set of outputs, specified by name
*/
struct Names : std::set<std::string> {
using std::set<std::string>::set;
struct Names : std::set<OutputName> {
using std::set<OutputName>::set;

/* These need to be "inherited manually" */

Names(const std::set<std::string> & s)
: std::set<std::string>(s)
Names(const std::set<OutputName> & s)
: std::set<OutputName>(s)
{ assert(!empty()); }

/**
* Needs to be "inherited manually"
*/
Names(std::set<std::string> && s)
: std::set<std::string>(s)
Names(std::set<OutputName> && s)
: std::set<OutputName>(s)
{ assert(!empty()); }

/* This set should always be non-empty, so we delete this
Expand All @@ -57,7 +69,7 @@ struct OutputsSpec {
*/
OutputsSpec() = delete;

bool contains(const std::string & output) const;
bool contains(const OutputName & output) const;

/**
* Create a new OutputsSpec which is the union of this and that.
Expand Down
6 changes: 3 additions & 3 deletions src/libstore/realisation.hh
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct DrvOutput {
/**
* The name of the output.
*/
std::string outputName;
OutputName outputName;

std::string to_string() const;

Expand Down Expand Up @@ -84,7 +84,7 @@ struct Realisation {
* Since these are the outputs of a single derivation, we know the
* output names are unique so we can use them as the map key.
*/
typedef std::map<std::string, Realisation> SingleDrvOutputs;
typedef std::map<OutputName, Realisation> SingleDrvOutputs;

/**
* Collection type for multiple derivations' outputs' `Realisation`s.
Expand Down Expand Up @@ -146,7 +146,7 @@ public:
MissingRealisation(DrvOutput & outputId)
: MissingRealisation(outputId.outputName, outputId.strHash())
{}
MissingRealisation(std::string_view drv, std::string outputName)
MissingRealisation(std::string_view drv, OutputName outputName)
: Error( "cannot operate on output '%s' of the "
"unbuilt derivation '%s'",
outputName,
Expand Down

0 comments on commit 2f5d3da

Please sign in to comment.