Commit f0970f96 authored by Kenton Varda's avatar Kenton Varda

Make kj::defer work correctly when the input is a lambda with non-copyable captures.

parent dbac82e8
...@@ -1270,7 +1270,7 @@ namespace _ { // private ...@@ -1270,7 +1270,7 @@ namespace _ { // private
template <typename Func> template <typename Func>
class Deferred { class Deferred {
public: public:
inline Deferred(Func func): func(func), canceled(false) {} inline Deferred(Func&& func): func(kj::fwd<Func>(func)), canceled(false) {}
inline ~Deferred() noexcept(false) { if (!canceled) func(); } inline ~Deferred() noexcept(false) { if (!canceled) func(); }
KJ_DISALLOW_COPY(Deferred); KJ_DISALLOW_COPY(Deferred);
...@@ -1286,7 +1286,7 @@ private: ...@@ -1286,7 +1286,7 @@ private:
} // namespace _ (private) } // namespace _ (private)
template <typename Func> template <typename Func>
_::Deferred<Decay<Func>> defer(Func&& func) { _::Deferred<Func> defer(Func&& func) {
// Returns an object which will invoke the given functor in its destructor. The object is not // Returns an object which will invoke the given functor in its destructor. The object is not
// copyable but is movable with the semantics you'd expect. Since the return type is private, // copyable but is movable with the semantics you'd expect. Since the return type is private,
// you need to assign to an `auto` variable. // you need to assign to an `auto` variable.
...@@ -1294,7 +1294,7 @@ _::Deferred<Decay<Func>> defer(Func&& func) { ...@@ -1294,7 +1294,7 @@ _::Deferred<Decay<Func>> defer(Func&& func) {
// The KJ_DEFER macro provides slightly more convenient syntax for the common case where you // The KJ_DEFER macro provides slightly more convenient syntax for the common case where you
// want some code to run at function exit. // want some code to run at function exit.
return _::Deferred<Decay<Func>>(kj::fwd<Func>(func)); return _::Deferred<Func>(kj::fwd<Func>(func));
} }
#define KJ_DEFER(code) auto KJ_UNIQUE_NAME(_kjDefer) = ::kj::defer([&](){code;}) #define KJ_DEFER(code) auto KJ_UNIQUE_NAME(_kjDefer) = ::kj::defer([&](){code;})
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment