Skip to content

Initial C Bindings for Cosmos DB SDK for Rust - #3347

Merged
Ashley Stanton-Nurse (analogrelay) merged 30 commits into
Azure:mainfrom
analogrelay:ashleyst/port-c-bindings
Jan 8, 2026
Merged

Initial C Bindings for Cosmos DB SDK for Rust#3347
Ashley Stanton-Nurse (analogrelay) merged 30 commits into
Azure:mainfrom
analogrelay:ashleyst/port-c-bindings

Conversation

@analogrelay

@analogrelay Ashley Stanton-Nurse (analogrelay) commented Nov 14, 2025

Copy link
Copy Markdown
Member

This PR introduces C language bindings (FFI) for the azure_data_cosmos Rust SDK, enabling C/C++ applications to interact with Azure Cosmos DB.

Key Changes

New azure_data_cosmos_native Package:

  • Complete C API with FFI layer exposing core Cosmos DB operations (originally developed by a partner team)
  • Runtime and call context management for async/sync bridging. The Runtime Context defines the threads on which async operations will run, and the Call Context provides a place for call-specific reporting of error details. This is based on the pattern found in the AMQP package for C++
  • Comprehensive error handling with optional detailed error information
  • CRUD operations for databases, containers, and items
  • Connection string support

C Tests:

  • Shared test infrastructure (test_common.h) with automatic test discovery
  • Basic tests of item/container/database CRUD.
  • Integration with CMake/CTest

Build System:

  • CMake build configuration with organized output directories
  • cbindgen integration for automatic header generation
  • pkg-config file generation for library integration
  • Cross-platform support (Linux, macOS, Windows)

Closes #3444

@github-actions github-actions Bot added Azure.Core The azure_core crate Cosmos The azure_cosmos crate labels Nov 14, 2025
@github-actions

github-actions Bot commented Nov 14, 2025

Copy link
Copy Markdown

API Change Check

APIView identified API level changes in this PR and created the following API reviews

azure_data_cosmos

Comment thread sdk/core/typespec_client_core/src/http/clients/reqwest.rs Outdated
@analogrelay Ashley Stanton-Nurse (analogrelay) changed the title [WIP] Initial C Bindings for Cosmos DB SDK for Rust Initial C Bindings for Cosmos DB SDK for Rust Nov 18, 2025
@analogrelay
Ashley Stanton-Nurse (analogrelay) marked this pull request as ready for review November 20, 2025 00:03
Copilot AI review requested due to automatic review settings November 20, 2025 00:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces C bindings for the Azure Cosmos DB SDK for Rust, allowing the SDK to be used from C and other languages. Key changes include:

  • New FFI layer with runtime context, call context, and error handling infrastructure
  • C bindings for CosmosClient, DatabaseClient, and ContainerClient
  • Support for CRUD operations, queries, and resource management
  • Auto-generated C header file (azurecosmos.h)
  • Comprehensive C test suite covering CRUD operations, error handling, and memory management

Reviewed Changes

Copilot reviewed 22 out of 23 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
src/string.rs String utilities and FFI string conversion helpers
src/runtime/tokio.rs Tokio runtime wrapper for async operations
src/runtime/mod.rs Runtime context FFI functions
src/options/mod.rs Empty option structs for future extensibility
src/lib.rs Main library entry, version function, and tracing setup
src/error.rs Error types, codes, and conversion from Azure SDK errors
src/context.rs Call context for FFI operations and helper macros
src/clients/mod.rs Client module exports
src/clients/database_client.rs Database operations FFI functions
src/clients/cosmos_client.rs Cosmos client creation and database operations
src/clients/container_client.rs Container and item operations FFI functions
include/azurecosmos.h Auto-generated C header file
c_tests/*.c C test suite for FFI functionality
build.rs cbindgen configuration for header generation
Cargo.toml Dependencies and features for native bindings
CMakeLists.txt CMake build configuration for C tests
Comments suppressed due to low confidence (1)

sdk/cosmos/azure_data_cosmos_native/src/string.rs:37

  • The safe_cstring_new function calls expect() which can panic, but this panic behavior is not documented. Add a # Panics section to the function documentation explaining when this function panics (when the string contains interior NUL bytes).

Comment thread sdk/cosmos/azure_data_cosmos_native/src/options/mod.rs Outdated
Comment thread sdk/cosmos/azure_data_cosmos_native/src/options/mod.rs Outdated
Comment thread sdk/cosmos/azure_data_cosmos_native/src/clients/container_client.rs Outdated
Comment thread sdk/cosmos/azure_data_cosmos_native/c_tests/context_memory_management.c Outdated
Comment thread sdk/cosmos/azure_data_cosmos_native/CMakeLists.txt
@analogrelay

Copy link
Copy Markdown
Member Author

/azp run rust - pullrequest

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

Comment thread sdk/cosmos/azure_data_cosmos_native/include/azurecosmos.h

@tvaron3 Tomas Varon (tvaron3) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@analogrelay

Copy link
Copy Markdown
Member Author

/azp run rust - pullrequest

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@analogrelay

Copy link
Copy Markdown
Member Author

Hopefully, a simple rebuild should catch the fixes from #3461 and give us a green build 🤞🏻

@heaths Heath Stewart (heaths) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Signing off on Cargo.lock (only non-Cosmos change) and to unblock the PR.

@analogrelay
Ashley Stanton-Nurse (analogrelay) merged commit 583455e into Azure:main Jan 8, 2026
17 checks passed
Ashley Stanton-Nurse (analogrelay) added a commit that referenced this pull request Jan 9, 2026
This PR just deletes the `cosmosclient.h` header file, which was the
name prior to #3347 . I just forgot to properly delete the old file in
that PR.
Charles Lowell (chlowell) pushed a commit that referenced this pull request Jan 16, 2026
This PR introduces C language bindings (FFI) for the `azure_data_cosmos`
Rust SDK, enabling C/C++ applications to interact with Azure Cosmos DB.

### Key Changes

**New `azure_data_cosmos_native` Package:**
- Complete C API with FFI layer exposing core Cosmos DB operations
(originally developed by a partner team)
- Runtime and call context management for async/sync bridging. The
Runtime Context defines the threads on which async operations will run,
and the Call Context provides a place for call-specific reporting of
error details. This is based on the pattern found in the [AMQP
package](https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/core/azure-core-amqp/src/impl/rust_amqp/rust_amqp/rust_wrapper/src/call_context.rs)
for C++
- Comprehensive error handling with optional detailed error information
- CRUD operations for databases, containers, and items
- Connection string support

**C Tests:**
- Shared test infrastructure (`test_common.h`) with automatic test
discovery
- Basic tests of item/container/database CRUD.
- Integration with CMake/CTest

**Build System:**
- CMake build configuration with organized output directories
- `cbindgen` integration for automatic header generation
- pkg-config file generation for library integration
- Cross-platform support (Linux, macOS, Windows)

Closes #3444
Charles Lowell (chlowell) pushed a commit that referenced this pull request Jan 16, 2026
This PR just deletes the `cosmosclient.h` header file, which was the
name prior to #3347 . I just forgot to properly delete the old file in
that PR.
Debdatta Kunda (kundadebdatta) pushed a commit that referenced this pull request May 23, 2026
…PER_SPEC

Blocking fixes:
- §6 / §4.7: bind cosmos_driver_execute to execute_singleton_operation,
  document Result<Option<CosmosResponse>> handling and FEED_EXHAUSTED.
- §4.6: remove cosmos_operation_with_partition_key; PK now lives on item /
  feed factory args (ItemReference::from_name requires PK at construction).
- §4.4: add normative driver-cache documentation (endpoint-only key,
  options dropped on cache hit, credential collision caveat) plus a new
  COSMOS_ERROR_CODE_OPTIONS_IGNORED_ON_CACHE_HIT advisory.
- §3.5 / §6: rewrite error model for azure_data_cosmos::Error from #4442
  (Kind enum, typed accessors, predicates, synthetic sub-status codes,
  non_exhaustive future-proofing via COSMOS_ERROR_KIND_UNKNOWN).
- §4.2: fix DriverOptions surface to mirror the actual 3-field type;
  builder takes account, with_operation_options for per-call defaults;
  removed allow_emulator_invalid_certs (lives on runtime instead).
- §5.2: fix Cargo features (default = tokio + rustls; drop fake
  reqwest_native_tls / tracing feature; native_tls is the correct name).
- §4.6.3: normative execute-consumption contract (sentinel, free always
  safe, failed execute does not consume, double-execute returns 4005).

Recommended fixes:
- §4.6.2: split with_precondition into IF_MATCH / IF_NONE_MATCH with a
  PRECONDITION_ALREADY_SET error to enforce single-precondition rule.
- §4.3: document resource-token routing via master-key Secret path; add
  with_credential mirror.
- §4.6.1: add missing factories — read_all_items_cross_partition,
  query_items, batch, query/read/replace_offer.
- §4.3 / §4.5 / §3.4: define cosmos_*_clone functions promised in §3.4.
- §5.3: Phase 0 ancillary-tooling re-introduction checklist for entries
  PR #4103 removed (cbindgen as [build-dependencies] only per heaths,
  dict files, .cspell.json, verify-dependencies.rs, AGENTS.md, skills).
- §3 / §5.1: tighten inheritance attribution between #2906 and #3347.
- §7: loosen strict ABI version equality to major-equal / minor->= so the
  spec's additive-growth promise actually holds.
- §2.2: add cbindgen export.rename / item_types policy so the generated
  header matches the naming table (avoids cosmos_cosmos_* double-prefix
  and prevents driver-internal types from leaking).

Other:
- §9: replaced single open question on RuntimeAlreadyInitialized with 9
  questions covering implementation parking-lot items (header-visitor
  borrow vs copy, continuation-token format, multi-part body, C++
  companion header, credential identity in cache, ConnectionString
  parser ownership, symbol stripping, pager continuation-token resume).
- §8: updated Phase 0 / 1 / 2 / 3 / 5 / 6 acceptance criteria to match
  the new contracts (header check-in, error accessor coverage, cache-hit
  advisory test, error-mapped 404 surfaces as is_not_found).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Azure.Core The azure_core crate Cosmos The azure_cosmos crate

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cosmos: Create libazurecosmos, a C library wrapping the Rust SDK

5 participants