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 ParsedMethodReference to the parsed method metadata.
c_ident: StringThe C wrapper identifier string (e.g., "C_Counter__inc"), used in .Call().
params: StringR formals string with defaults (e.g., "value, step = 1L"), used in
function(...) signatures.
args: StringR 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: CallAttributionThe 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>
impl<'a> MethodContext<'a>
Sourcepub fn new(method: &'a ParsedMethod, type_ident: &Ident) -> Self
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.
Sourcepub fn with_impl_preconditions(self, impl_preconditions: Option<bool>) -> Self
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.
Sourcepub fn with_call_attribution(self, call_attribution: CallAttribution) -> Self
pub fn with_call_attribution(self, call_attribution: CallAttribution) -> Self
Set the call the method’s conditions report (see
MethodContext::call_attribution).
Sourcepub fn choice_param_docs(&self) -> HashMap<String, String>
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.
Sourcepub fn match_arg_prelude(&self) -> Vec<String>
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().
Sourcepub fn static_call(&self) -> String
pub fn static_call(&self) -> String
Build the .Call() expression for a static/constructor call.
Sourcepub fn instance_call(&self, self_expr: &str) -> String
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.
Sourcepub fn instance_call_null_attr(&self, self_expr: &str) -> String
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.
Sourcepub fn s3_method_formals(&self) -> String
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.
Sourcepub fn instance_formals_with_receiver(
&self,
receiver: &str,
include_dots: bool,
) -> String
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.
Sourcepub fn generic_name(&self) -> String
pub fn generic_name(&self) -> String
Get the generic name (uses override if present).
Sourcepub fn source_comment(&self, type_ident: &Ident) -> String
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.
Sourcepub fn has_generic_override(&self) -> bool
pub fn has_generic_override(&self) -> bool
Check if this method uses a generic override (for existing generics like print).
Sourcepub fn class_suffix(&self) -> Option<&str>
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"))].
Sourcepub fn has_class_override(&self) -> bool
pub fn has_class_override(&self) -> bool
Check if this method uses a custom class suffix.
Sourcepub fn precondition_checks(&self) -> Vec<String>
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.
Sourcepub fn emit_method_prelude(
&self,
lines: &mut Vec<String>,
indent: &str,
what: &str,
formals: &str,
)
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:
r_entry— user code injected before any checksr_on_exit—on.exit(...)cleanuplifecycle_prelude— deprecation/superseded banner (class-system-specific label)- the
...check ofdots = empty | forward(..)(#2157,DotsPolicy::r_check), which reads the wrapper’sformals(the R formals list of thefunction()the prelude opens) and is skipped when they have no... precondition_checks— oneisTRUE()guard per check on typed paramsmatch_arg_prelude—base::match.arg(param)validationr_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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> SizeHint for Twhere
T: ?Sized,
impl<T> SizeHint for Twhere
T: ?Sized,
Source§default fn lower_bound(&self) -> usize
default fn lower_bound(&self) -> usize
core_io_internals)[u8; 12] could return any value between 0 and
12 inclusively as a correct implementation. Read moreSource§impl<T> SizedTypeProperties for T
impl<T> SizedTypeProperties for T
Source§#[doc(hidden)]const SIZE: usize = _
#[doc(hidden)]const SIZE: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGN: usize = _
#[doc(hidden)]const ALIGN: usize = _
sized_type_properties)Source§#[doc(hidden)]const ALIGNMENT: Alignment = _
#[doc(hidden)]const ALIGNMENT: Alignment = _
ptr_alignment_type)Source§#[doc(hidden)]const IS_ZST: bool = _
#[doc(hidden)]const IS_ZST: bool = _
sized_type_properties)Source§#[doc(hidden)]const LAYOUT: Layout = _
#[doc(hidden)]const LAYOUT: Layout = _
sized_type_properties)Source§#[doc(hidden)]const MAX_SLICE_LEN: usize = _
#[doc(hidden)]const MAX_SLICE_LEN: usize = _
sized_type_properties)[Self]. Read moreLayout§
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