Skip to content

Commit 6f67b33

Browse files
committed
feat utils: mark more containers with lifetimebound annotations
commit_hash:24adda0d8e82eedfccb00e60d90f8d030c787e8d
1 parent 7432fd0 commit 6f67b33

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

core/include/userver/dynamic_config/snapshot.hpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string>
88
#include <type_traits>
99

10+
#include <userver/compiler/impl/lifetime.hpp>
1011
#include <userver/dynamic_config/impl/snapshot.hpp>
1112
#include <userver/dynamic_config/impl/to_json.hpp>
1213
#include <userver/formats/json_fwd.hpp>
@@ -137,7 +138,7 @@ class Snapshot final {
137138

138139
/// Used to access individual configs in the type-safe config map
139140
template <typename VariableType>
140-
const VariableType& operator[](const Key<VariableType>& key) const&;
141+
const VariableType& operator[](const Key<VariableType>& key) const& USERVER_IMPL_LIFETIME_BOUND;
141142

142143
/// Used to access individual configs in the type-safe config map
143144
template <typename VariableType>
@@ -146,7 +147,7 @@ class Snapshot final {
146147
/// @cond
147148
// No longer supported, use `config[key]` instead
148149
template <typename T>
149-
const T& Get() const&;
150+
const T& Get() const& USERVER_IMPL_LIFETIME_BOUND;
150151

151152
// No longer supported, use `config[key]` instead
152153
template <typename T>
@@ -258,7 +259,7 @@ VariableType Key<VariableType>::Parse(const DocsMap& docs_map) const {
258259
}
259260

260261
template <typename VariableType>
261-
const VariableType& Snapshot::operator[](const Key<VariableType>& key) const& {
262+
const VariableType& Snapshot::operator[](const Key<VariableType>& key) const& USERVER_IMPL_LIFETIME_BOUND {
262263
return GetData().Get<VariableType>(impl::ConfigIdGetter::Get(key));
263264
}
264265

@@ -268,7 +269,7 @@ const VariableType& Snapshot::operator[](const Key<VariableType>&) && {
268269
}
269270

270271
template <typename T>
271-
const T& Snapshot::Get() const& {
272+
const T& Snapshot::Get() const& USERVER_IMPL_LIFETIME_BOUND {
272273
return (*this)[T::kDeprecatedKey];
273274
}
274275

core/include/userver/dynamic_config/source.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <string_view>
88
#include <utility>
99

10+
#include <userver/compiler/impl/lifetime.hpp>
1011
#include <userver/concurrent/async_event_source.hpp>
1112
#include <userver/dynamic_config/snapshot.hpp>
1213
#include <userver/utils/assert.hpp>
@@ -23,10 +24,10 @@ class VariableSnapshotPtr final {
2324
VariableSnapshotPtr(VariableSnapshotPtr&&) = delete;
2425
VariableSnapshotPtr& operator=(VariableSnapshotPtr&&) = delete;
2526

26-
const VariableType& operator*() const& { return *variable_; }
27+
const VariableType& operator*() const& USERVER_IMPL_LIFETIME_BOUND { return *variable_; }
2728
const VariableType& operator*() && { ReportMisuse(); }
2829

29-
const VariableType* operator->() const& { return variable_; }
30+
const VariableType* operator->() const& USERVER_IMPL_LIFETIME_BOUND { return variable_; }
3031
const VariableType* operator->() && { ReportMisuse(); }
3132

3233
private:

universal/include/userver/utils/not_null.hpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,23 @@
88
#include <type_traits>
99
#include <utility>
1010

11+
#include <userver/compiler/impl/lifetime.hpp>
1112
#include <userver/utils/assert.hpp>
1213

1314
USERVER_NAMESPACE_BEGIN
1415

1516
namespace utils {
1617

18+
namespace impl {
19+
20+
template <typename T>
21+
struct IsStdSharedPtr : std::false_type {};
22+
23+
template <typename T>
24+
struct IsStdSharedPtr<std::shared_ptr<T>> : std::true_type {};
25+
26+
} // namespace impl
27+
1728
/// @ingroup userver_universal userver_containers
1829
///
1930
/// @brief Restricts a pointer or smart pointer to only hold non-null values.
@@ -91,6 +102,18 @@ class NotNull {
91102

92103
constexpr decltype(auto) operator*() const& { return *GetBase(); }
93104

105+
constexpr decltype(auto) operator->() const& USERVER_IMPL_LIFETIME_BOUND
106+
requires(!std::is_trivially_copyable_v<T>) && (!impl::IsStdSharedPtr<T>::value)
107+
{
108+
return GetBase();
109+
}
110+
111+
constexpr decltype(auto) operator*() const& USERVER_IMPL_LIFETIME_BOUND
112+
requires(!std::is_trivially_copyable_v<T>) && (!impl::IsStdSharedPtr<T>::value)
113+
{
114+
return *GetBase();
115+
}
116+
94117
template <typename U>
95118
constexpr bool operator==(const NotNull<U>& other) const& {
96119
return GetBase() == other.GetBase();

0 commit comments

Comments
 (0)