Skip to main content

generate_env_class

Function generate_env_class 

Source
pub fn generate_env_class(parsed_impl: &ParsedImpl) -> ClassWrapper
Expand description

Generates the R wrapper of an environment-based class, in the parts the wrapper writer merges per type (super::ClassWrapper, #1776).

Produces an R environment object (new.env(parent = emptyenv())) that serves as a class namespace, with methods attached as ClassName$method_name. This pattern supports both inherent methods and trait namespace dispatch via $/[[.

The generated code includes:

  • Class environment: ClassName <- new.env(parent = emptyenv())
  • Constructor: ClassName$new(...) that calls the Rust new function, sets class(self) <- "ClassName", and returns the ExternalPtr as self
  • Instance methods: ClassName$method(...) whose bodies read a free self, which $ dispatch binds in a fresh environment over the method’s own
  • Static methods: ClassName$method(...) that call Rust directly
  • $.ClassName S3 method: dispatches obj$method(...) by looking up the method in the class environment, binding .mx_self for instance methods, and supporting trait namespace environments (nested envs with .__mx_instance__ attributes), through the package’s shared .miniextendr_env_method helper
  • [[.ClassName: the same lookup, with exact (ignored) and a ... that refuses every argument (#2174)
  • With env(r_data_accessors): a field branch at the top of $.ClassName (a #[r_data] field wins over a method of the same name), .DollarNames.ClassName (the fields, then the methods; #1885), and, unless the getters-only r_data_accessors = "get", $<-.ClassName with its [[<-.ClassName alias (field_syntax.rs, #1848)

Roxygen2 documentation is generated for the class, each method, and the dispatch methods, with appropriate @export/@keywords internal/@noRd tags.

The class environment and constructor are the head; the instance and static methods are the members; the dispatch methods are the tail.