Skip to main content

executorch_sys/cxx_bridge/
core.rs

1// Clippy doesnt detect the 'Safety' comments in the cxx bridge.
2#![allow(clippy::missing_safety_doc)]
3
4use cxx::{type_id, ExternType};
5
6#[cxx::bridge]
7pub(crate) mod ffi {
8
9    unsafe extern "C++" {
10        include!("executorch-sys/cpp/executorch_rs/cxx_bridge.hpp");
11
12        /// Redefinition of the [`MemoryAllocator`](crate::MemoryAllocator).
13        type MemoryAllocator = crate::MemoryAllocator;
14
15        /// Convert a `MemoryAllocator` into a `UniquePtr<MemoryAllocator>`.
16        ///
17        /// The function moves the `MemoryAllocator` into a `UniquePtr`, and calls the destructor of the original
18        /// `MemoryAllocator`. It does not free the object itself though.
19        #[namespace = "executorch_rs"]
20        fn BufferMemoryAllocator_into_memory_allocator_unique_ptr(
21            self_: Pin<&mut MemoryAllocator>,
22        ) -> UniquePtr<MemoryAllocator>;
23
24        /// Dynamically allocates memory using malloc() and frees all pointers at
25        /// destruction time.
26        ///
27        /// For systems with malloc(), this can be easier than using a fixed-sized
28        /// MemoryAllocator.
29        #[namespace = "executorch::extension"]
30        type MallocMemoryAllocator;
31
32        /// Construct a new Malloc memory allocator.
33        #[namespace = "executorch_rs"]
34        fn MallocMemoryAllocator_new() -> UniquePtr<MallocMemoryAllocator>;
35
36        /// Get a pointer to the base class `MemoryAllocator`.
37        ///
38        /// Safety: The caller must ensure that the pointer is valid for the lifetime of the `MemoryAllocator`.
39        #[namespace = "executorch_rs"]
40        unsafe fn MallocMemoryAllocator_as_memory_allocator(
41            self_: Pin<&mut MallocMemoryAllocator>,
42        ) -> *mut MemoryAllocator;
43
44        /// Convert a `UniquePtr<MallocMemoryAllocator>` into a `UniquePtr<MemoryAllocator>`.
45        #[namespace = "executorch_rs"]
46        fn MallocMemoryAllocator_into_memory_allocator_unique_ptr(
47            self_: UniquePtr<MallocMemoryAllocator>,
48        ) -> UniquePtr<MemoryAllocator>;
49
50    }
51
52    impl UniquePtr<MemoryAllocator> {}
53}
54
55unsafe impl ExternType for crate::ScalarType {
56    type Id = type_id!("ScalarType");
57    type Kind = cxx::kind::Trivial;
58}
59
60unsafe impl ExternType for crate::TensorShapeDynamism {
61    type Id = type_id!("TensorShapeDynamism");
62    type Kind = cxx::kind::Trivial;
63}
64
65unsafe impl ExternType for crate::Error {
66    type Id = type_id!("Error");
67    type Kind = cxx::kind::Trivial;
68}
69
70unsafe impl ExternType for crate::MethodMeta {
71    type Id = type_id!("MethodMeta");
72    type Kind = cxx::kind::Trivial;
73}
74
75unsafe impl ExternType for crate::ArrayRefEValue {
76    type Id = type_id!("ArrayRefEValue");
77    type Kind = cxx::kind::Trivial;
78}
79
80unsafe impl ExternType for crate::VecEValue {
81    type Id = type_id!("VecEValue");
82    type Kind = cxx::kind::Trivial;
83}
84
85unsafe impl ExternType for crate::ProgramVerification {
86    type Id = type_id!("ProgramVerification");
87    type Kind = cxx::kind::Trivial;
88}
89
90unsafe impl ExternType for crate::MemoryAllocator {
91    type Id = type_id!("MemoryAllocator");
92    type Kind = cxx::kind::Opaque;
93}