From 448c7d50e1bfac6644c0cf13085eb1bbeda32e6b Mon Sep 17 00:00:00 2001 From: eldritch horrors Date: Sat, 5 Apr 2025 21:57:20 +0200 Subject: [PATCH] libutil: add shared_from_this support to ref we don't need to unsafely cast a shared_from_this pointer to create a ref (the pointer must be non-null to not invoke nasal demons anyway). Change-Id: I133fe5f07b2cff8ec2c925f0528f4c5288827261 --- lix/libexpr/eval-cache.cc | 2 +- lix/libstore/binary-cache-store.cc | 2 +- lix/libstore/remote-store.cc | 2 +- lix/libutil/ref.hh | 5 +++++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/lix/libexpr/eval-cache.cc b/lix/libexpr/eval-cache.cc index 03d11ebdb..ad6e0a069 100644 --- a/lix/libexpr/eval-cache.cc +++ b/lix/libexpr/eval-cache.cc @@ -352,7 +352,7 @@ Value * EvalCache::getRootValue(EvalState & state) ref EvalCache::getRoot() { - return make_ref(ref(shared_from_this()), std::nullopt); + return make_ref(*this, std::nullopt); } AttrCursor::AttrCursor( diff --git a/lix/libstore/binary-cache-store.cc b/lix/libstore/binary-cache-store.cc index 2dbf57d2f..77602efec 100644 --- a/lix/libstore/binary-cache-store.cc +++ b/lix/libstore/binary-cache-store.cc @@ -530,7 +530,7 @@ try { ref BinaryCacheStore::getFSAccessor() { - return make_ref(ref(shared_from_this()), config().localNarCache); + return make_ref(*this, config().localNarCache); } kj::Promise> diff --git a/lix/libstore/remote-store.cc b/lix/libstore/remote-store.cc index 6f2d61f18..e24c578b7 100644 --- a/lix/libstore/remote-store.cc +++ b/lix/libstore/remote-store.cc @@ -1022,7 +1022,7 @@ try { ref RemoteStore::getFSAccessor() { - return make_ref(ref(shared_from_this())); + return make_ref(*this); } static Logger::Fields readFields(Source & from) diff --git a/lix/libutil/ref.hh b/lix/libutil/ref.hh index 8ccad834b..312b7d09e 100644 --- a/lix/libutil/ref.hh +++ b/lix/libutil/ref.hh @@ -32,6 +32,11 @@ public: throw std::invalid_argument("null pointer cast to ref"); } + template> T2> + ref(T2 & r) : p(r.shared_from_this()) + { + } + T* operator ->() const { return &*p;