Skip to content

build: generate bindings for basic types #188

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

Merged
merged 15 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions scylla-rust-wrapper/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,25 @@ fn prepare_basic_types(out_path: &Path) {
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
.layout_tests(true)
.generate_comments(false)
.allowlist_type("cass_bool_t")
// cass_bool_t enum variants represented as constants.
.constified_enum("cass_bool_t")
// cass_[false/true] instead of cass_bool_t_cass_[*].
.prepend_enum_name(false)
.allowlist_type("cass_float_t")
.allowlist_type("cass_double_t")
.allowlist_type("cass_int8_t")
.allowlist_type("cass_uint8_t")
.allowlist_type("cass_int16_t")
.allowlist_type("cass_uint16_t")
.allowlist_type("cass_int32_t")
.allowlist_type("cass_uint32_t")
.allowlist_type("cass_int64_t")
.allowlist_type("cass_uint64_t")
.allowlist_type("cass_byte_t")
.allowlist_type("cass_duration_t")
.allowlist_type("size_t")
.size_t_is_usize(false)
.generate()
.expect("Unable to generate bindings");

Expand Down Expand Up @@ -67,22 +85,29 @@ fn main() {
prepare_basic_types(&out_path);

prepare_cppdriver_data(
"cppdriver_data_errors.rs",
"cppdriver_error_types.rs",
&[
"CassErrorSource_",
"CassErrorSource",
"CassError_",
"CassError",
"CassWriteType",
"CassWriteType_",
],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_consistency_types.rs",
&["CassConsistency_", "CassConsistency"],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_batch_types.rs",
&["CassBatchType_", "CassBatchType"],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_data_collection.rs",
"cppdriver_collection_types.rs",
&["CassCollectionType_", "CassCollectionType"],
&out_path,
);
Expand All @@ -92,12 +117,12 @@ fn main() {
&out_path,
);
prepare_cppdriver_data(
"cppdriver_data_inet.rs",
"cppdriver_inet_types.rs",
&["CassInet_", "CassInet"],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_log.rs",
"cppdriver_log_types.rs",
&[
"CassLogLevel_",
"CassLogLevel",
Expand All @@ -107,17 +132,7 @@ fn main() {
&out_path,
);
prepare_cppdriver_data(
"cppdriver_data_query_error.rs",
&[
"CassConsistency_",
"CassConsistency",
"CassWriteType",
"CassWriteType_",
],
&out_path,
);
prepare_cppdriver_data(
"cppdriver_data_uuid.rs",
"cppdriver_uuid_types.rs",
&["CassUuid_", "CassUuid"],
&out_path,
);
Expand Down
3 changes: 2 additions & 1 deletion scylla-rust-wrapper/src/cass_error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use scylla::transport::errors::*;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_errors.rs"));
// Re-export error types.
pub(crate) use crate::cass_error_types::{CassError, CassErrorSource};

impl From<&QueryError> for CassError {
fn from(error: &QueryError) -> Self {
Expand Down
6 changes: 3 additions & 3 deletions scylla-rust-wrapper/src/cass_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ use std::os::raw::c_char;
use std::ptr;
use std::sync::Arc;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_types.rs"));
include!(concat!(env!("OUT_DIR"), "/cppdriver_data_query_error.rs"));
include!(concat!(env!("OUT_DIR"), "/cppdriver_batch_types.rs"));
pub(crate) use crate::cass_batch_types::CassBatchType;
pub(crate) use crate::cass_consistency_types::CassConsistency;
pub(crate) use crate::cass_data_types::CassValueType;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UDTDataType {
Expand Down
2 changes: 1 addition & 1 deletion scylla-rust-wrapper/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use std::os::raw::{c_char, c_int, c_uint};
use std::sync::Arc;
use std::time::Duration;

include!(concat!(env!("OUT_DIR"), "/cppdriver_compression_types.rs"));
use crate::cass_compression_types::CassCompressionType;

// According to `cassandra.h` the default CPP driver's
// - consistency for statements is LOCAL_ONE,
Expand Down
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/collection.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::cass_collection_types::CassCollectionType;
use crate::cass_error::CassError;
use crate::cass_types::{CassDataType, MapDataType};
use crate::types::*;
Expand All @@ -6,8 +7,6 @@ use crate::{argconv::*, value};
use std::convert::TryFrom;
use std::sync::Arc;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_collection.rs"));

// These constants help us to save an allocation in case user calls `cass_collection_new` (untyped collection).
static UNTYPED_LIST_TYPE: CassDataType = CassDataType::List {
typ: None,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,4 @@
#![allow(non_camel_case_types)]

// Definition for size_t (and possibly other types in the future)
include!(concat!(env!("OUT_DIR"), "/basic_types.rs"));

pub type cass_bool_t = ::std::os::raw::c_uint;
pub type cass_float_t = f32;
pub type cass_double_t = f64;
pub type cass_int8_t = i8;
pub type cass_uint8_t = u8;
pub type cass_int16_t = i16;
pub type cass_uint16_t = u16;
pub type cass_int32_t = i32;
pub type cass_uint32_t = u32;
pub type cass_int64_t = i64;
pub type cass_uint64_t = u64;
pub type cass_byte_t = cass_uint8_t;
pub type cass_duration_t = cass_uint64_t;
pub type size_t = cass_uint64_t;
use crate::types::{cass_int64_t, cass_uint32_t};

// Implementation directly ported from Cpp Driver implementation:

Expand Down Expand Up @@ -45,8 +27,3 @@ pub unsafe extern "C" fn cass_date_time_to_epoch(
(((date as u64) - CASS_DATE_EPOCH) * (NUM_SECONDS_PER_DAY as u64)
+ ((time / CASS_TIME_NANOSECONDS_PER_SECOND) as u64)) as i64
}

#[allow(non_upper_case_globals)]
pub const cass_false: cass_bool_t = 0;
#[allow(non_upper_case_globals)]
pub const cass_true: cass_bool_t = 1;
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/inet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(non_camel_case_types, non_snake_case)]
use crate::argconv::*;
use crate::cass_error::CassError;
use crate::types::*;
Expand All @@ -11,7 +10,7 @@ use std::os::raw::c_char;
use std::slice::from_raw_parts;
use std::str::FromStr;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_inet.rs"));
pub(crate) use crate::cass_inet_types::CassInet;

#[repr(u8)] // address_length field in CassInet is cass_uint8_t
#[allow(non_camel_case_types)]
Expand Down
75 changes: 74 additions & 1 deletion scylla-rust-wrapper/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod cass_error;
pub mod cass_types;
pub mod cluster;
pub mod collection;
pub mod date_time;
pub mod exec_profile;
mod external;
pub mod future;
Expand All @@ -32,11 +33,83 @@ pub mod statement;
#[cfg(test)]
pub mod testing;
pub mod tuple;
pub mod types;
pub mod user_type;
pub mod uuid;
pub mod value;

/// Includes a file generated by bindgen called `filename`.
macro_rules! include_bindgen_generated {
($filename:expr) => {
include!(concat!(env!("OUT_DIR"), '/', $filename));
};
}

/// All numeric types are defined here.
pub mod types {
#![allow(non_camel_case_types)]
// for `cass_false` and `cass_true` globals.
#![allow(non_upper_case_globals)]

// Definition for size_t (and possibly other types in the future)
include_bindgen_generated!("basic_types.rs");
}

/// CassError, CassErrorSource, CassWriteType
pub mod cass_error_types {
include_bindgen_generated!("cppdriver_error_types.rs");
}

/// CassValueType
pub mod cass_data_types {
include_bindgen_generated!("cppdriver_data_types.rs");
}

/// CassConsistency
pub mod cass_consistency_types {
include_bindgen_generated!("cppdriver_consistency_types.rs");
}

/// CassBatchType
pub mod cass_batch_types {
include_bindgen_generated!("cppdriver_batch_types.rs");
}

/// CassCompressionType
pub mod cass_compression_types {
include_bindgen_generated!("cppdriver_compression_types.rs");
}

/// CassCollectionType
pub mod cass_collection_types {
include_bindgen_generated!("cppdriver_collection_types.rs");
}

/// CassInet
pub mod cass_inet_types {
#![allow(non_camel_case_types, non_snake_case)]

include_bindgen_generated!("cppdriver_inet_types.rs");
}

/// CassLogLevel, CassLogMessage
pub mod cass_log_types {
#![allow(non_camel_case_types, non_snake_case)]

include_bindgen_generated!("cppdriver_log_types.rs");
}

/// CassColumnType
pub mod cass_column_types {
include_bindgen_generated!("cppdriver_column_type.rs");
}

/// CassUuid
pub mod cass_uuid_types {
#![allow(non_camel_case_types, non_snake_case)]

include_bindgen_generated!("cppdriver_uuid_types.rs");
}

lazy_static! {
pub static ref RUNTIME: Runtime = Runtime::new().unwrap();
pub static ref LOGGER: RwLock<Logger> = RwLock::new(Logger {
Expand Down
7 changes: 1 addition & 6 deletions scylla-rust-wrapper/src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::argconv::{arr_to_cstr, ptr_to_cstr, ptr_to_ref, str_to_arr};
use crate::cass_log_types::{CassLogLevel, CassLogMessage};
use crate::types::size_t;
use crate::LOGGER;
use std::convert::TryFrom;
Expand All @@ -13,12 +14,6 @@ use tracing_subscriber::layer::Context;
use tracing_subscriber::prelude::*;
use tracing_subscriber::Layer;

mod cass_log {
#![allow(non_camel_case_types, non_snake_case)]
include!(concat!(env!("OUT_DIR"), "/cppdriver_log.rs"));
}
use cass_log::*;

pub type CassLogCallback =
Option<unsafe extern "C" fn(message: *const CassLogMessage, data: *mut c_void)>;

Expand Down
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/metadata.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::argconv::*;
use crate::cass_column_types::CassColumnType;
use crate::cass_types::get_column_type_from_cql_type;
use crate::cass_types::CassDataType;
use crate::types::*;
Expand All @@ -8,8 +9,6 @@ use std::os::raw::c_char;
use std::sync::Arc;
use std::sync::Weak;

include!(concat!(env!("OUT_DIR"), "/cppdriver_column_type.rs"));

pub struct CassSchemaMeta {
pub keyspaces: HashMap<String, CassKeyspaceMeta>,
}
Expand Down
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/misc.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::ffi::{c_char, CStr};

// CassConsistency definition.
include!(concat!(env!("OUT_DIR"), "/cppdriver_data_query_error.rs"));
use crate::{cass_error_types::CassWriteType, cass_types::CassConsistency};

impl CassConsistency {
pub(crate) fn as_cstr(&self) -> &'static CStr {
Expand Down
4 changes: 2 additions & 2 deletions scylla-rust-wrapper/src/query_error.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::argconv::*;
use crate::cass_error::*;
use crate::cass_error_types::CassWriteType;
use crate::cass_types::CassConsistency;
use crate::types::*;
use scylla::statement::Consistency;
use scylla::transport::errors::*;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_query_error.rs"));

pub type CassErrorResult = QueryError;

impl From<Consistency> for CassConsistency {
Expand Down
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/statement.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::cass_error::CassError;
use crate::cass_types::CassConsistency;
use crate::exec_profile::PerStatementExecProfile;
use crate::prepared::CassPrepared;
use crate::query_result::CassResult;
Expand All @@ -18,8 +19,6 @@ use std::os::raw::{c_char, c_int};
use std::slice;
use std::sync::Arc;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_query_error.rs"));

#[derive(Clone)]
pub enum Statement {
Simple(SimpleQuery),
Expand Down
3 changes: 1 addition & 2 deletions scylla-rust-wrapper/src/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![allow(non_camel_case_types, non_snake_case)]
use crate::argconv::*;
use crate::cass_error::CassError;
use crate::types::*;
Expand All @@ -11,7 +10,7 @@ use std::sync::atomic::{AtomicU64, Ordering};
use std::time::{SystemTime, UNIX_EPOCH};
use uuid::Uuid;

include!(concat!(env!("OUT_DIR"), "/cppdriver_data_uuid.rs"));
pub(crate) use crate::cass_uuid_types::CassUuid;

pub struct CassUuidGen {
pub clock_seq_and_node: cass_uint64_t,
Expand Down