Cargo.toml
Overview
`Cargo.toml` is the manifest file for the Rust package **orjson**, a high-performance JSON serialization and deserialization library for Python. This file serves as the central configuration point for the Rust build system Cargo, specifying metadata, dependencies, features, build profiles, and compilation settings for the Rust components of the project.
The key functionalities of this file include:
Defining the package metadata (name, version, authors, description).
Specifying Rust edition and minimum Rust compiler version.
Declaring the crate type as
cdylibto generate a C-compatible dynamic library for Python FFI.Managing feature flags that control conditional compilation and optional functionality such as the embedded
yyjsonC JSON parser backend.Listing dependencies and build dependencies with precise versioning and feature control.
Configuring compiler profiles (
dev,release, and build overrides) to optimize build speed, debug capabilities, and binary size.Including files and directories to be packaged for distribution.
This manifest is essential for enabling reproducible, configurable builds that integrate Rust with embedded C code and Python bindings.
Detailed Explanation of Sections
[package]
Defines core metadata and packaging details.
Field | Description |
|---|---|
The package name, `"orjson"`. | |
Current version, `"3.11.1"`. | |
`authors` | List of authors; here `["ijl "]`. |
`description` | Brief description of the package. |
`edition` | Rust edition used, `"2021"`. |
`resolver` | Dependency resolver version, `"2"`. |
`rust-version` | Minimum Rust compiler version required, `"1.82"`. |
License type, `"Apache-2.0 OR MIT"` (dual licensed). | |
`readme` | Path to the README file included in the package. |
`keywords` | Array of keywords describing the package, aiding discoverability. |
`include` | List of files/directories to include in the published package (e.g., source, tests, licenses). |
[lib]
Configures the Rust library crate output.
Field | Description |
|---|---|
Library crate name, `"orjson"`. | |
`crate-type` | Build output types. Here [["cdylib"]](/projects/287/67747) indicates building a C-compatible dynamic library, suitable for Python FFI via PyO3. |
[features]
Defines optional and conditional compilation features.
Feature | Description |
|---|---|
Empty array, meaning no features enabled by default. | |
`unwind` | Enables panic unwinding support, specifically avoiding bundling [libgcc](/projects/287/67747) on musl targets. |
`yyjson` | Enables building and linking the embedded `yyjson` C JSON parser backend. |
Detected by `build.rs`, enables AVX-512 SIMD optimizations on supported hardware. | |
Detected by `build.rs`, related to platform-specific integer size handling. | |
`generic_simd` | Detected by `build.rs`, enables generic SIMD optimizations. |
Detected by `build.rs`, enables inline integer optimizations. | |
Detected by `build.rs`, enables use of CPU intrinsics for performance. | |
Detected by `build.rs`, enables general build-time optimizations. |
**Note:** Features marked as "detected by build.rs" are automatically enabled based on environment and compiler capabilities and should not be manually specified.
[dependencies]
Lists runtime dependencies for the Rust crate with specific version constraints and feature flags.
Dependency | Version/Source | Features & Notes |
|---|---|---|
`"2"` | [default-features = false](/projects/287/68131) disables features not required. | |
`"1"` | [default-features = false](/projects/287/68131). | |
`bytecount` | `"^0.6.7"` | Disabled default features; enables ["runtime-dispatch-simd"](/projects/287/67764) for SIMD. |
`encoding_rs` | `"0.8"` | [default-features = false](/projects/287/68131). |
`half` | `"2"` | Enables `"std"` feature for standard library support. |
`itoa` | `"1"` | Integer to ASCII conversions, [default-features = false](/projects/287/68131). |
`itoap` | `"1"` | Fast integer to ascii printing, with `"std"` and `"simd"` features. |
`jiff` | `"^0.2"` | [default-features = false](/projects/287/68131). |
`once_cell` | `"1"` | For lazy static initialization with ["alloc"](/projects/287/67784) and `"race"` features. |
`pyo3-ffi` | `"0.25"` | Python FFI layer with ["extension-module"](/projects/287/67734) feature enabled. |
`ryu` | `"1"` | Fast float-to-string conversion, [default-features = false](/projects/287/68131). |
`serde` | `"1"` | Serialization framework, no default features. |
`"1"` | JSON serialization with `"std"` and ["float_roundtrip"](/projects/287/67755) features. | |
`"0.1"` | UTF-8 validation with SIMD acceleration, `"std"`, `"public_imp"`, `"aarch64_neon"` features enabled. | |
`smallvec` | `"^1.11"` | Small vector optimization with `"union"` and `"write"` features. |
`unwinding` | `"=0.2.5"` | Optional dependency for panic unwinding with `"unwinder"` feature. |
`"1"` | UUID support, defaults disabled. | |
`xxhash-rust` | `"^0.8"` | Fast hashing with `"xxh3"` feature enabled. |
[build-dependencies]
Crates required during the build process, not linked to the final binary.
Dependency | Version | Purpose |
|---|---|---|
`"1"` | Used to compile embedded C code (`yyjson`). | |
`"0.25"` | Detects Python interpreter configuration for PyO3 integration. | |
`"0.9"` | Checks Rust compiler version for conditional compilation. |
[profile.dev]
Compiler settings for development builds.
Setting | Value | Description |
|---|---|---|
`codegen-units` | Number of codegen units, affecting build parallelism and optimization. | |
Debug information level. | ||
Enable debug assertions. | ||
`incremental` | Disable incremental compilation for consistency. | |
`lto` | `"off"` | Link Time Optimization off for faster build times. |
`3` | Optimization level (max for dev). | |
`overflow-checks` | Enable integer overflow checks. |
[profile.release]
Compiler settings for release builds.
Setting | Value | Description |
|---|---|---|
`codegen-units` | Single codegen unit for better optimization. | |
No debug info for smaller binary. | ||
`incremental` | Disable incremental builds for reproducibility. | |
`lto` | `"thin"` | Enables thin link-time optimization for better performance. |
`3` | Maximum optimization level. | |
`panic` | `"abort"` | Panic strategy set to abort for smaller binary and performance. |
[profile.release.build-override]
Overrides for builds triggered during the release build process.
Setting | Value | Description |
|---|---|---|
No optimization during build override. |
Important Implementation Details
Crate Type:
cdylib
The library is compiled as a C-compatible dynamic library to facilitate integration with Python via the PyO3 bindings. This enables Python code to load and call Rust functions directly.Feature Flags and Build Script Integration
Thefeaturessection declares features that are typically enabled or disabled by thebuild.rsscript based on environment detection (CPU architecture, Rust compiler version, Python environment). For example, advanced CPU instructions like AVX512 are enabled only if detected.Embedded C Code Integration
The manifest declares cc as a build dependency to compile the embeddedyyjsonC JSON parser. Theyyjsonfeature flag controls whether this backend is compiled and linked.Dependency Feature Control
Many dependencies have default-features = false to minimize unnecessary code, binary size, and potential conflicts. Specific features are enabled as needed, such as SIMD dispatch or float roundtrip correctness.Profiles Optimize for Different Use Cases
Thedevprofile favors debugging with assertions and debug info, while thereleaseprofile focuses on size and speed, using LTO and panic abort strategy.File Inclusion for Packaging
Theincludearray explicitly lists files and directories to be included when packaging the crate, ensuring all source files, tests, licenses, and documentation are bundled.
Usage Example
This file is not directly "used" in code but consumed by Cargo during build commands. Typical usage involves running:
cargo build --release
or when building Python wheels with maturin or other build tools that call Cargo internally. The manifest guides the build process, dependency resolution, and feature activation.
Interaction with Other Parts of the System
Build Script (
build.rs)
Reads and reacts to features defined here, enabling/disabling compilation of embedded C libraries and Rust features.Rust Source Code (
src/)
Uses conditional compilation flags and dependencies declared here to compile the core JSON serialization logic.Python Bindings (
pyo3integration)
Thecdylibcrate type and dependencies likepyo3-ffifacilitate Python interoperability.Embedded C Library (
include/yyjson)
The build system compiles this C code into the Rust crate if theyyjsonfeature is enabled.Build Tools (
maturin,cargo)
Use this file to configure builds, test runs, and packaging.Continuous Integration and Packaging
CI pipelines rely on this manifest for reproducible builds with locked dependencies and set compiler profiles.
Visual Diagram
flowchart TD
A[Cargo.toml Manifest]
B[Package Metadata]
C[Dependencies & Features]
D[Build Dependencies]
E[Profiles: dev & release]
F[Build Script (build.rs)]
G[Rust Core & Embedded C Libraries Compilation]
H[Python FFI Dynamic Library (cdylib)]
I[Packaging & Distribution]
A --> B
A --> C
A --> D
A --> E
C --> F
D --> F
F --> G
G --> H
H --> I
**Diagram Explanation:** The manifest (`Cargo.toml`) declares metadata, dependencies, features, build dependencies, and profiles. The build script (`build.rs`) reads feature flags and dependencies to conditionally compile Rust and embedded C code. The result is a dynamic library (`cdylib`) exposing Rust functions to Python. Finally, the artifact is packaged and distributed.
Summary
The `Cargo.toml` file is the declarative cornerstone of the **orjson** Rust package. It defines the package identity, manages dependencies and feature flags, controls build profiles, and integrates with build scripts to enable conditional compilation and embedding of the high-performance `yyjson` C JSON parser. By specifying dependencies with precision and tailoring compilation profiles, it ensures reproducible, optimized builds of the Rust core that seamlessly interface with Python. This manifest connects the Rust ecosystem, build tooling, and Python FFI, enabling the performant and correct JSON serialization library that **orjson** is known for.