Native language interoperability
StructOp does not seek to replace established programming languages. It provides a unified declarative layer through which supported capabilities of C and Rust can be used, and it hands the generated source to their own toolchains.
On this page
Targets#
StructOp 0.1.0 has two targets, C and Rust. A project selects one in intent.st:
structop {
type_app {
exec = { lang = "C" }
}
}lang is "C" or "Rust". standard is optional: C defaults to c11 and also accepts c99, c17 and c23; Rust defaults to edition 2021 and also accepts 2018 and 2024. See C and Rust targets.
Source generation and compilation#
structop apply writes one source file, .structop/build/main.c or .structop/build/main.rs, and compiles it: C with cc (or the compiler named by STRUCTOP_CC), Rust with cargo build --release in a generated Cargo project. The recorded state names the command, as here for the guest list:
$ structop state show program.app # program.app path = .structop/build/app compile = cc -std=c11 providers = StructOp/c 2.2.1 ...
Switching the same project to Rust changes the compiler and the provider, and the plan says so 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.2.1 -> StructOp/rust 2.2.1
.structop/build/main.rs is missing or was changed outside structop
Plan: 0 to add, 1 to change, 0 to destroy.Inspecting generated source#
The generated source is ordinary C or Rust. The handbook does not reproduce it; to read it for your own project, export it locally:
$ structop apply -source -no-run
This writes out/main.c or out/main.rs without running the program. Add -binary to copy the program as well.
Target-independent declarations#
Values, types, expressions, functions, modules and the abilities of the portable provider families are written once. The guest list above builds and prints the same output as C and as Rust. The portable families are StructOp/core (C and Rust core providers), StructOp/gui (GTK 3 and gtk-rs) and StructOp/game (curses and bracket-lib).
Target-specific functionality#
- Inline C. A native C expression,
c"...", builds only as C. - C library bindings. A provider written by
structop bindfrom a C header is for the C target. A Rust project can also call some of them: the zlib example does. Bindings whose functions take callbacks, such as the curl, libc and SQLite examples, are C only. - Rust crate bindings. A provider written from a Rust crate is for the Rust target only.
- Concrete providers. A provider named directly, rather than through a portable family, declares its language, and
initrefuses a project whose target does not match:
$ structop init Error: provider "StructOp/c" builds C programs, but this project has lang = "Rust"; use a provider for Rust on intent.st line 7, column 9 Initializing provider plugins...
Native libraries#
structop bind reads a C header, or a crate’s documentation data, and writes a provider with one ability kind per function. The application then calls the library like any other provider. See C libraries and Rust crates.
Portability scope#
Portability is a property of a project’s declarations and providers. A project is portable between C and Rust when every provider it uses exists for both targets and it uses no inline C. Validation checks the language and the source generation for the selected target; building and running the other target still needs its toolchain and native dependencies. Memory operations are target-aware; the table in C and Rust language support lists them. 195 of the 209 documented example projects produce the expected output on both targets, and the other 14 are target-specific by what they use.