Skip to content

Commit 3007cb0

Browse files
authored
initial port of c bindings
1 parent 6d60897 commit 3007cb0

File tree

14 files changed

+2233
-5
lines changed

14 files changed

+2233
-5
lines changed

Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sdk/cosmos/.dict.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ udfs
88
backoff
99
pluggable
1010
cloneable
11+
upsert
12+
upserts
1113

1214
# Cosmos' docs all use "Autoscale" as a single word, rather than a compound "AutoScale" or "Auto Scale"
1315
autoscale
1416

1517
# Words used within the Cosmos Native Client (azure_data_cosmos_native)
1618
azurecosmos
1719
cosmosclient
20+
cstring

sdk/cosmos/azure_data_cosmos/src/models/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ pub struct SystemProperties {
9898
///
9999
/// Returned by [`DatabaseClient::read()`](crate::clients::DatabaseClient::read()).
100100
#[non_exhaustive]
101-
#[derive(Clone, Default, Debug, Deserialize, PartialEq, Eq)]
101+
#[derive(Clone, Default, Debug, Deserialize, Serialize, PartialEq, Eq)]
102102
pub struct DatabaseProperties {
103103
/// The ID of the database.
104104
pub id: String,

sdk/cosmos/azure_data_cosmos_native/Cargo.toml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,15 @@ name = "azurecosmos"
1414
crate-type = ["cdylib", "staticlib"]
1515

1616
[dependencies]
17-
azure_data_cosmos = { path = "../azure_data_cosmos" }
17+
futures.workspace = true
18+
tokio = { workspace = true, optional = true, features = ["rt-multi-thread", "macros"] }
19+
serde_json = { workspace = true, features = ["raw_value"] }
20+
azure_core.workspace = true
21+
azure_data_cosmos = { path = "../azure_data_cosmos", features = [ "key_auth", "preview_query_engine" ] }
22+
23+
[features]
24+
default = ["tokio"]
25+
tokio = ["dep:tokio"]
1826

1927
[build-dependencies]
2028
cbindgen = "0.29.0"

sdk/cosmos/azure_data_cosmos_native/build.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,13 @@ fn main() {
3636
"\n// Specifies the version of cosmosclient this header file was generated from.\n// This should match the version of libcosmosclient you are referencing.\n#define COSMOSCLIENT_H_VERSION \"{}\"",
3737
env!("CARGO_PKG_VERSION")
3838
))
39+
.with_style(cbindgen::Style::Both)
40+
.rename_item("CosmosClientHandle", "CosmosClient")
41+
.rename_item("DatabaseClientHandle", "DatabaseClient")
42+
.rename_item("ContainerClientHandle", "ContainerClient")
3943
.with_cpp_compat(true)
4044
.with_header(header)
4145
.generate()
4246
.expect("unable to generate bindings")
43-
.write_to_file("include/cosmosclient.h");
47+
.write_to_file("include/azurecosmos.h");
4448
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
use std::{future::Future, sync::OnceLock};
2+
use tokio::runtime::Runtime;
3+
4+
// Centralized runtime - single instance per process (following Azure SDK pattern)
5+
// https://github.com/Azure/azure-sdk-for-cpp/blob/main/sdk/core/azure-core-amqp/src/impl/rust_amqp/rust_amqp/rust_wrapper/src/amqp/connection.rs#L100-L107
6+
pub static RUNTIME: OnceLock<Runtime> = OnceLock::new();
7+
8+
pub fn block_on<F>(future: F) -> F::Output
9+
where
10+
F: Future,
11+
{
12+
let runtime = RUNTIME.get_or_init(|| Runtime::new().expect("Failed to create Tokio runtime"));
13+
runtime.block_on(future)
14+
}
15+
16+
#[cfg(test)]
17+
mod tests {
18+
use super::*;
19+
20+
#[test]
21+
fn test_blocking_runtime_initialization() {
22+
let result1 = block_on(async { 42 });
23+
let result2 = block_on(async { 24 });
24+
25+
assert_eq!(result1, 42);
26+
assert_eq!(result2, 24);
27+
}
28+
29+
#[test]
30+
fn test_blocking_async_operation() {
31+
let result = block_on(async {
32+
tokio::time::sleep(tokio::time::Duration::from_millis(1)).await;
33+
"completed"
34+
});
35+
36+
assert_eq!(result, "completed");
37+
}
38+
39+
#[test]
40+
fn test_runtime_singleton() {
41+
block_on(async { 1 });
42+
let runtime1 = RUNTIME.get();
43+
44+
block_on(async { 2 });
45+
let runtime2 = RUNTIME.get();
46+
47+
assert!(runtime1.is_some());
48+
assert!(runtime2.is_some());
49+
assert!(std::ptr::eq(runtime1.unwrap(), runtime2.unwrap()));
50+
}
51+
}

0 commit comments

Comments
 (0)