Skip to main content

miniextendr_api/trait_abi/
signature.rs

1//! Concrete argument types inferred from an inherited trait method.
2//!
3//! Metadata describes the R-facing shape, but a copied Rust path may name a
4//! private declaration-local alias. This zero-sized witness gets its type from
5//! the actual method item instead. Conversion errors remain concrete at the
6//! generated call site, preserving the condition-class probes.
7
8use crate::from_r::NativeBorrow;
9use crate::gc_protect::Root;
10use crate::{SEXP, TryFromRoot, TryFromSexp};
11use core::marker::PhantomData;
12
13/// An inferred method argument type, used only by generated wrappers.
14#[doc(hidden)]
15pub struct Argument<T>(PhantomData<fn() -> T>);
16
17impl<T> Argument<T> {
18    /// Construct a type witness without constructing an argument.
19    pub const NEW: Self = Self(PhantomData);
20
21    /// Convert through the inferred type's ordinary converter.
22    pub fn convert(&self, input: SEXP) -> Result<T, T::Error>
23    where
24        T: TryFromSexp,
25    {
26        T::try_from_sexp(input)
27    }
28
29    /// Convert a borrow whose lifetime is tied to the wrapper's root.
30    pub fn rooted<'a>(&self, input: Root<'a>) -> Result<T, T::Error>
31    where
32        T: TryFromRoot<'a>,
33    {
34        T::try_from_root(input)
35    }
36
37    /// The ordinary converter's alias-preflight metadata.
38    pub fn native_borrow(&self) -> Option<NativeBorrow>
39    where
40        T: TryFromSexp,
41    {
42        T::NATIVE_BORROW
43    }
44
45    /// The rooted converter's alias-preflight metadata.
46    pub fn rooted_native_borrow<'a>(&self) -> Option<NativeBorrow>
47    where
48        T: TryFromRoot<'a>,
49    {
50        T::NATIVE_BORROW
51    }
52
53    /// The ordinary converter's R-facing diagnostic expectation.
54    pub fn expectation(&self) -> Option<&'static str>
55    where
56        T: TryFromSexp,
57    {
58        T::__MX_EXPECTATION
59    }
60}