Skip to main content

2  Types between R and Rust

new_struct() runs no compiler. R8’s Rust code is compiled once, when the package R8 is built, and it holds no code for any particular type. A declaration in R makes a description of the type, as data, and gives it to that code, which reads the description each time it makes, checks, compares, or writes a value (Figure 2.1). As CREATE TABLE adds a description of a table to a database and leaves the database’s program as it is, new_struct() adds a description of a struct to R8.

R runs at the call Rust compiled once, when R8 is built 1 Declare once in a session Interval <- new_struct("Interval", fields = list(lo = type_double(), hi = type_double()), rules = rule(lo <= hi)) Lists that describe the type; the rule is not run. r8_declare() one function for every type Read the lists into a value of StructDef StructDef { name: "Interval", fields: [lo: Double, hi: Double], rules: [lo <= hi], .. } Check names, bounds, defaults, and rules Store it in the registry Registry demo::Interval, ... .Call Interval an R function function(lo, hi) .Call(construct, environment(), "demo::Interval", ...) Its attribute: the declaration, as a record of bytes. formals, record 2 Construct for each value x <- Interval(lo = 0, hi = 1) The arguments, and the name of the type. r8_new_value() one function for every type Find the StructDef of demo::Interval Read each field's R vector where it is, and check it against the field's type Evaluate the rules over the fields Make the value, or list every problem The code reads the declaration as data, as an interpreter reads a program. .Call x an R list lo 0 hi 1 identity demo::Interval, declaration 4f2a9c... value No Rust is compiled. Interval is data: a value of the Rust struct StructDef, which exists once for all types. No Rust type Interval is made. With #[derive(R8)], one is, by rustc.
Figure 2.1: A declaration, and a construction. Teal marks R; orange marks Rust. The Rust functions are the same for every type.

2.1 The declaration

Interval <- new_struct(
  "Interval",
  fields = list(lo = type_double(), hi = type_double()),
  rules = rule(lo <= hi)
)

In R. type_double() returns an R list that describes a field type, and rule() a list that holds the expression lo <= hi, which it does not run. new_struct() passes these lists to Rust in one .Call.

In Rust. The function r8_declare() reads the lists into a value of StructDef, an ordinary struct of the crate rtemis-r8:

pub struct StructDef {
    pub name: Name,
    pub description: String,
    pub version: u32,
    pub fields: Vec<FieldDef>,
    pub rules: Vec<RuleDef>,
    // ...
}

A FieldDef holds the field’s name, its default, and its FieldType, an enum with a variant for each kind of type, Double with its bounds among them. r8_declare() checks the description: the names, the bounds, the defaults against the types, and each rule against the fields it reads. It stores the StructDef in the registry, a map from qualified names to declarations, and returns the formals of the constructor and the declaration’s record, its form in bytes.

Interval is then an R function:

class(Interval)
[1] "r8_struct_type" "r8_type"        "function"      
names(formals(Interval))
[1] "lo" "hi"
body(Interval)
{
    .r8_value_ <- .Call(.r8_native[["construct"]], environment(), 
        c("lo", "hi"), c(TRUE, TRUE), "demo::Interval", "")
    if (inherits(.r8_value_, "r8_problems")) 
        construct_failed(.r8_value_, environment(), sys.function(), 
            "demo::Interval", "")
    else .r8_value_
}

The body is the same for every struct, but for the names of the fields and of the type. The registry’s copy is the declaration, which r8declaration() writes as JSON:

jsonlite::prettify(r8declaration(Interval), indent = 2)
{
  "$ir": 1,
  "namespace": "demo",
  "types": [
    {
      "kind": "struct",
      "name": "Interval",
      "description": "",
      "version": 1,
      "fields": [
        {
          "name": "lo",
          "type": {
            "kind": "double"
          },
          "description": ""
        },
        {
          "name": "hi",
          "type": {
            "kind": "double"
          },
          "description": ""
        }
      ],
      "rules": [
        {
          "invariant": {
            "<=": [
              {
                "var": "lo"
              },
              {
                "var": "hi"
              }
            ]
          }
        }
      ]
    }
  ]
}
 

2.2 A construction

x <- Interval(lo = 0, hi = 1)
typeof(x)
[1] "list"

The constructor passes its arguments, as R objects, and the name "demo::Interval". The Rust function r8_new_value() then

  1. finds the StructDef of that name in the registry;
  2. goes through its fields, and checks the R object of each against the FieldType, reading the vector in R’s memory;
  3. evaluates the rules, once the fields are valid;
  4. makes the value, an R list of the fields that carries its type and the hash of its declaration, or returns every problem.

The code of these steps matches on the variants of FieldType as it runs. It is generic over where a value is held, in R objects or in a JSON document, and not over the type that the value is of. A field is found by its position in the declaration, at run time.

A rule is held as a tree, whose nodes are the operators <= and +, the fields, and the constants. Its first evaluation binds the names to the fields and their types, and keeps the result until a declaration changes. The tree is evaluated, node by node, over the values. No machine code is generated. Rules over the rows of a table are evaluated node by node too, with a block of 1,024 rows at each node, so that the cost of the tree is spread over the rows.

2.3 In Rust terms

A type declared in R is a value, and a type declared in Rust is a type:

new_struct() in R #[derive(R8)] in Rust
rustc does not run compiles the struct, when its package is built
A new Rust type none the struct
The declaration a StructDef, made from R’s arguments at run time a StructDef, made by the code that the derive writes
A value an R list the struct, or an R list (Values in R or in Rust)
A field, in Rust code found at run time by its position in the declaration self.lo, an f64 at a known offset
The checks by the one validator, which reads the StructDef by the same validator, which reads the StructDef
The compiler’s checks none: a wrong field name is an error when the declaration or the call runs the Rust compiler’s

Both declarations are entries of one registry, so that R code, documents, hashes, and dispatch treat them alike. A type whose work happens in Rust is declared there, as a Rust-stored type, and its methods have the struct at hand (Impls and types in Rust).

What a description costs is a lookup and a match for each field at each construction. A struct of two fields and a rule is declared in about 57 µs, and a value of it is made in about 1.8 µs (one session on an arm64 Mac, 2026-11-25). The checks over the elements of a vector are loops over its memory, compiled for the kind of the vector.

2.4 Again, in a package, and in another session

A struct declared again replaces the StructDef of its name. Values made before carry the hash of the earlier declaration, and are refused where the type is used until r8update() converts them (Values made before a change).

In a package, new_struct() runs when the package is installed. The constructor and the record are saved with the package’s R code, and r8register(), in .onLoad(), reads the records into the registry of each session that loads it (R8 in a package). The installation of a package that declares its types in R needs no Rust toolchain.

Outside a package, the record is an attribute of the type object. A worker process that receives Interval with a task registers it from the record on first use.

© 2026 E.D. Gennatas