RustCell.h (1358B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 /* an object with the same layout as a Rust std::cell::Cell<T> */ 8 9 #ifndef mozilla_RustCell_h 10 #define mozilla_RustCell_h 11 12 namespace mozilla { 13 14 /** 15 * Object with the same layout as std::cell::Cell<T>. 16 * 17 * ServoBindings.toml defines a mapping so that generated bindings use a 18 * real std::cell::Cell<T>. 19 * 20 * Note that while the layout matches, this doesn't have the same ABI as a 21 * std::cell::Cell<T>, so values of this type can't be passed over FFI 22 * functions. 23 */ 24 template <typename T> 25 class RustCell { 26 public: 27 RustCell() : mValue() {} 28 explicit RustCell(T aValue) : mValue(aValue) {} 29 30 T Get() const { return mValue; } 31 void Set(T aValue) { mValue = aValue; } 32 33 // That this API doesn't mirror the API of rust's Cell because all values in 34 // C++ effectively act like they're wrapped in Cell<...> already, so this type 35 // only exists for FFI purposes. 36 T* AsPtr() { return &mValue; } 37 const T* AsPtr() const { return &mValue; } 38 39 private: 40 T mValue; 41 }; 42 43 } // namespace mozilla 44 45 #endif // mozilla_RustCell_h