executorch_sys/cxx_bridge/
module.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        /// Cpp executorch error type.
13        type Error = crate::Error;
14
15        /// Types of validation that a `Program` can do before parsing the data.
16        type ProgramVerification = crate::ProgramVerification;
17
18        /// Describes a method in an ExecuTorch program.
19        ///
20        /// The program used to create a MethodMeta object must outlive the MethodMeta.
21        /// It is separate from Method so that this information can be accessed without
22        /// paying the initialization cost of loading the full Method.
23        type MethodMeta = crate::MethodMeta;
24
25        /// A facade class for loading programs and executing methods within them.
26        #[namespace = "executorch::extension"]
27        type Module;
28
29        /// Enum to define loading behavior.
30        type ModuleLoadMode = crate::ModuleLoadMode;
31
32        /// A specification of `ArrayRef<EValue>`.
33        type ArrayRefEValue = crate::ArrayRefEValue;
34
35        /// A vector of `EValue`.
36        type VecEValue = crate::VecEValue;
37
38        /// EventTracer is a class that users can inherit and implement to log/serialize/stream etc.
39        #[namespace = "executorch::runtime"]
40        type EventTracer;
41
42        /// Redefinition of the [`HierarchicalAllocator`](crate::HierarchicalAllocator).
43        type HierarchicalAllocator = crate::HierarchicalAllocator;
44
45        /// Constructs an instance by loading a program from a file with specified
46        /// memory locking behavior.
47        ///
48        /// # Arguments
49        ///
50        /// - `file_path`: The path to the ExecuTorch program file to load.
51        /// - `data_files`: The path to one or more .ptd file/s.
52        /// - `load_mode`: The loading mode to use.
53        /// - `event_tracer`: An EventTracer used for tracking and logging events, or null if not needed.
54        #[namespace = "executorch_rs"]
55        fn Module_new(
56            file_path: &CxxString,
57            data_files: &[&str],
58            load_mode: ModuleLoadMode,
59            event_tracer: UniquePtr<EventTracer>,
60        ) -> UniquePtr<Module>;
61
62        /// Load the program if needed.
63        ///
64        /// # Arguments
65        ///
66        /// - `verification`: The type of verification to do before returning success.
67        ///
68        /// # Returns
69        ///
70        /// An Error to indicate success or failure of the loading process.
71        #[namespace = "executorch_rs"]
72        fn Module_load(self_: Pin<&mut Module>, verification: ProgramVerification) -> Error;
73
74        /// Checks if the program is loaded.
75        #[namespace = "executorch_rs"]
76        fn Module_is_loaded(self_: &Module) -> bool;
77
78        /// Get the number of methods available in the loaded program.
79        ///
80        /// # Safety
81        ///
82        /// The `method_num_out` is valid only if the function returns `Error::Ok`.
83        #[namespace = "executorch_rs"]
84        unsafe fn Module_num_methods(self_: Pin<&mut Module>, method_num_out: *mut usize) -> Error;
85
86        /// Get a list of method names available in the loaded program.
87        ///
88        /// Loads the program and method if needed.
89        ///
90        /// # Arguments
91        ///
92        /// - `method_names_out`: A pointer to a (non initialized) vector that will be created and filled with
93        ///    the method names.
94        ///
95        /// # Returns
96        ///
97        /// A error indicating whether the method names retrieval was successful or not.
98        ///
99        /// # Safety
100        ///
101        /// The `method_names_out` vector can be used only if the function returns `Error::Ok`.
102        #[namespace = "executorch_rs"]
103        unsafe fn Module_method_names(
104            self_: Pin<&mut Module>,
105            method_names_out: *mut Vec<String>,
106        ) -> Error;
107
108        /// Load a specific method from the program and set up memory management if
109        /// needed.
110        ///
111        /// The loaded method is cached to reuse the next time it's executed.
112        ///
113        /// # Arguments
114        ///
115        /// - `method_name`: The name of the method to load.
116        ///
117        /// # Returns
118        ///
119        /// An Error to indicate success or failure.
120        #[namespace = "executorch_rs"]
121        unsafe fn Module_load_method(
122            self_: Pin<&mut Module>,
123            method_name: &CxxString,
124            planned_memory: *mut HierarchicalAllocator,
125            event_tracer: *mut EventTracer,
126        ) -> Error;
127
128        /// Unload a specific method from the program.
129        ///
130        /// # Arguments
131        /// - `method_name`: The name of the method to unload.
132        ///
133        /// # Returns
134        ///
135        /// True if the method is unloaded, false if no-op.
136        #[namespace = "executorch_rs"]
137        unsafe fn Module_unload_method(self_: Pin<&mut Module>, method_name: &CxxString) -> bool;
138
139        /// Checks if a specific method is loaded.
140        ///
141        /// # Arguments
142        ///
143        /// - `method_name`: The name of the method to check.
144        ///
145        /// # Returns
146        ///
147        /// `true` if the method specified by `method_name` is loaded, `false` otherwise.
148        #[namespace = "executorch_rs"]
149        fn Module_is_method_loaded(self_: &Module, method_name: &CxxString) -> bool;
150
151        /// Get a method metadata struct by method name.
152        ///
153        /// Loads the program if needed.
154        ///
155        /// # Arguments
156        ///
157        /// - `method_name`: The name of the method to get the metadata for.
158        /// - `method_meta_out`: A mutable reference to a `MethodMeta` struct that will be filled with the metadata.
159        ///
160        /// # Returns
161        ///
162        /// A error indicating whether the metadata retrieval was successful or not.
163        ///
164        /// # Safety
165        ///
166        /// The `method_meta_out` struct must be valid for the lifetime of the function.
167        /// The `method_meta_out` struct can be used only if the function returns `Error::Ok`.
168        #[namespace = "executorch_rs"]
169        unsafe fn Module_method_meta(
170            self_: Pin<&mut Module>,
171            method_name: &CxxString,
172            method_meta_out: *mut MethodMeta,
173        ) -> Error;
174
175        /// Execute a specific method with the given input values and retrieve the
176        /// output values. Loads the program and method before executing if needed.
177        ///
178        /// # Arguments
179        ///
180        /// - `method_name`: The name of the method to execute.
181        /// - `inputs`: A vector of input values to be passed to the method.
182        /// - `outputs`: A mutable reference to a vector that will be filled with the output values from the method.
183        ///
184        /// # Returns
185        ///
186        /// A error indicating whether the execution was successful or not.
187        ///
188        /// # Safety
189        ///
190        /// The `outputs` vector must be valid for the lifetime of the function.
191        /// The `outputs` vector can be used only if the function returns `Error::Ok`.
192        #[namespace = "executorch_rs"]
193        unsafe fn Module_execute(
194            self_: Pin<&mut Module>,
195            method_name: &CxxString,
196            inputs: ArrayRefEValue,
197            outputs: *mut VecEValue,
198        ) -> Error;
199    }
200}
201
202unsafe impl ExternType for crate::HierarchicalAllocator {
203    type Id = type_id!("HierarchicalAllocator");
204    type Kind = cxx::kind::Trivial;
205}
206
207unsafe impl ExternType for crate::ModuleLoadMode {
208    type Id = type_id!("ModuleLoadMode");
209    type Kind = cxx::kind::Trivial;
210}