mod.rs 4.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. /// C++ bindings for base.
  5. ///
  6. /// This mod contains all the FFI bindings for C++ types in the base
  7. /// namespace. It should declare shared types, and the 'extern "C++"'
  8. /// section indicating C++ functions that are callable from Rust.
  9. ///
  10. /// Other [cxx::bridge] mods throughout the //base codebase may exist
  11. /// for exporting Rust functions to C++.
  12. ///
  13. /// C++ functions which exist only for the benefit of calls from
  14. /// Rust->C++ should live within the base::rs_glue C++ namespace.
  15. #[cxx::bridge(namespace=base::rs_glue)]
  16. pub(crate) mod ffi {
  17. unsafe extern "C++" {
  18. include!("base/rs_glue/values_glue.h");
  19. /// C++ `base::Value`
  20. #[namespace=base]
  21. type Value;
  22. // Bindings to existing base::Value methods which happen to
  23. // line up with our needs precisely.
  24. #[cxx_name = "Append"]
  25. fn ValueAppendBool(self: Pin<&mut Value>, val: bool);
  26. #[cxx_name = "Append"]
  27. fn ValueAppendInteger(self: Pin<&mut Value>, val: i32);
  28. #[cxx_name = "Append"]
  29. fn ValueAppendDouble(self: Pin<&mut Value>, val: f64);
  30. // Free functions in C++ for cases where existing base::Value
  31. // APIs don't quite match our needs.
  32. // Set a key on a base::Value of type DICTIONARY to the given
  33. // value.
  34. fn ValueSetNoneKey(v: Pin<&mut Value>, key: &str);
  35. fn ValueSetBoolKey(v: Pin<&mut Value>, key: &str, val: bool);
  36. fn ValueSetIntegerKey(v: Pin<&mut Value>, key: &str, val: i32);
  37. fn ValueSetDoubleKey(v: Pin<&mut Value>, key: &str, val: f64);
  38. fn ValueSetStringKey(v: Pin<&mut Value>, key: &str, value: &str);
  39. // Returns a new child base::Value, of type DICTIONARY.
  40. fn ValueSetDictKey<'a>(v: Pin<&'a mut Value>, key: &str) -> Pin<&'a mut Value>;
  41. // Returns a new child base::Value, of type LIST.
  42. fn ValueSetListKey<'a>(v: Pin<&'a mut Value>, key: &str) -> Pin<&'a mut Value>;
  43. // Appends to a base::Value of type LIST.
  44. fn ValueAppendNone(v: Pin<&mut Value>);
  45. fn ValueAppendString(v: Pin<&mut Value>, value: &str);
  46. // Returns a new child base::Value, of type DICTIONARY.
  47. fn ValueAppendDict(v: Pin<&mut Value>) -> Pin<&mut Value>;
  48. // Returns a new child base::Value, of type LIST.
  49. fn ValueAppendList(v: Pin<&mut Value>) -> Pin<&mut Value>;
  50. fn ValueReserveSize(v: Pin<&mut Value>, len: usize);
  51. /// Represents a slot (on stack or heap) into which a new
  52. /// `base::Value` can be constructed. Only C++ code can construct
  53. /// such a slot.
  54. ///
  55. /// This type exists because we want to expose many functions
  56. /// which *construct* a `base::Value` which needs to live somewhere.
  57. /// For that reason, we can't simply extract a reference
  58. /// to the underlying base::Value because it doesn't yet exist.
  59. /// Future interop tools should support constructing a base::Value
  60. /// in Rust and moving it (by value).
  61. /// TODO(crbug.com/1272780): Use bindgen-like tools to generate
  62. /// direct bindings to C++ constructors.
  63. type ValueSlot;
  64. // Construct a base::Value of the given type, with the given value.
  65. fn ConstructNoneValue(v: Pin<&mut ValueSlot>);
  66. fn ConstructBoolValue(v: Pin<&mut ValueSlot>, val: bool);
  67. fn ConstructIntegerValue(v: Pin<&mut ValueSlot>, val: i32);
  68. fn ConstructDoubleValue(v: Pin<&mut ValueSlot>, val: f64);
  69. fn ConstructStringValue(v: Pin<&mut ValueSlot>, value: &str);
  70. // Returns a reference to the base::Value within the
  71. // `absl::optional<base::Value>` (which is of type DICTIONARY)
  72. // so that it can be populated.
  73. fn ConstructDictValue(v: Pin<&mut ValueSlot>) -> Pin<&mut Value>;
  74. // Returns a reference to the base::Value within the
  75. // `absl::optional<base::Value>` (which is of type LIST)
  76. // so that it can be populated.
  77. fn ConstructListValue(v: Pin<&mut ValueSlot>) -> Pin<&mut Value>;
  78. fn DumpValueSlot(v: &ValueSlot) -> String;
  79. // Defined for Rust tests to crate a ValueSlot, because it requires C++ to do the creation
  80. // at this time.
  81. fn NewValueSlotForTesting() -> UniquePtr<ValueSlot>;
  82. }
  83. }