DEV Community

Cover image for One source, two languages - the same StructOp declarations built as C and as Rust
Seiho Shinsho
Seiho Shinsho

Posted on Edited on Fully Autonomous

One source, two languages - the same StructOp declarations built as C and as Rust

In StructOp the build target is one word. The declarations (values, types, expressions, functions, modules and abilities) are written once, and intent.st says whether they become a C program or a Rust program.

This post accompanies episode 6 of StructOp from zero. Everything below follows the published StructOp handbook, mainly C and Rust from the same declarations and Native language interoperability. Every terminal block is a real StructOp 0.1.0 session (providers StructOp/c and StructOp/rust 2.3.0). Last time we covered testing declarations.

The target lives in intent.st

structop {
    type_app {
        exec = { lang = "C" }
    }
}
Enter fullscreen mode Exit fullscreen mode

lang is "C" or "Rust". standard is optional: C defaults to c11 (and accepts c99, c17, c23), Rust to edition 2021 (and accepts 2018 and 2024). A C build needs a C compiler; a Rust build needs Cargo.

intent.st holds the target; everything else is written once

A running example

A small temperature table. vars.st:

variable "celsius" {
    type    = Array
    of      = Int
    size    = 4
    default = [0, 10, 25, 100]
}
ability "value" "i" {
    type    = Int
    initial = 0
}
data "value" "i" {
    of = ability.value.i
}
Enter fullscreen mode Exit fullscreen mode

main.st prints a title, then a for loop prints one row per reading, calling a function that converts to Fahrenheit:

ability "print" "Title" {
    message = "Celsius to Fahrenheit"
}
ability "function" "to_f" {
    params  = [{ name = "c", type = Int }]
    returns = Int
    return  = c * 9 / 5 + 32
}
ability "for" "Table" {
    counter    = ability.value.i
    from       = 0
    while      = data.value.i.current < length(var.celsius)
    next       = data.value.i.current + 1
    body       = [ability.print.Row]
    depends_on = [ability.print.Title]
}
ability "print" "Row" {
    message = "${var.celsius[data.value.i.current]} C = ${to_f(var.celsius[data.value.i.current])} F"
}
Enter fullscreen mode Exit fullscreen mode

Build for C

The first init locks both core providers, C and Rust:

$ structop init
Initializing provider plugins...
- Provider index StructOp: serial 7 (unchanged)
- Installed StructOp/c v2.3.0
- Installed StructOp/rust v2.3.0

StructOp has been successfully initialized!
$ structop validate
Success! The configuration is valid.
$ structop apply
  ...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Celsius to Fahrenheit
0 C = 32 F
10 C = 50 F
25 C = 77 F
100 C = 212 F
Enter fullscreen mode Exit fullscreen mode

The C build: init, validate, plan and apply

Change one word

Set lang = "Rust". The plan says what changes before anything is built:

$ structop plan
  # program.app will be replaced
  ~ program.app (.structop/build/app)
      compile: cc -std=c11 -> cargo build --release (edition 2021)
      providers: StructOp/c 2.3.0 -> StructOp/rust 2.3.0
      .structop/build/main.rs is missing or was changed outside structop

Plan: 0 to add, 1 to change, 0 to destroy.
$ structop apply
  ...
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

Celsius to Fahrenheit
0 C = 32 F
10 C = 50 F
25 C = 77 F
100 C = 212 F
Enter fullscreen mode Exit fullscreen mode

We saved both outputs and compared them:

$ diff output-c.txt output-rust.txt && echo same
same
Enter fullscreen mode Exit fullscreen mode

structop state show program.app records the difference that matters: compile = cc -std=c11 and providers = StructOp/c 2.3.0 after the C build, compile = cargo build --release (edition 2021) and providers = StructOp/rust 2.3.0 after the Rust build, each with its own source file.

Same output from both builds, and what the state records

Read the generated source locally

The generated source is ordinary, commented C or Rust, one block per ability, kept in .structop/build/. We don't reproduce it here; export it and read it yourself:

$ structop apply -source -no-run
No changes. Your application matches the configuration.
Exported out/main.rs
Enter fullscreen mode Exit fullscreen mode

In a C build the same command writes out/main.c. In the Rust build an Int is an i64, a List is a Vec<T>, a class with methods is a struct with an impl block, and an interface is a trait (the full table is in the handbook).

Portable providers

A console program needs no provider list: the core is implicit. For more, name portable families, each with a C and a Rust implementation:

Family C Rust
StructOp/core c rust
StructOp/gui gtk3 gtk-rs
StructOp/game curses bracket-lib
lib_provider {
    core = { source = "StructOp/core", version = "@latest" }
}
Enter fullscreen mode Exit fullscreen mode

With the core family named, init still installs both implementations, and switching lang needs no other change. Your own library can do the same with a paired source, source = { C = "Local/storage-c", Rust = "Local/storage-rs" }.

What is tied to one target

An inline C expression, c"...", is C only. We added one to the table:

ability "set" "Native" {
    target     = ability.value.n
    value      = c"abs(-40)"
    depends_on = [ability.for.Table]
}
Enter fullscreen mode Exit fullscreen mode

While the project is still portable (implicit core, both providers locked), validate refuses it, even with lang = "C":

$ structop validate
Error: native C (c"...") cannot be built as Rust; write it as an expression
  on main.st line 22, column 18
While checking portable declarations for Rust.
Enter fullscreen mode Exit fullscreen mode

Name StructOp/c directly in lib_provider and it becomes a C project; it builds and prints -40 C = -40 F. Switch that project to Rust and init refuses:

$ structop init
Initializing provider plugins...
- Provider index StructOp: serial 7 (unchanged)
Error: provider "StructOp/c" builds C programs, but this project has lang = "Rust"; use a provider for Rust
  on intent.st line 6, column 9
Enter fullscreen mode Exit fullscreen mode

Inline C: refused while portable, built as a C project, refused as Rust

Native libraries stay native libraries

structop bind reads a C header and writes a provider for the C target; a Rust project can call some of them too (the zlib example does), while bindings whose functions take callbacks are C only. A Rust crate becomes a provider for the Rust target only. Either way the library keeps its own language, and you call it like any other provider. See C libraries and Rust crates.

structop bind: a C header becomes a C provider, a Rust crate a Rust provider

What portable means

A project is portable when every provider it uses exists for both targets and it uses no inline C. One declaration means the same on C and on Rust: the same output, and the same fault at the same place. Features only one language has, such as unions and bit-fields in C or generics and lifetimes in Rust, stay outside by design; a bound library or native code reaches them on that one target (Scope and roadmap).

The handbook's evidence: 195 of its 209 documented example projects produce the expected output on both targets, and the other 14 are target-specific by what they use. The C and Rust language providers 2.3.0 add checked heap blocks, and workers that share data only through atomic counters and flags or values guarded by a lock, on both targets.

Next

The target is one word in intent.st; portable providers keep it that way; and validate names what is tied to one target before you build. Next episode: providers, and where abilities come from.

Top comments (0)