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 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.
Implementations§
Source§impl<'a> MethodContext<'a>
impl<'a> MethodContext<'a>
Sourcepub fn new(
method: &'a ParsedMethod,
type_ident: &Ident,
label: Option<&str>,
) -> Self
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.
Sourcepub fn match_arg_doc_placeholders(&self) -> HashMap<String, String>
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).
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().
Sourcefn match_arg_skip_set(&self) -> HashSet<String>
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.
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”.
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
match.call() captures the S7 dispatch frame, not the user’s call.
Sourcepub fn instance_formals(&self, add_self_param: bool) -> String
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)
Sourcepub fn instance_formals_with_dots(
&self,
add_self_param: bool,
include_dots: bool,
) -> String
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.
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 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(...)).
Sourcepub fn emit_method_prelude(
&self,
lines: &mut Vec<String>,
indent: &str,
what: &str,
)
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:
r_entry— user code injected before any checksr_on_exit—on.exit(...)cleanupmissing_prelude—if (missing(param)) param <- quote(expr=)forMissing<T>lifecycle_prelude— deprecation/superseded banner (class-system-specific label)precondition_checks—stopifnot(is.*(param))for typed paramsmatch_arg_prelude—base::match.arg(param)validationr_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).
Sourcepub fn missing_prelude(&self) -> Vec<String>
pub fn missing_prelude(&self) -> Vec<String>
Build if (missing(param)) param <- quote(expr=) prelude lines for Missing
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> 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> 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: 80 bytes