Skip to main content

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<'i> {
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    pub(crate) net: *mut mnn_sys::Interpreter,
16    /// Internal session configurations.
17    pub(crate) __session_internals: crate::SessionInternals,
18    /// Marker to ensure the struct is not Send or Sync.
19    pub(crate) __marker: PhantomData<&'i ()>,
20}
21
22/// Enum representing the internal configurations of a session.
23#[derive(Debug)]
24pub enum SessionInternals {
25    /// Single session configuration.
26    Single(crate::ScheduleConfig),
27    /// Multiple session configurations.
28    MultiSession(crate::ScheduleConfigs),
29}
30
31impl<'i> Session<'i> {
32    /// Calls the destroy function on the underlying MNN session.
33    pub fn destroy(&mut self) {
34        unsafe {
35            mnn_sys::Interpreter_releaseSession(self.net, self.inner);
36        }
37        // unsafe { mnn_sys::Session_destroy(self.inner) }
38    }
39}
40
41impl<'i> Drop for Session<'i> {
42    /// Custom drop implementation to ensure the underlying MNN session is properly destroyed.
43    fn drop(&mut self) {
44        self.destroy();
45    }
46}