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,
    pub impl_preconditions: Option<bool>,
    pub call_attribution: CallAttribution,
}
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.

§impl_preconditions: Option<bool>

The impl block’s preconditions / no_preconditions (ImplAttrs::preconditions): below the method’s own and each parameter’s, above the crate default (see build_method_precondition_checks).

§call_attribution: CallAttribution

The call the method’s conditions report (CallAttribution): the method’s own sys.call() by default; an S3 method’s generator sets CallAttribution::Generic, the frame resolved to the generic’s call on a raise (#1851). It decides the .call of instance_call, the call the R-side checks raise with and the raise fallback.

Implementations§

Source§

impl<'a> MethodContext<'a>

Source

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

Create a new MethodContext for a method.

Computes the C wrapper identifier from the method name and type name, then formats the R formals and call arguments from the method’s signature and default values. The impl block says nothing about the R-side checks; use MethodContext::with_impl_preconditions to inherit its preconditions / no_preconditions.

Source

pub fn with_impl_preconditions(self, impl_preconditions: Option<bool>) -> Self

Set the preconditions decision inherited from the surrounding ImplAttrs. Returns self so callers can chain on top of MethodContext::new.

Source

pub fn with_call_attribution(self, call_attribution: CallAttribution) -> Self

Set the call the method’s conditions report (see MethodContext::call_attribution).

Source

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

Build the R-param-name → @param text map for this method’s match_arg / choices params (choice_param_doc_map) and its Missing<..> params with a default (missing_default_param_docs, #2161). Pass to MethodDocBuilder::with_choice_param_docs.

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

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”. The .call is the context’s MethodContext::call_attribution.

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 sys.call() names the S7 dispatch frame, not the user’s call.

Source

pub fn s3_method_formals(&self) -> String

Full R formals of a <generic>.<Class> S3 method: "x, <params>, ...", except that for a replacement generic (a name ending in <-, such as [[<- or names<-) the dispatch ... goes before the last formal, "x, i, ..., value". R CMD check (tools::checkReplaceFuns()) requires a replacement method’s last formal to be value, and the impl-block check (check_s3_replacement_methods) makes sure it is (#1853). A method that takes &Dots keeps its own order.

Source

pub fn instance_formals_with_receiver( &self, receiver: &str, include_dots: bool, ) -> String

Build instance formals with a custom receiver name (default is x).

Used by S3/S4 generic overrides and the S7 per-class shortcut.

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 the R-side precondition guard 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 an is.character() check.

Each parameter keeps or drops its type-derived checks as its own spelling, the method’s, the impl block’s, the crate default and the feature decide (build_method_precondition_checks); the per-parameter inherits(...) / not_inherits(...) / no_na(...) checks stay.

Source

pub fn emit_method_prelude( &self, lines: &mut Vec<String>, indent: &str, what: &str, formals: &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_exit — on.exit(...) cleanup
  3. lifecycle_prelude — deprecation/superseded banner (class-system-specific label)
  4. the ... check of dots = empty | forward(..) (#2157, DotsPolicy::r_check), which reads the wrapper’s formals (the R formals list of the function() the prelude opens) and is skipped when they have no ...
  5. precondition_checks — one isTRUE() guard per check on typed params
  6. match_arg_prelude — base::match.arg(param) validation
  7. r_post_checks — user code after all checks, before .Call()

(Missing<T> forwarding is not a prelude step: it lives inline in the .Call() args — see build_call_args_vec — because a binding of the missing sentinel errors on lookup.)

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).

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<'a> RefUnwindSafe 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> SizeHint for T
where T: ?Sized,

Source§

default fn lower_bound(&self) -> usize

🔬This is a nightly-only experimental API. (core_io_internals)
Returns a lower bound on the number of elements this container-like item contains. For example, an array [u8; 12] could return any value between 0 and 12 inclusively as a correct implementation. Read more
Source§

default fn upper_bound(&self) -> Option<usize>

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an upper bound on the number of elements this container-like item contains if it can be determined, otherwise None. Read more
Source§

final fn size_hint(&self) -> (usize, Option<usize>)

🔬This is a nightly-only experimental API. (core_io_internals)
Returns an estimate for the number of elements this container like type contains. Read more
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 = !

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

fn try_from(value: U) -> Result<T, !>

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: 88 bytes