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        /// - `file_path`: The path to the ExecuTorch program file to load.
50        /// - `data_map_path`: The path to a .ptd file, or an empty string if no data map is needed.
51        /// - `load_mode`: The loading mode to use.
52        /// - `event_tracer`: An EventTracer used for tracking and logging events, or null if not needed.
53        #[namespace = "executorch_rs"]
54        fn Module_new(
55            file_path: &str,
56            data_map_path: &str,
57            load_mode: ModuleLoadMode,
58            event_tracer: UniquePtr<EventTracer>,
59        ) -> UniquePtr<Module>;
60
61        /// Load the program if needed.
62        ///
63        /// # Arguments
64        /// - `verification`: The type of verification to do before returning success.
65        ///
66        /// # Returns
67        /// An Error to indicate success or failure of the loading process.
68        #[namespace = "executorch_rs"]
69        fn Module_load(self_: Pin<&mut Module>, verification: ProgramVerification) -> Error;
70
71        /// Get the number of methods available in the loaded program.
72        ///
73        /// # Safety
74        ///
75        /// The `method_num_out` is valid only if the function returns `Error::Ok`.
76        #[namespace = "executorch_rs"]
77        unsafe fn Module_num_methods(self_: Pin<&mut Module>, method_num_out: &mut usize) -> Error;
78
79        /// Get a list of method names available in the loaded program.
80        ///
81        /// Loads the program and method if needed.
82        ///
83        /// # Arguments
84        /// - `method_names_out`: A mutable reference to a vector that will be filled with the method names.
85        ///
86        /// # Returns
87        /// A error indicating whether the method names retrieval was successful or not.
88        ///
89        /// # Safety
90        /// The `method_names_out` vector must be valid for the lifetime of the function.
91        /// The `method_names_out` vector can be used only if the function returns `Error::Ok`.
92        #[namespace = "executorch_rs"]
93        unsafe fn Module_method_names(
94            self_: Pin<&mut Module>,
95            method_names_out: &mut Vec<String>,
96        ) -> Error;
97
98        /// Load a specific method from the program and set up memory management if
99        /// needed.
100        ///
101        /// The loaded method is cached to reuse the next time it's executed.
102        ///
103        /// # Arguments
104        /// - `method_name`: The name of the method to load.
105        ///
106        /// # Returns
107        /// An Error to indicate success or failure.
108        #[namespace = "executorch_rs"]
109        unsafe fn Module_load_method(
110            self_: Pin<&mut Module>,
111            method_name: &str,
112            planned_memory: *mut HierarchicalAllocator,
113            event_tracer: *mut EventTracer,
114        ) -> Error;
115
116        /// Checks if a specific method is loaded.
117        ///
118        /// # Arguments
119        /// - `method_name`: The name of the method to check.
120        ///
121        /// # Returns
122        /// `true` if the method specified by `method_name` is loaded, `false` otherwise.
123        #[namespace = "executorch_rs"]
124        fn Module_is_method_loaded(self_: &Module, method_name: &str) -> bool;
125
126        /// Get a method metadata struct by method name.
127        ///
128        /// Loads the program and method if needed.
129        ///
130        /// # Arguments
131        /// - `method_name`: The name of the method to get the metadata for.
132        /// - `method_meta_out`: A mutable reference to a `MethodMeta` struct that will be filled with the metadata.
133        ///
134        /// # Returns
135        /// A error indicating whether the metadata retrieval was successful or not.
136        ///
137        /// # Safety
138        /// The `method_meta_out` struct must be valid for the lifetime of the function.
139        /// The `method_meta_out` struct can be used only if the function returns `Error::Ok`.
140        #[namespace = "executorch_rs"]
141        unsafe fn Module_method_meta(
142            self_: Pin<&mut Module>,
143            method_name: &str,
144            method_meta_out: *mut MethodMeta,
145        ) -> Error;
146
147        /// Execute a specific method with the given input values and retrieve the
148        /// output values. Loads the program and method before executing if needed.
149        ///
150        /// # Arguments
151        /// - `method_name`: The name of the method to execute.
152        /// - `inputs`: A vector of input values to be passed to the method.
153        /// - `outputs`: A mutable reference to a vector that will be filled with the output values from the method.
154        ///
155        /// # Returns
156        /// A error indicating whether the execution was successful or not.
157        ///
158        /// # Safety
159        /// The `outputs` vector must be valid for the lifetime of the function.
160        /// The `outputs` vector can be used only if the function returns `Error::Ok`.
161        #[namespace = "executorch_rs"]
162        unsafe fn Module_execute(
163            self_: Pin<&mut Module>,
164            method_name: &str,
165            inputs: ArrayRefEValue,
166            outputs: *mut VecEValue,
167        ) -> Error;
168    }
169}
170
171unsafe impl ExternType for crate::HierarchicalAllocator {
172    type Id = type_id!("HierarchicalAllocator");
173    type Kind = cxx::kind::Trivial;
174}
175
176unsafe impl ExternType for crate::ModuleLoadMode {
177    type Id = type_id!("ModuleLoadMode");
178    type Kind = cxx::kind::Trivial;
179}