Skip to content

cosmos: add experimental c wrapper around cosmos sdk #2906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

analogrelay
Copy link
Member

This is a bit of an experiment, but I'm game to merge it if @heaths and other Cosmos folks like it.

We want to explore the option of publishing a C library of the Azure Cosmos DB SDK for Rust. This PR adds the basic framework to start that effort:

  • A new package cosmosclient that depends on azure_data_cosmos. This package builds a cdylib and staticlib target (libcosmosclient.a/libcosmosclient.so/cosmosclient.dll/etc.) that exports a C-compatible API. For now, that API is extremely simple (cosmosclient_version).
  • A simple test infrastructure (NOT currently integrated into anything). There are C programs in sdk/cosmos/cosmosclient/c_tests that will be built and linked against the cosmosclient library. The tests are simple pass/fail programs. If the test returns a 0 exit code, it passed, otherwise, it failed.
  • build.rs script in cosmosclient to produce sdk/cosmos/cosmosclient/include/cosmosclient.h, which is a C header file defining the API surface of cosmosclient. We should publish this as an artifact at some point, but for now it's there to be used by tests.

@github-actions github-actions bot added the Cosmos The azure_cosmos crate label Aug 13, 2025
Cargo.toml Outdated
@@ -20,7 +20,7 @@ members = [
"sdk/template/azure_template_core",
"sdk/template/azure_template",
"sdk/storage/azure_storage_common",
"sdk/storage/azure_storage_blob",
"sdk/storage/azure_storage_blob", "sdk/cosmos/cosmosclient",
Copy link
Member

Choose a reason for hiding this comment

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

Separate line.

Copy link
Member Author

Choose a reason for hiding this comment

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

Cargo new just sticks it on the end. Disappointing. Will fix!

Copy link
Member

Choose a reason for hiding this comment

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

Make sure you install Even Better Toml and there are formatting instructions in the repo. Granted, you have to re-save it in VSCode.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, it's the resaving that got me. I didn't even open Cargo.toml 😅

@analogrelay analogrelay marked this pull request as ready for review August 13, 2025 20:33
@Copilot Copilot AI review requested due to automatic review settings August 13, 2025 20:33
Copy link
Contributor

@Copilot Copilot AI left a comment

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 adds an experimental C wrapper around the Azure Cosmos DB SDK for Rust, introducing a new cosmosclient package that exports a C-compatible API. The goal is to explore publishing a C library version of the Cosmos DB SDK.

Key changes:

  • New cosmosclient crate that builds both static and dynamic C libraries
  • C header generation using cbindgen with version information
  • Basic C test infrastructure using CMake for validation

Reviewed Changes

Copilot reviewed 10 out of 11 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
sdk/cosmos/cosmosclient/src/lib.rs Core library implementation with C-compatible API and version function
sdk/cosmos/cosmosclient/Cargo.toml Package configuration for building C libraries (cdylib/staticlib)
sdk/cosmos/cosmosclient/build.rs Build script for generating C headers and build identifiers
sdk/cosmos/cosmosclient/CMakeLists.txt CMake configuration for C test compilation and linking
sdk/cosmos/cosmosclient/c_tests/version.c C test program to validate version consistency
sdk/cosmos/azure_data_cosmos/Cargo.toml Added "c" feature for exposing additional types to C wrapper
Cargo.toml Added cosmosclient to workspace members

Copy link
Member

@LarryOsterman LarryOsterman left a comment

Choose a reason for hiding this comment

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

Just two minor nits.

@@ -0,0 +1,22 @@
[package]
name = "cosmosclient"
Copy link
Member

Choose a reason for hiding this comment

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

possibly cosmos_c_client for disambiguation?


const VERSION: &CStr = c_str!(env!("CARGO_PKG_VERSION"));

#[no_mangle]
Copy link
Member

Choose a reason for hiding this comment

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

This is a 100% style nit, but I generally prefer putting attributes after the rustdoc comment - since the attributes bind to the function I like putting them as close to the function declaration as I can.

Putting them before the rustdoc comment leads me to forget they exist :).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cosmos The azure_cosmos crate
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants