Skip to content

Commit e873042

Browse files
committed
Replace BoxFn with Fn when it's not an lvalue
1 parent 12b418e commit e873042

File tree

8 files changed

+248
-235
lines changed

8 files changed

+248
-235
lines changed

subdoc/lib/database.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "subdoc/lib/unique_symbol.h"
2626
#include "subdoc/llvm.h"
2727
#include "subspace/choice/choice.h"
28+
#include "subspace/fn/fn.h"
2829
#include "subspace/option/option.h"
2930
#include "subspace/prelude.h"
3031

@@ -145,7 +146,7 @@ struct FunctionElement : public CommentElement {
145146
return sus::none();
146147
}
147148

148-
void for_each_comment(sus::fn::BoxFnMut<void(Comment&)>& fn) { fn(comment); }
149+
void for_each_comment(sus::fn::FnMut<void(Comment&)> fn) { fn(comment); }
149150
};
150151

151152
struct FieldElement : public CommentElement {
@@ -186,7 +187,7 @@ struct FieldElement : public CommentElement {
186187
return sus::none();
187188
}
188189

189-
void for_each_comment(sus::fn::BoxFnMut<void(Comment&)>& fn) { fn(comment); }
190+
void for_each_comment(sus::fn::FnMut<void(Comment&)> fn) { fn(comment); }
190191
};
191192

192193
struct NamespaceId {
@@ -323,7 +324,7 @@ struct RecordElement : public TypeElement {
323324
return out;
324325
}
325326

326-
void for_each_comment(sus::fn::BoxFnMut<void(Comment&)>& fn) {
327+
void for_each_comment(sus::fn::FnMut<void(Comment&)> fn) {
327328
fn(comment);
328329
for (auto& [k, e] : records) e.for_each_comment(fn);
329330
for (auto& [k, e] : fields) e.for_each_comment(fn);
@@ -426,7 +427,7 @@ struct NamespaceElement : public CommentElement {
426427
return out;
427428
}
428429

429-
void for_each_comment(sus::fn::BoxFnMut<void(Comment&)>& fn) {
430+
void for_each_comment(sus::fn::FnMut<void(Comment&)> fn) {
430431
fn(comment);
431432
for (auto& [k, e] : namespaces) e.for_each_comment(fn);
432433
for (auto& [k, e] : records) e.for_each_comment(fn);

subspace/fn/box_fn_defn.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ class [[sus_trivial_abi]] BoxFnOnce<R(CallArgs...)> {
186186
BoxFnOnce& operator=(const BoxFnOnce&) noexcept = delete;
187187

188188
/// Runs and consumes the closure.
189-
inline R operator()(CallArgs&&... args) && noexcept;
189+
inline R operator()(CallArgs... args) && noexcept;
190190

191191
/// `sus::construct::From` trait implementation.
192192
//
@@ -339,8 +339,8 @@ class [[sus_trivial_abi]] BoxFnMut<R(CallArgs...)>
339339
BoxFnMut& operator=(const BoxFnMut&) noexcept = delete;
340340

341341
/// Runs the closure.
342-
inline R operator()(CallArgs&&... args) & noexcept;
343-
inline R operator()(CallArgs&&... args) && noexcept {
342+
inline R operator()(CallArgs... args) & noexcept;
343+
inline R operator()(CallArgs... args) && noexcept {
344344
return static_cast<BoxFnOnce<R(CallArgs...)>&&>(*this)(
345345
forward<CallArgs>(args)...);
346346
}
@@ -471,8 +471,8 @@ class [[sus_trivial_abi]] BoxFn<R(CallArgs...)> final
471471
BoxFn& operator=(const BoxFn&) noexcept = delete;
472472

473473
/// Runs the closure.
474-
inline R operator()(CallArgs&&... args) const& noexcept;
475-
inline R operator()(CallArgs&&... args) && noexcept {
474+
inline R operator()(CallArgs... args) const& noexcept;
475+
inline R operator()(CallArgs... args) && noexcept {
476476
return static_cast<BoxFnOnce<R(CallArgs...)>&&>(*this)(
477477
forward<CallArgs>(args)...);
478478
}

subspace/fn/box_fn_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ BoxFnOnce<R(CallArgs...)>& BoxFnOnce<R(CallArgs...)>::operator=(BoxFnOnce&& o) n
130130
}
131131

132132
template <class R, class... CallArgs>
133-
R BoxFnOnce<R(CallArgs...)>::operator()(CallArgs&&... args) && noexcept {
133+
R BoxFnOnce<R(CallArgs...)>::operator()(CallArgs... args) && noexcept {
134134
switch (type_) {
135135
case __private::BoxFnPointer: {
136136
::sus::check(fn_ptr_); // Catch use-after-move.
@@ -163,7 +163,7 @@ R BoxFnOnce<R(CallArgs...)>::operator()(CallArgs&&... args) && noexcept {
163163
}
164164

165165
template <class R, class... CallArgs>
166-
R BoxFnMut<R(CallArgs...)>::operator()(CallArgs&&... args) & noexcept {
166+
R BoxFnMut<R(CallArgs...)>::operator()(CallArgs... args) & noexcept {
167167
using Super = BoxFnOnce<R(CallArgs...)>;
168168
switch (Super::type_) {
169169
case __private::BoxFnPointer:
@@ -183,7 +183,7 @@ R BoxFnMut<R(CallArgs...)>::operator()(CallArgs&&... args) & noexcept {
183183
}
184184

185185
template <class R, class... CallArgs>
186-
R BoxFn<R(CallArgs...)>::operator()(CallArgs&&... args) const& noexcept {
186+
R BoxFn<R(CallArgs...)>::operator()(CallArgs... args) const& noexcept {
187187
using Super = BoxFnOnce<R(CallArgs...)>;
188188
switch (Super::type_) {
189189
case __private::BoxFnPointer:

subspace/fn/box_fn_unittest.cc

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,25 @@ concept can_run = requires (
190190
};
191191
// clang-format on
192192

193-
// Receiving by-value means it must be passed as a mutable move. This means no
194-
// implicit copy will happen.
193+
// Int is copyable, so references are copied when passed.
195194
static_assert(can_run<void, int, int>);
196195
static_assert(can_run<void, int, int&&>);
197-
static_assert(!can_run<void, int, int&>);
198-
static_assert(!can_run<void, int, const int>);
199-
static_assert(!can_run<void, int, const int&>);
200-
static_assert(!can_run<void, int, const int&&>);
196+
static_assert(can_run<void, int, int&>);
197+
static_assert(can_run<void, int, const int>);
198+
static_assert(can_run<void, int, const int&>);
199+
static_assert(can_run<void, int, const int&&>);
200+
// But for a move-only type, it can only be passed along as a reference or an
201+
// rvalue.
202+
static_assert(can_run<void, MoveOnly, MoveOnly>);
203+
static_assert(can_run<void, MoveOnly, MoveOnly&&>);
204+
static_assert(!can_run<void, MoveOnly, MoveOnly&>);
205+
static_assert(!can_run<void, MoveOnly, const MoveOnly>);
206+
static_assert(!can_run<void, MoveOnly, const MoveOnly&>);
207+
static_assert(!can_run<void, MoveOnly, const MoveOnly&&>);
208+
static_assert(can_run<void, const MoveOnly&, MoveOnly&>);
209+
static_assert(can_run<void, const MoveOnly&, const MoveOnly&>);
210+
static_assert(can_run<void, MoveOnly&, MoveOnly&>);
211+
static_assert(!can_run<void, MoveOnly&, const MoveOnly&>);
201212

202213
// Receiving a mutable reference means it must be passed as a mutable reference.
203214
static_assert(can_run<void, int&, int&>);

0 commit comments

Comments
 (0)