Skip to main content

MethodContext

Struct MethodContext 

Source
pub struct MethodContext<'a> {
    pub method: &'a ParsedMethod,
    pub c_ident: String,
    pub params: String,
    pub args: String,
}
Expand description

Pre-computed context for a method, holding all data needed for R wrapper generation.

This struct captures the common computations performed for every method across all class systems, reducing duplicate code. It pre-formats the C wrapper name, R formal parameters (with defaults), and R call arguments so each class generator can focus on its specific formatting logic.

Fields§

§method: &'a ParsedMethod

Reference to the parsed method metadata.

§c_ident: String

The C wrapper identifier string (e.g., "C_Counter__inc"), used in .Call().

§params: String

R formals string with defaults (e.g., "value, step = 1L"), used in function(...) signatures.

§args: String

R call arguments string without defaults (e.g., "value, step"), used inside .Call() expressions.

Implementations§

Source§

impl<'a> MethodContext<'a>

Source

pub fn new( method: &'a ParsedMethod, type_ident: &Ident, label: Option<&str>, ) -> Self

Create a new MethodContext for a method.

Computes the C wrapper identifier from the method name, type name, and optional label (for multi-impl-block disambiguation), then formats the R formals and call arguments from the method’s signature and default values.

Source

pub fn match_arg_doc_placeholders(&self) -> HashMap<String, String>

Build the R-param-name → @param placeholder map for this method’s match_arg params. Pass to MethodDocBuilder::with_match_arg_doc_placeholders so the cdylib write pass rewrites the placeholders into rendered choice descriptions (#210).

Source

pub fn match_arg_prelude(&self) -> Vec<String>

Build R prelude lines that validate match_arg / choices / several_ok parameters via base::match.arg() before the .Call().

Returns an empty vector when the method declares none. Both match_arg and choices(...) carry their choice list as the formal default (c("a", "b", ...)), so base::match.arg(arg) finds the list by itself — no second arg, no C helper lookup. match_arg adds a factor → character coercion in front of match.arg.

Callers should include these lines in the R wrapper body after parameter defaulting but before the .Call().

Source

fn match_arg_skip_set(&self) -> HashSet<String>

Rust-side parameter names that are validated by R’s match.arg() and therefore don’t need stopifnot() preconditions generated for them.

Source

pub fn static_call(&self) -> String

Build the .Call() expression for a static/constructor call.

Source

pub fn instance_call(&self, self_expr: &str) -> String

Build the .Call() expression for an instance method with self as ptr.

The self_expr is typically “self”, “private$.ptr”, “x”, “x@ptr”, or “x@.ptr”.

Source

pub fn instance_call_null_attr(&self, self_expr: &str) -> String

Like instance_call but passes .call = NULL.

Use for lambda dispatch sites (S7 property getter/setter) where match.call() captures the S7 dispatch frame, not the user’s call.

Source

pub fn instance_formals(&self, add_self_param: bool) -> String

Build full R formals for instance methods (prefixing x/self parameter).

For S3/S4/S7: "x, <params>, ..." For Env/R6: "<params>" (self is implicit)

Source

pub fn instance_formals_with_dots( &self, add_self_param: bool, include_dots: bool, ) -> String

Build full R formals for instance methods with optional dots.

When include_dots is false, omits ... from the signature. This is used for strict generics that don’t accept extra args.

Source

pub fn generic_name(&self) -> String

Get the generic name (uses override if present).

Source

pub fn source_comment(&self, type_ident: &Ident) -> String

Generate a source location comment for this method.

Returns a string like # Type::method (line:col) using the method’s span info. The file name is already stated in the impl block header comment, so line:col is sufficient to locate the method within that file.

Source

pub fn has_generic_override(&self) -> bool

Check if this method uses a generic override (for existing generics like print).

Source

pub fn class_suffix(&self) -> Option<&str>

Get custom class suffix if specified.

This allows double-dispatch patterns like vec_ptype2.my_class.my_class by specifying #[miniextendr(s3(generic = "vec_ptype2", class = "my_class.my_class"))].

Source

pub fn has_class_override(&self) -> bool

Check if this method uses a custom class suffix.

Source

pub fn precondition_checks(&self) -> Vec<String>

Build R-side precondition stopifnot() lines for this method’s parameters.

Returns static checks for known types. Custom types not in the static table are identified as fallback params but no R-side precheck is generated for them.

Skips self/receiver parameters automatically (they are FnArg::Receiver) and any parameter validated by base::match.arg() (via match_arg / choices) — those already have a stronger runtime guarantee than stopifnot(is.character(...)).

Source

pub fn emit_method_prelude( &self, lines: &mut Vec<String>, indent: &str, what: &str, )

Emit the 7-step method prelude into lines, each line prefixed with indent.

The prelude is the standardised sequence that appears at the top of every generated R method body, in order:

  1. r_entry — user code injected before any checks
  2. r_on_exiton.exit(...) cleanup
  3. missing_preludeif (missing(param)) param <- quote(expr=) for Missing<T>
  4. lifecycle_prelude — deprecation/superseded banner (class-system-specific label)
  5. precondition_checksstopifnot(is.*(param)) for typed params
  6. match_arg_preludebase::match.arg(param) validation
  7. r_post_checks — user code after all checks, before .Call()

what is the human-readable method label passed to lifecycle_prelude (e.g., "Type.method" for S3/S4, "Type$method" for Env/R6/S7). indent is the per-line prefix (e.g., " " for 2-space, " " for 6-space).

Source

pub fn missing_prelude(&self) -> Vec<String>

Build if (missing(param)) param <- quote(expr=) prelude lines for Missing parameters.

Skips params that have a user-specified default (they get the default in formals instead).

Auto Trait Implementations§

§

impl<'a> Freeze for MethodContext<'a>

§

impl<'a> RefUnwindSafe for MethodContext<'a>

§

impl<'a> !Send for MethodContext<'a>

§

impl<'a> !Sync for MethodContext<'a>

§

impl<'a> Unpin for MethodContext<'a>

§

impl<'a> UnsafeUnpin for MethodContext<'a>

§

impl<'a> UnwindSafe for MethodContext<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> SizedTypeProperties for T

Source§

#[doc(hidden)]
const SIZE: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const ALIGNMENT: Alignment = _

🔬This is a nightly-only experimental API. (ptr_alignment_type)
Source§

#[doc(hidden)]
const IS_ZST: bool = _

🔬This is a nightly-only experimental API. (sized_type_properties)
true if this type requires no storage. false if its size is greater than zero. Read more
Source§

#[doc(hidden)]
const LAYOUT: Layout = _

🔬This is a nightly-only experimental API. (sized_type_properties)
Source§

#[doc(hidden)]
const MAX_SLICE_LEN: usize = _

🔬This is a nightly-only experimental API. (sized_type_properties)
The largest safe length for a [Self]. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 80 bytes