Use this page with an LLM

Markdown

Constants

BoltFFI exports two kinds of constants:

  • A global constant is a standalone Rust const marked with #[export].
  • An associated constant belongs to an exported record, enum, or class.

Both forms produce immutable values in the generated API. BoltFFI emits simple literals directly in generated source and reads values that require Rust evaluation through a generated native accessor.

Use a global constant for a value that belongs to the package as a whole. Use an associated constant when the value describes a particular record, enum, or class.

Global constants

Mark a standalone public constant with #[export]. Swift, Kotlin, TypeScript, and Python expose it at module scope. Java and C# place it on the generated module class because those languages do not have module-level values.

RustSource
#[export]
pub const API_VERSION: u32 = 3;

#[export]
pub const SERVICE_NAME: &'static str = "BoltFFI";
public let apiVersion: UInt32 = 3
public let serviceName: String = "BoltFFI"

let version = apiVersion

The Rust name is converted to the naming convention of each target. API_VERSION becomes apiVersion in Swift, Kotlin, and TypeScript, API_VERSION in Java, ApiVersion in C#, and api_version in Python.

Associated constants

Associated constants are declared inside the same exported impl blocks used for methods and constructors:

  • Use #[data(impl)] for a struct or enum marked with #[data].
  • Use #[export] for a class impl.

Every public constant in the marked impl is generated on its owner type. The constant may use Self or any other supported constant type. The impl marker exports the member, so the individual constant does not need another #[export] attribute.

RustSource
#[data]
pub struct Color {
  pub r: u8,
  pub g: u8,
  pub b: u8,
  pub a: u8,
}

#[data(impl)]
impl Color {
  pub const BLACK: Self = Self {
      r: 0,
      g: 0,
      b: 0,
      a: 255,
  };

  pub const CHANNEL_COUNT: u8 = 4;
}

#[repr(u8)]
#[data]
pub enum Mode {
  Fast = 1,
  Slow = 2,
}

#[data(impl)]
impl Mode {
  pub const DEFAULT: Self = Self::Fast;
}

pub struct Palette;

#[export]
impl Palette {
  pub const MAX_COLORS: u8 = 16;

  pub fn new() -> Self {
      Self
  }
}
public struct Color {
  public var r: UInt8
  public var g: UInt8
  public var b: UInt8
  public var a: UInt8

  public static var black: Color { get }
  public static let channelCount: UInt8
}

public enum Mode: UInt8 {
  case fast = 1
  case slow = 2

  public static let `default`: Mode
}

public final class Palette {
  public static let maxColors: UInt8
}

let color = Color.black
let mode = Mode.default
let limit = Palette.maxColors

Color.BLACK uses Self, Color.CHANNEL_COUNT uses a primitive, Mode.DEFAULT aliases an enum variant, and Palette.MAX_COLORS belongs to a Rust-backed class. In every target, the value remains attached to the type that owns it.

Data constants

Every record or enum marked with #[data] can be the value type of a global or associated constant. This includes primitive-only records, encoded records, C-style enums, and data enums. The selected target must already support that data type as a normal return value.

The Color and Mode declarations above are examples of associated data constants. The same data types also work as global constants:

RustSource
#[export]
pub const DEFAULT_COLOR: Color = Color {
  r: 30,
  g: 30,
  b: 30,
  a: 255,
};

#[export]
pub const DEFAULT_MODE: Mode = Mode::Fast;
public var defaultColor: Color { get }
public let defaultMode: Mode = Mode.fast

let color = defaultColor
let mode = defaultMode

Values such as Color { ... } are constructed in Rust and delivered through a generated accessor. The generated API still exposes an ordinary immutable value.

Supported values

Constants can use the same types as exported function results, including primitives, strings, byte slices, tuples, and types marked with #[data]. Associated constants can use Self for values of their owner type.