1 #pragma once 2 3 #include "base/callback.h" 4 #include "base/threading/sequenced_task_runner_handle.h" 5 #include "rust/cxx.h" 6 7 namespace bluetooth { 8 namespace shim { 9 namespace rust { 10 11 template <class TArg> 12 class TrampolineCallback { 13 public: TrampolineCallback(base::Callback<void (TArg)> callback)14 TrampolineCallback(base::Callback<void(TArg)> callback) : callback_(callback) {} 15 Run(TArg value)16 void Run(TArg value) const { 17 callback_.Run(value); 18 } 19 20 private: 21 base::Callback<void(TArg)> callback_; 22 }; 23 24 template <class TArg> 25 class TrampolineOnceCallback { 26 public: TrampolineOnceCallback(base::OnceCallback<void (TArg)> callback)27 TrampolineOnceCallback(base::OnceCallback<void(TArg)> callback) 28 : callback_(new base::OnceCallback<void(TArg)>(std::move(callback))) {} ~TrampolineOnceCallback()29 ~TrampolineOnceCallback() { 30 if (callback_ != nullptr) { 31 delete callback_; 32 callback_ = nullptr; 33 } 34 } 35 Run(TArg value)36 void Run(TArg value) const { 37 std::move(*callback_).Run(value); 38 delete callback_; 39 ((TrampolineOnceCallback<TArg>*)this)->callback_ = nullptr; 40 } 41 42 private: 43 base::OnceCallback<void(TArg)>* callback_; 44 }; 45 46 class OnceClosure { 47 public: OnceClosure(base::OnceClosure closure)48 OnceClosure(base::OnceClosure closure) : closure_(new base::OnceClosure(std::move(closure))) {} ~OnceClosure()49 ~OnceClosure() { 50 if (closure_ != nullptr) { 51 delete closure_; 52 closure_ = nullptr; 53 } 54 } 55 Run()56 void Run() const { 57 std::move(*closure_).Run(); 58 delete closure_; 59 ((OnceClosure*)this)->closure_ = nullptr; 60 } 61 62 private: 63 base::OnceClosure* closure_; 64 }; 65 66 using u8SliceCallback = TrampolineCallback<::rust::Slice<const uint8_t>>; 67 using u8SliceOnceCallback = TrampolineOnceCallback<::rust::Slice<const uint8_t>>; 68 69 } // namespace rust 70 } // namespace shim 71 } // namespace bluetooth 72 73 #include "src/bridge.rs.h" 74