1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-04 03:01:47 +02:00

Remove comparator.hh and switch to <=> in a bunch of places

Known behavior changes:

- `MemorySourceAccessor`'s comparison operators no longer forget to
  compare the `SourceAccessor` base class.

Progress on #10832

What remains for that issue is hopefully much easier!
This commit is contained in:
John Ericson 2024-05-16 18:46:38 -04:00
parent 2a95a2d780
commit bc83b9dc1f
49 changed files with 300 additions and 271 deletions

View file

@ -2,17 +2,7 @@
namespace nix {
GENERATE_CMP_EXT(
,
BuildResult,
me->status,
me->errorMsg,
me->timesBuilt,
me->isNonDeterministic,
me->builtOutputs,
me->startTime,
me->stopTime,
me->cpuUser,
me->cpuSystem);
bool BuildResult::operator==(const BuildResult &) const noexcept = default;
std::strong_ordering BuildResult::operator<=>(const BuildResult &) const noexcept = default;
}

View file

@ -3,7 +3,6 @@
#include "realisation.hh"
#include "derived-path.hh"
#include "comparator.hh"
#include <string>
#include <chrono>
@ -101,7 +100,8 @@ struct BuildResult
*/
std::optional<std::chrono::microseconds> cpuUser, cpuSystem;
DECLARE_CMP(BuildResult);
bool operator ==(const BuildResult &) const noexcept;
std::strong_ordering operator <=>(const BuildResult &) const noexcept;
bool success()
{

View file

@ -73,6 +73,7 @@ struct ContentAddressMethod
Raw raw;
bool operator ==(const ContentAddressMethod &) const = default;
auto operator <=>(const ContentAddressMethod &) const = default;
MAKE_WRAPPER_CONSTRUCTOR(ContentAddressMethod);
@ -159,6 +160,7 @@ struct ContentAddress
*/
Hash hash;
bool operator ==(const ContentAddress &) const = default;
auto operator <=>(const ContentAddress &) const = default;
/**
@ -217,6 +219,10 @@ struct StoreReferences
* iff self is true.
*/
size_t size() const;
bool operator ==(const StoreReferences &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=>(const StoreReferences &) const = default;
};
// This matches the additional info that we need for makeTextPath
@ -232,6 +238,10 @@ struct TextInfo
* disallowed
*/
StorePathSet references;
bool operator ==(const TextInfo &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=>(const TextInfo &) const = default;
};
struct FixedOutputInfo
@ -250,6 +260,10 @@ struct FixedOutputInfo
* References to other store objects or this one.
*/
StoreReferences references;
bool operator ==(const FixedOutputInfo &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=>(const FixedOutputInfo &) const = default;
};
/**
@ -266,6 +280,10 @@ struct ContentAddressWithReferences
Raw raw;
bool operator ==(const ContentAddressWithReferences &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=>(const ContentAddressWithReferences &) const = default;
MAKE_WRAPPER_CONSTRUCTOR(ContentAddressWithReferences);
/**

View file

@ -8,7 +8,6 @@
#include "repair-flag.hh"
#include "derived-path-map.hh"
#include "sync.hh"
#include "comparator.hh"
#include "variant-wrapper.hh"
#include <map>
@ -32,7 +31,8 @@ struct DerivationOutput
{
StorePath path;
GENERATE_CMP(InputAddressed, me->path);
bool operator == (const InputAddressed &) const = default;
auto operator <=> (const InputAddressed &) const = default;
};
/**
@ -56,7 +56,8 @@ struct DerivationOutput
*/
StorePath path(const StoreDirConfig & store, std::string_view drvName, OutputNameView outputName) const;
GENERATE_CMP(CAFixed, me->ca);
bool operator == (const CAFixed &) const = default;
auto operator <=> (const CAFixed &) const = default;
};
/**
@ -76,7 +77,8 @@ struct DerivationOutput
*/
HashAlgorithm hashAlgo;
GENERATE_CMP(CAFloating, me->method, me->hashAlgo);
bool operator == (const CAFloating &) const = default;
auto operator <=> (const CAFloating &) const = default;
};
/**
@ -84,7 +86,8 @@ struct DerivationOutput
* isn't known yet.
*/
struct Deferred {
GENERATE_CMP(Deferred);
bool operator == (const Deferred &) const = default;
auto operator <=> (const Deferred &) const = default;
};
/**
@ -103,7 +106,8 @@ struct DerivationOutput
*/
HashAlgorithm hashAlgo;
GENERATE_CMP(Impure, me->method, me->hashAlgo);
bool operator == (const Impure &) const = default;
auto operator <=> (const Impure &) const = default;
};
typedef std::variant<
@ -116,7 +120,8 @@ struct DerivationOutput
Raw raw;
GENERATE_CMP(DerivationOutput, me->raw);
bool operator == (const DerivationOutput &) const = default;
auto operator <=> (const DerivationOutput &) const = default;
MAKE_WRAPPER_CONSTRUCTOR(DerivationOutput);
@ -177,7 +182,8 @@ struct DerivationType {
*/
bool deferred;
GENERATE_CMP(InputAddressed, me->deferred);
bool operator == (const InputAddressed &) const = default;
auto operator <=> (const InputAddressed &) const = default;
};
/**
@ -201,7 +207,8 @@ struct DerivationType {
*/
bool fixed;
GENERATE_CMP(ContentAddressed, me->sandboxed, me->fixed);
bool operator == (const ContentAddressed &) const = default;
auto operator <=> (const ContentAddressed &) const = default;
};
/**
@ -211,7 +218,8 @@ struct DerivationType {
* type, but has some restrictions on its usage.
*/
struct Impure {
GENERATE_CMP(Impure);
bool operator == (const Impure &) const = default;
auto operator <=> (const Impure &) const = default;
};
typedef std::variant<
@ -222,7 +230,8 @@ struct DerivationType {
Raw raw;
GENERATE_CMP(DerivationType, me->raw);
bool operator == (const DerivationType &) const = default;
auto operator <=> (const DerivationType &) const = default;
MAKE_WRAPPER_CONSTRUCTOR(DerivationType);
@ -312,14 +321,9 @@ struct BasicDerivation
static std::string_view nameFromPath(const StorePath & storePath);
GENERATE_CMP(BasicDerivation,
me->outputs,
me->inputSrcs,
me->platform,
me->builder,
me->args,
me->env,
me->name);
bool operator == (const BasicDerivation &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=> (const BasicDerivation &) const = default;
};
class Store;
@ -377,9 +381,9 @@ struct Derivation : BasicDerivation
const nlohmann::json & json,
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
GENERATE_CMP(Derivation,
static_cast<const BasicDerivation &>(*me),
me->inputDrvs);
bool operator == (const Derivation &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
//auto operator <=> (const Derivation &) const = default;
};

View file

@ -54,17 +54,18 @@ typename DerivedPathMap<V>::ChildNode * DerivedPathMap<V>::findSlot(const Single
namespace nix {
GENERATE_CMP_EXT(
template<>,
DerivedPathMap<std::set<std::string>>::ChildNode,
me->value,
me->childMap);
template<>
bool DerivedPathMap<std::set<std::string>>::ChildNode::operator == (
const DerivedPathMap<std::set<std::string>>::ChildNode &) const noexcept = default;
GENERATE_CMP_EXT(
template<>,
DerivedPathMap<std::set<std::string>>,
me->map);
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
#if 0
template<>
std::strong_ordering DerivedPathMap<std::set<std::string>>::ChildNode::operator <=> (
const DerivedPathMap<std::set<std::string>>::ChildNode &) const noexcept = default;
#endif
template struct DerivedPathMap<std::set<std::string>>::ChildNode;
template struct DerivedPathMap<std::set<std::string>>;
};

View file

@ -47,7 +47,11 @@ struct DerivedPathMap {
*/
Map childMap;
DECLARE_CMP(ChildNode);
bool operator == (const ChildNode &) const noexcept;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
// decltype(std::declval<V>() <=> std::declval<V>())
// operator <=> (const ChildNode &) const noexcept;
};
/**
@ -60,7 +64,10 @@ struct DerivedPathMap {
*/
Map map;
DECLARE_CMP(DerivedPathMap);
bool operator == (const DerivedPathMap &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
// auto operator <=> (const DerivedPathMap &) const noexcept;
/**
* Find the node for `k`, creating it if needed.
@ -83,14 +90,21 @@ struct DerivedPathMap {
ChildNode * findSlot(const SingleDerivedPath & k);
};
template<>
bool DerivedPathMap<std::set<std::string>>::ChildNode::operator == (
const DerivedPathMap<std::set<std::string>>::ChildNode &) const noexcept;
DECLARE_CMP_EXT(
template<>,
DerivedPathMap<std::set<std::string>>::,
DerivedPathMap<std::set<std::string>>);
DECLARE_CMP_EXT(
template<>,
DerivedPathMap<std::set<std::string>>::ChildNode::,
DerivedPathMap<std::set<std::string>>::ChildNode);
// TODO libc++ 16 (used by darwin) missing `std::map::operator <=>`, can't do yet.
#if 0
template<>
std::strong_ordering DerivedPathMap<std::set<std::string>>::ChildNode::operator <=> (
const DerivedPathMap<std::set<std::string>>::ChildNode &) const noexcept;
template<>
inline auto DerivedPathMap<std::set<std::string>>::operator <=> (const DerivedPathMap<std::set<std::string>> &) const noexcept = default;
#endif
extern template struct DerivedPathMap<std::set<std::string>>::ChildNode;
extern template struct DerivedPathMap<std::set<std::string>>;
}

View file

@ -1,6 +1,7 @@
#include "derived-path.hh"
#include "derivations.hh"
#include "store-api.hh"
#include "comparator.hh"
#include <nlohmann/json.hpp>
@ -8,26 +9,32 @@
namespace nix {
#define CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, COMPARATOR) \
bool MY_TYPE ::operator COMPARATOR (const MY_TYPE & other) const \
{ \
const MY_TYPE* me = this; \
auto fields1 = std::tie(*me->drvPath, me->FIELD); \
me = &other; \
auto fields2 = std::tie(*me->drvPath, me->FIELD); \
return fields1 COMPARATOR fields2; \
}
#define CMP(CHILD_TYPE, MY_TYPE, FIELD) \
CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, ==) \
CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, !=) \
CMP_ONE(CHILD_TYPE, MY_TYPE, FIELD, <)
// Custom implementation to avoid `ref` ptr equality
GENERATE_CMP_EXT(
,
std::strong_ordering,
SingleDerivedPathBuilt,
*me->drvPath,
me->output);
CMP(SingleDerivedPath, SingleDerivedPathBuilt, output)
// Custom implementation to avoid `ref` ptr equality
CMP(SingleDerivedPath, DerivedPathBuilt, outputs)
#undef CMP
#undef CMP_ONE
// TODO no `GENERATE_CMP_EXT` because no `std::set::operator<=>` on
// Darwin, per header.
GENERATE_EQUAL(
,
DerivedPathBuilt ::,
DerivedPathBuilt,
*me->drvPath,
me->outputs);
GENERATE_ONE_CMP(
,
bool,
DerivedPathBuilt ::,
<,
DerivedPathBuilt,
*me->drvPath,
me->outputs);
nlohmann::json DerivedPath::Opaque::toJSON(const StoreDirConfig & store) const
{

View file

@ -3,7 +3,6 @@
#include "path.hh"
#include "outputs-spec.hh"
#include "comparator.hh"
#include "config.hh"
#include "ref.hh"
@ -32,7 +31,8 @@ struct DerivedPathOpaque {
static DerivedPathOpaque parse(const StoreDirConfig & store, std::string_view);
nlohmann::json toJSON(const StoreDirConfig & store) const;
GENERATE_CMP(DerivedPathOpaque, me->path);
bool operator == (const DerivedPathOpaque &) const = default;
auto operator <=> (const DerivedPathOpaque &) const = default;
};
struct SingleDerivedPath;
@ -79,7 +79,8 @@ struct SingleDerivedPathBuilt {
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;
DECLARE_CMP(SingleDerivedPathBuilt);
bool operator == (const SingleDerivedPathBuilt &) const noexcept;
std::strong_ordering operator <=> (const SingleDerivedPathBuilt &) const noexcept;
};
using _SingleDerivedPathRaw = std::variant<
@ -109,6 +110,9 @@ struct SingleDerivedPath : _SingleDerivedPathRaw {
return static_cast<const Raw &>(*this);
}
bool operator == (const SingleDerivedPath &) const = default;
auto operator <=> (const SingleDerivedPath &) const = default;
/**
* Get the store path this is ultimately derived from (by realising
* and projecting outputs).
@ -202,7 +206,9 @@ struct DerivedPathBuilt {
const ExperimentalFeatureSettings & xpSettings = experimentalFeatureSettings);
nlohmann::json toJSON(Store & store) const;
DECLARE_CMP(DerivedPathBuilt);
bool operator == (const DerivedPathBuilt &) const noexcept;
// TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet.
bool operator < (const DerivedPathBuilt &) const noexcept;
};
using _DerivedPathRaw = std::variant<
@ -231,6 +237,10 @@ struct DerivedPath : _DerivedPathRaw {
return static_cast<const Raw &>(*this);
}
bool operator == (const DerivedPath &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet.
//auto operator <=> (const DerivedPath &) const = default;
/**
* Get the store path this is ultimately derived from (by realising
* and projecting outputs).

View file

@ -4,15 +4,6 @@
namespace nix {
GENERATE_CMP_EXT(
,
NarInfo,
me->url,
me->compression,
me->fileHash,
me->fileSize,
static_cast<const ValidPathInfo &>(*me));
NarInfo::NarInfo(const Store & store, const std::string & s, const std::string & whence)
: ValidPathInfo(StorePath(StorePath::dummy), Hash(Hash::dummy)) // FIXME: hack
{

View file

@ -24,7 +24,9 @@ struct NarInfo : ValidPathInfo
NarInfo(const ValidPathInfo & info) : ValidPathInfo(info) { }
NarInfo(const Store & store, const std::string & s, const std::string & whence);
DECLARE_CMP(NarInfo);
bool operator ==(const NarInfo &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::optional::operator <=>`, can't do yet
//auto operator <=>(const NarInfo &) const = default;
std::string to_string(const Store & store) const;

View file

@ -6,9 +6,7 @@
#include <set>
#include <variant>
#include "comparator.hh"
#include "json-impls.hh"
#include "comparator.hh"
#include "variant-wrapper.hh"
namespace nix {
@ -60,7 +58,11 @@ struct OutputsSpec {
Raw raw;
GENERATE_CMP(OutputsSpec, me->raw);
bool operator == (const OutputsSpec &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet.
bool operator < (const OutputsSpec & other) const {
return raw < other.raw;
}
MAKE_WRAPPER_CONSTRUCTOR(OutputsSpec);
@ -99,7 +101,9 @@ struct ExtendedOutputsSpec {
Raw raw;
GENERATE_CMP(ExtendedOutputsSpec, me->raw);
bool operator == (const ExtendedOutputsSpec &) const = default;
// TODO libc++ 16 (used by darwin) missing `std::set::operator <=>`, can't do yet.
bool operator < (const ExtendedOutputsSpec &) const;
MAKE_WRAPPER_CONSTRUCTOR(ExtendedOutputsSpec);

View file

@ -3,11 +3,13 @@
#include "path-info.hh"
#include "store-api.hh"
#include "json-utils.hh"
#include "comparator.hh"
namespace nix {
GENERATE_CMP_EXT(
,
std::weak_ordering,
UnkeyedValidPathInfo,
me->deriver,
me->narHash,
@ -19,12 +21,6 @@ GENERATE_CMP_EXT(
me->sigs,
me->ca);
GENERATE_CMP_EXT(
,
ValidPathInfo,
me->path,
static_cast<const UnkeyedValidPathInfo &>(*me));
std::string ValidPathInfo::fingerprint(const Store & store) const
{
if (narSize == 0)

View file

@ -32,17 +32,47 @@ struct SubstitutablePathInfo
using SubstitutablePathInfos = std::map<StorePath, SubstitutablePathInfo>;
/**
* Information about a store object.
*
* See `store/store-object` and `protocols/json/store-object-info` in
* the Nix manual
*/
struct UnkeyedValidPathInfo
{
/**
* Path to derivation that produced this store object, if known.
*/
std::optional<StorePath> deriver;
/**
* \todo document this
*/
Hash narHash;
/**
* Other store objects this store object referes to.
*/
StorePathSet references;
/**
* When this store object was registered in the store that contains
* it, if known.
*/
time_t registrationTime = 0;
uint64_t narSize = 0; // 0 = unknown
uint64_t id = 0; // internal use only
/**
* 0 = unknown
*/
uint64_t narSize = 0;
/**
* internal use only: SQL primary key for on-disk store objects with
* `LocalStore`.
*
* @todo Remove, layer violation
*/
uint64_t id = 0;
/**
* Whether the path is ultimately trusted, that is, it's a
@ -75,7 +105,12 @@ struct UnkeyedValidPathInfo
UnkeyedValidPathInfo(Hash narHash) : narHash(narHash) { };
DECLARE_CMP(UnkeyedValidPathInfo);
bool operator == (const UnkeyedValidPathInfo &) const noexcept;
/**
* @todo return `std::strong_ordering` once `id` is removed
*/
std::weak_ordering operator <=> (const UnkeyedValidPathInfo &) const noexcept;
virtual ~UnkeyedValidPathInfo() { }
@ -95,7 +130,8 @@ struct UnkeyedValidPathInfo
struct ValidPathInfo : UnkeyedValidPathInfo {
StorePath path;
DECLARE_CMP(ValidPathInfo);
bool operator == (const ValidPathInfo &) const = default;
auto operator <=> (const ValidPathInfo &) const = default;
/**
* Return a fingerprint of the store path to be used in binary