Skip to main content

nativeview_hybrid_spec/model/
batch_model.rs

1//! Shared `#[batchable]` command-buffer lowering model (`DirectBatchSpec`) consumed across all
2//! target language emitters so every language agrees on opcodes, unified error channels, reply
3//! flags, and wire encodings.
4
5use crate::model::direct_model::{DirectErrKind, DirectReturnSpec};
6use crate::schema::{HybridFileSpec, HybridMethodSpec, HybridModuleSpec, HybridType};
7
8/// Lowered metadata for one `#[batchable]` method inside a direct-FFI module.
9#[derive(Debug, Clone)]
10pub struct DirectBatchMethodSpec<'a> {
11    pub method: &'a HybridMethodSpec,
12    /// 1-based method opcode matching `NV_MOD_<UPPER>_METHOD_<NAME>`.
13    pub opcode: u16,
14    pub ret: DirectReturnSpec,
15    pub ok_ty: &'a HybridType,
16    pub has_reply: bool,
17}
18
19/// Complete lowered batch contract for a `binding = "direct"` module with `>= 1` `#[batchable]` method.
20#[derive(Debug, Clone)]
21pub struct DirectBatchSpec<'a> {
22    pub module: &'a HybridModuleSpec,
23    pub methods: Vec<DirectBatchMethodSpec<'a>>,
24    /// True iff at least one batchable method returns a non-void Ok payload.
25    pub has_replies: bool,
26    /// Unified error channel (`ModStatus`, `CustomEnum(name)`, or `String`; all-infallible
27    /// `impl = "rust"` modules normalize to `ModStatus`).
28    pub err_kind: DirectErrKind,
29}
30
31impl<'a> DirectBatchSpec<'a> {
32    /// Returns `Some(DirectBatchSpec)` iff `m` has at least one `#[batchable]` method.
33    pub fn from_module(m: &'a HybridModuleSpec, spec: &HybridFileSpec) -> Option<Self> {
34        let mut methods = Vec::new();
35        let mut has_replies = false;
36        let mut err_kind: Option<DirectErrKind> = None;
37
38        for (idx, method) in m.methods.iter().enumerate() {
39            if !method.batchable {
40                continue;
41            }
42            let ret = DirectReturnSpec::classify(method, spec);
43            let ok_ty = ret.ok_hybrid_type(method);
44            let has_reply = !matches!(ok_ty, HybridType::Void);
45            has_replies |= has_reply;
46            if err_kind.is_none() && ret.err != DirectErrKind::None {
47                err_kind = Some(ret.err.clone());
48            }
49            methods.push(DirectBatchMethodSpec {
50                method,
51                opcode: (idx + 1) as u16,
52                ret,
53                ok_ty,
54                has_reply,
55            });
56        }
57        if methods.is_empty() {
58            return None;
59        }
60        Some(Self {
61            module: m,
62            methods,
63            has_replies,
64            err_kind: err_kind.unwrap_or(DirectErrKind::ModStatus),
65        })
66    }
67
68    /// True iff the C-ABI `submit_batch` signature carries `(on_err: NvBytesSink, err_ctx: void*)`.
69    #[inline]
70    pub fn uses_err_sink(&self) -> bool {
71        self.err_kind == DirectErrKind::String
72    }
73
74    /// True iff any batchable method takes a borrowed `String` or `Bytes` parameter.
75    #[inline]
76    pub fn any_borrowed_param(&self) -> bool {
77        self.methods.iter().any(|bm| {
78            bm.method
79                .params
80                .iter()
81                .any(|p| matches!(p.ty, HybridType::String | HybridType::Bytes))
82        })
83    }
84}