Skip to main content

CodeGen

Struct CodeGen 

Source
pub struct CodeGen { /* private fields */ }
Expand description

Builder for generating Rust source code from symbolic expressions.

Collects multiple functions (scalar and matrix) and writes them all to a single output file with shared preamble (cfg-gated math module, etc.).

Implementations§

Source§

impl CodeGen

Source

pub fn new() -> Self

Create a new CodeGen builder with default options.

Source

pub fn options(self, options: CodegenOptions) -> Self

Set custom code generation options.

Every registered function is emitted with these options. The special-function runtime (mod symplex_rt, needed by gamma, lambertw, Bessel functions, …) is emitted once at the top of the file when CodegenOptions::emit_runtime is true (the default) and at least one function needs it. Set emit_runtime: false to leave it out entirely — e.g. when several generated files share one copy of CodegenOptions::runtime_module:

use symplex::matrix::{CodegenOptions, MathBackend};
use symplex::prelude::*;
use symplex_build::CodeGen;

let ctx = Context::new();
let x = ctx.symbol("x");
let opts = CodegenOptions {
    math_backend: MathBackend::CfgGated,
    emit_runtime: false,
    ..Default::default()
};
let body = CodeGen::new()
    .options(opts.clone())
    .add_scalar_fn("g", &x.gamma(), &["x"])
    .add_scalar_fn("w", &x.lambertw(), &["x"])
    .generate()
    .unwrap();
std::fs::write("robot_math.rs", body).unwrap();
std::fs::write("symplex_rt.rs", opts.runtime_module()).unwrap();
Source

pub fn no_std(self, enabled: bool) -> Self

Enable or disable no_std-compatible output (uses the CfgGated math backend).

Source

pub fn precision_f32(self) -> Self

Use f32 precision for generated code.

Source

pub fn inline(self, enabled: bool) -> Self

Set whether to emit #[inline] annotations on generated functions.

Source

pub fn add_scalar_fn(self, name: &str, expr: &Ex, params: &[&str]) -> Self

Add a scalar function to the output.

The generated function will take the named parameters as f64 (or f32) arguments and return the scalar result.

Source

pub fn add_matrix_fn(self, name: &str, matrix: &Matrix, params: &[&str]) -> Self

Add a matrix function to the output.

The generated function will take the named parameters and return a flat array [f64; rows*cols] in row-major order.

Source

pub fn with_tests(self, enabled: bool) -> Self

Enable or disable companion test generation.

When enabled, a #[cfg(test)] mod generated_tests { ... } block is appended with test functions that evaluate at any configured test points.

Source

pub fn add_test_point(self, point: &[f64]) -> Self

Add a test evaluation point.

Each test point is a slice of f64 values corresponding to the function parameters in order. During test generation, each function is called with each test point to verify it produces a finite result.

Source

pub fn generate(&self) -> Result<String, Box<dyn Error>>

Generate the full source file as a String.

  1. If the math backend is CfgGated, emits the cfg-gated math module (once; the per-function copies are stripped).
  2. If CodegenOptions::emit_runtime is set and any registered function uses a special function, emits the mod symplex_rt runtime once, containing exactly the helpers the file needs.
  3. For each registered function, calls the appropriate symplex codegen method.
  4. If test generation is enabled, emits a #[cfg(test)] module.
Source

pub fn write_to_out_dir(&self, filename: &str) -> Result<(), Box<dyn Error>>

Generate code and write it to $OUT_DIR/<filename>.

Also prints cargo:rerun-if-changed=build.rs so Cargo knows when to re-run the build script.

Source

pub fn write_to_path( &self, path: impl AsRef<Path>, ) -> Result<(), Box<dyn Error>>

Generate code and write it to an explicit path.

Trait Implementations§

Source§

impl Default for CodeGen

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more