1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-16 07:22:43 +01:00

Merge pull request #13688 from xokdvium/odr-toplevel-types

treewide: Move private types and code into anonymous namespaces
This commit is contained in:
Jörg Thalheim 2025-08-05 14:05:16 +02:00 committed by GitHub
commit 4a7285c649
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 23 additions and 10 deletions

View file

@ -81,6 +81,8 @@ std::optional<std::string> RootArgs::needsCompletion(std::string_view s)
return {};
}
namespace {
/**
* Basically this is `typedef std::optional<Parser> Parser(std::string_view s, Strings & r);`
*
@ -246,6 +248,8 @@ void ParseQuoted::operator()(std::shared_ptr<Parser> & state, Strings & r)
assert(false);
}
} // namespace
Strings parseShebangContent(std::string_view s)
{
Strings result;

View file

@ -258,7 +258,7 @@ Hash newHashAllowEmpty(std::string_view hashStr, std::optional<HashAlgorithm> ha
return Hash::parseAny(hashStr, ha);
}
union Ctx
union Hash::Ctx
{
blake3_hasher blake3;
MD5_CTX md5;
@ -267,7 +267,7 @@ union Ctx
SHA512_CTX sha512;
};
static void start(HashAlgorithm ha, Ctx & ctx)
static void start(HashAlgorithm ha, Hash::Ctx & ctx)
{
if (ha == HashAlgorithm::BLAKE3)
blake3_hasher_init(&ctx.blake3);
@ -302,7 +302,7 @@ void blake3_hasher_update_with_heuristics(blake3_hasher * blake3, std::string_vi
}
}
static void update(HashAlgorithm ha, Ctx & ctx, std::string_view data)
static void update(HashAlgorithm ha, Hash::Ctx & ctx, std::string_view data)
{
if (ha == HashAlgorithm::BLAKE3)
blake3_hasher_update_with_heuristics(&ctx.blake3, data);
@ -316,7 +316,7 @@ static void update(HashAlgorithm ha, Ctx & ctx, std::string_view data)
SHA512_Update(&ctx.sha512, data.data(), data.size());
}
static void finish(HashAlgorithm ha, Ctx & ctx, unsigned char * hash)
static void finish(HashAlgorithm ha, Hash::Ctx & ctx, unsigned char * hash)
{
if (ha == HashAlgorithm::BLAKE3)
blake3_hasher_finalize(&ctx.blake3, hash, BLAKE3_OUT_LEN);
@ -332,7 +332,7 @@ static void finish(HashAlgorithm ha, Ctx & ctx, unsigned char * hash)
Hash hashString(HashAlgorithm ha, std::string_view s, const ExperimentalFeatureSettings & xpSettings)
{
Ctx ctx;
Hash::Ctx ctx;
Hash hash(ha, xpSettings);
start(ha, ctx);
update(ha, ctx, s);
@ -350,7 +350,7 @@ Hash hashFile(HashAlgorithm ha, const Path & path)
HashSink::HashSink(HashAlgorithm ha)
: ha(ha)
{
ctx = new Ctx;
ctx = new Hash::Ctx;
bytes = 0;
start(ha, *ctx);
}
@ -378,7 +378,7 @@ HashResult HashSink::finish()
HashResult HashSink::currentHash()
{
flush();
Ctx ctx2 = *ctx;
Hash::Ctx ctx2 = *ctx;
Hash hash(ha);
nix::finish(ha, ctx2, hash.hash);
return HashResult(hash, bytes);

View file

@ -55,6 +55,9 @@ extern const StringSet hashFormats;
struct Hash
{
/** Opaque handle type for the hash calculation state. */
union Ctx;
constexpr static size_t maxHashSize = 64;
size_t hashSize = 0;
uint8_t hash[maxHashSize] = {};
@ -222,8 +225,6 @@ std::optional<HashAlgorithm> parseHashAlgoOpt(std::string_view s);
*/
std::string_view printHashAlgo(HashAlgorithm ha);
union Ctx;
struct AbstractHashSink : virtual Sink
{
virtual HashResult finish() = 0;
@ -233,7 +234,7 @@ class HashSink : public BufferedSink, public AbstractHashSink
{
private:
HashAlgorithm ha;
Ctx * ctx;
Hash::Ctx * ctx;
uint64_t bytes;
public: