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::from_r::prepared::Prepared;
10use crate::gc_protect::Root;
11use crate::{SEXP, TryFromRoot, TryFromSexp};
12use core::marker::PhantomData;
13
14/// An inferred method argument type, used only by generated wrappers.
15#[doc(hidden)]
16pub struct Argument<T>(PhantomData<fn() -> T>);
17
18impl<T> Argument<T> {
19    /// Construct a type witness without constructing an argument.
20    pub const NEW: Self = Self(PhantomData);
21
22    /// Convert through the inferred type's ordinary converter.
23    pub fn convert(&self, input: SEXP) -> Result<T, T::Error>
24    where
25        T: TryFromSexp,
26    {
27        T::try_from_sexp(input)
28    }
29
30    /// Prepare the conversion for the second pass of the alias check, which
31    /// learns the arm an `Either` takes (#1504).
32    pub fn prepare(&self, input: SEXP) -> Prepared<T>
33    where
34        T: TryFromSexp,
35    {
36        T::__mx_prepare(input)
37    }
38
39    /// Convert an argument the alias check may have prepared.
40    pub fn convert_prepared(&self, prepared: Prepared<T>, input: SEXP) -> Result<T, T::Error>
41    where
42        T: TryFromSexp,
43    {
44        crate::from_r::prepared::convert(prepared, input)
45    }
46
47    /// Convert a borrow whose lifetime is tied to the wrapper's root.
48    pub fn rooted<'a>(&self, input: Root<'a>) -> Result<T, T::Error>
49    where
50        T: TryFromRoot<'a>,
51    {
52        T::try_from_root(input)
53    }
54
55    /// The ordinary converter's alias-preflight metadata.
56    pub fn native_borrow(&self) -> Option<NativeBorrow>
57    where
58        T: TryFromSexp,
59    {
60        T::NATIVE_BORROW
61    }
62
63    /// The rooted converter's alias-preflight metadata.
64    pub fn rooted_native_borrow<'a>(&self) -> Option<NativeBorrow>
65    where
66        T: TryFromRoot<'a>,
67    {
68        T::NATIVE_BORROW
69    }
70
71    /// The ordinary converter's R-facing diagnostic expectation.
72    pub fn expectation(&self) -> Option<&'static str>
73    where
74        T: TryFromSexp,
75    {
76        T::__MX_EXPECTATION
77    }
78}