Skip to main content

ncnn_rust/net/
blob.rs

1//! Endpoint selection for extractor input and output.
2
3use std::rc::Rc;
4
5#[derive(Clone, Copy, PartialEq, Eq, Debug)]
6pub(crate) enum BlobKind {
7    Input,
8    Output,
9}
10/// A graph-specific blob index resolved once before repeated inference.
11/// The token remains invalid after graph replacement, including address reuse.
12#[derive(Clone, Debug)]
13pub struct BlobId {
14    graph: Rc<()>,
15    index: i32,
16    kind: BlobKind,
17}
18impl BlobId {
19    pub(crate) fn new(graph: Rc<()>, index: i32, kind: BlobKind) -> Self {
20        Self { graph, index, kind }
21    }
22    pub(crate) fn kind(&self) -> BlobKind {
23        self.kind
24    }
25    pub(crate) fn belongs_to(&self, graph: &Rc<()>) -> bool {
26        Rc::ptr_eq(graph, &self.graph)
27    }
28    /// Returns the endpoint index for checked indexed APIs on the originating graph.
29    /// Unlike this token, an integer does not retain graph identity across reloads.
30    pub fn index(&self) -> i32 {
31        self.index
32    }
33}
34
35/// Selects a graph endpoint by name, exposed index or resolved token.
36#[derive(Clone, Copy)]
37#[non_exhaustive]
38pub enum Endpoint<'a> {
39    /// Looks the endpoint up by blob name.
40    Name(&'a str),
41    /// Addresses the native blob index directly.
42    Index(i32),
43    /// Reuses a token bound to the loaded graph.
44    Blob(&'a BlobId),
45}
46
47impl<'a> From<&'a str> for Endpoint<'a> {
48    fn from(name: &'a str) -> Self {
49        Self::Name(name)
50    }
51}
52
53impl From<i32> for Endpoint<'_> {
54    fn from(index: i32) -> Self {
55        Self::Index(index)
56    }
57}
58
59impl<'a> From<&'a String> for Endpoint<'a> {
60    fn from(name: &'a String) -> Self {
61        Self::Name(name)
62    }
63}
64
65impl<'a> From<&'a BlobId> for Endpoint<'a> {
66    fn from(blob: &'a BlobId) -> Self {
67        Self::Blob(blob)
68    }
69}