Skip to main content

generate_s4_class

Function generate_s4_class 

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

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

Produces the following R code:

  • Class definition: methods::setClass("<class>", slots = c(ptr = "externalptr")) with a single ptr slot holding the ExternalPtr to the Rust struct
  • Constructor function: ClassName(...) that calls the Rust constructor and wraps the result with methods::new("<class>", ptr = .val). A class without a constructor has no ClassName(): its factories call methods::new(), and a package may define its own (#2069).
  • S4 generics: methods::setGeneric(...) for each instance method, guarded by s4_generic_guard
  • S4 methods: methods::setMethod("<generic>", "<class>", function(x, ...) ...) dispatching to the Rust .Call() wrapper, extracting the ptr via x@ptr
  • Static methods: regular functions named <class>_<method>(...)

Roxygen2 @exportMethod, @importFrom methods, and @slot tags are generated as appropriate.

The class, its constructor and the field methods are the head; the S4 methods and the static methods are the members.