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