mnn/
session.rs

1use crate::prelude::*;
2
3/// A session is a context in which a computation graph is executed.
4///
5/// Inference unit. multiple sessions could share one net/interpreter.
6#[derive(Debug)]
7pub struct Session {
8    /// Pointer to the underlying MNN session.
9    pub(crate) inner: *mut mnn_sys::Session,
10    /// Pointer to the underlying MNN interpreter
11    /// # Safety Note
12    /// Since the interpreter is actually not owned by session but it is a shared resource we can
13    /// reasonably assume that the interpreter will outlive the session. (This is not a compile
14    /// time gurantee yet)
15    /// TODO: Add a proper lifetime bound to ensure the interpreter outlives the session.
16    pub(crate) net: *mut mnn_sys::Interpreter,
17    /// Internal session configurations.
18    pub(crate) __session_internals: crate::SessionInternals,
19    /// Marker to ensure the struct is not Send or Sync.
20    pub(crate) __marker: PhantomData<()>,
21}
22
23/// Enum representing the internal configurations of a session.
24#[derive(Debug)]
25pub enum SessionInternals {
26    /// Single session configuration.
27    Single(crate::ScheduleConfig),
28    /// Multiple session configurations.
29    MultiSession(crate::ScheduleConfigs),
30}
31
32impl Session {
33    /// Calls the destroy function on the underlying MNN session.
34    pub fn destroy(&mut self) {
35        unsafe {
36            mnn_sys::Interpreter_releaseSession(self.net, self.inner);
37        }
38        // unsafe { mnn_sys::Session_destroy(self.inner) }
39    }
40}
41
42impl Drop for Session {
43    /// Custom drop implementation to ensure the underlying MNN session is properly destroyed.
44    fn drop(&mut self) {
45        self.destroy();
46    }
47}