Skip to content
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
25 changes: 13 additions & 12 deletions scylla-rust-wrapper/src/argconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,24 @@ use std::os::raw::c_char;
use std::ptr::NonNull;
use std::sync::{Arc, Weak};

pub unsafe fn ptr_to_cstr(ptr: *const c_char) -> Option<&'static str> {
pub(crate) unsafe fn ptr_to_cstr(ptr: *const c_char) -> Option<&'static str> {
unsafe { CStr::from_ptr(ptr) }.to_str().ok()
}

pub unsafe fn ptr_to_cstr_n(ptr: *const c_char, size: size_t) -> Option<&'static str> {
pub(crate) unsafe fn ptr_to_cstr_n(ptr: *const c_char, size: size_t) -> Option<&'static str> {
if ptr.is_null() {
return None;
}
std::str::from_utf8(unsafe { std::slice::from_raw_parts(ptr as *const u8, size as usize) }).ok()
}

pub unsafe fn arr_to_cstr<const N: usize>(arr: &[c_char]) -> Option<&'static str> {
pub(crate) unsafe fn arr_to_cstr<const N: usize>(arr: &[c_char]) -> Option<&'static str> {
let null_char = '\0' as c_char;
let end_index = arr[..N].iter().position(|c| c == &null_char).unwrap_or(N);
unsafe { ptr_to_cstr_n(arr.as_ptr(), end_index as size_t) }
}

pub fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
pub(crate) fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
let mut result = ['\0' as c_char; N];

// Max length must be null-terminated
Expand All @@ -40,22 +40,22 @@ pub fn str_to_arr<const N: usize>(s: &str) -> [c_char; N] {
result
}

pub unsafe fn write_str_to_c(s: &str, c_str: *mut *const c_char, c_strlen: *mut size_t) {
pub(crate) unsafe fn write_str_to_c(s: &str, c_str: *mut *const c_char, c_strlen: *mut size_t) {
unsafe {
*c_str = s.as_ptr() as *const c_char;
*c_strlen = s.len() as u64;
}
}

pub unsafe fn strlen(ptr: *const c_char) -> size_t {
pub(crate) unsafe fn strlen(ptr: *const c_char) -> size_t {
if ptr.is_null() {
return 0;
}
unsafe { libc::strlen(ptr) as size_t }
}

#[cfg(test)]
pub fn str_to_c_str_n(s: &str) -> (*const c_char, size_t) {
pub(crate) fn str_to_c_str_n(s: &str) -> (*const c_char, size_t) {
let mut c_str = std::ptr::null();
let mut c_strlen = size_t::default();

Expand Down Expand Up @@ -198,7 +198,7 @@ impl<O: Ownership, CM: CMutability> Properties for (O, CM) {
///
/// ## Memory layout
/// We use repr(transparent), so the struct has the same layout as underlying [`Option<NonNull<T>>`].
/// Thanks to https://doc.rust-lang.org/std/option/#representation optimization,
/// Thanks to <https://doc.rust-lang.org/std/option/#representation optimization>,
/// we are guaranteed, that for `T: Sized`, our struct has the same layout
/// and function call ABI as simply [`NonNull<T>`].
#[repr(transparent)]
Expand Down Expand Up @@ -227,7 +227,7 @@ pub type CassBorrowedExclusivePtr<'a, T, CM> = CassPtr<'a, T, (Exclusive, CM)>;
/// and then another method accepts `const T*`.
#[cfg(test)]
impl<'a, T: Sized, P: Properties> CassPtr<'a, T, P> {
pub fn into_c_const(self) -> CassPtr<'a, T, (P::Onwership, CConst)> {
pub(crate) fn into_c_const(self) -> CassPtr<'a, T, (P::Onwership, CConst)> {
CassPtr {
ptr: self.ptr,
_phantom: PhantomData,
Expand Down Expand Up @@ -306,7 +306,7 @@ impl<T: Sized, P: Properties> CassPtr<'_, T, P> {
/// Resulting pointer inherits the lifetime from the immutable borrow
/// of original pointer.
#[allow(clippy::needless_lifetimes)]
pub fn borrow<'a>(&'a self) -> CassPtr<'a, T, (Shared, P::CMutability)> {
pub(crate) fn borrow<'a>(&'a self) -> CassPtr<'a, T, (Shared, P::CMutability)> {
CassPtr {
ptr: self.ptr,
_phantom: PhantomData,
Expand All @@ -320,7 +320,8 @@ impl<T: Sized> CassPtr<'_, T, (Exclusive, CMut)> {
/// of original pointer. Since the method accepts a mutable reference
/// to the original pointer, we enforce aliasing ^ mutability principle at compile time.
#[allow(clippy::needless_lifetimes)]
pub fn borrow_mut<'a>(&'a mut self) -> CassPtr<'a, T, (Exclusive, CMut)> {
#[cfg_attr(not(test), expect(unused))]
pub(crate) fn borrow_mut<'a>(&'a mut self) -> CassPtr<'a, T, (Exclusive, CMut)> {
CassPtr {
ptr: self.ptr,
_phantom: PhantomData,
Expand Down Expand Up @@ -413,7 +414,7 @@ pub trait BoxFFI: Sized + origin_sealed::FromBoxSealed {
/// The data should be allocated via [`Arc::new`], and then returned to the user as a pointer.
/// The user is responsible for freeing the memory associated
/// with the pointer using corresponding driver's API function.
pub trait ArcFFI: Sized + origin_sealed::FromArcSealed {
pub(crate) trait ArcFFI: Sized + origin_sealed::FromArcSealed {
/// Creates a pointer from a valid reference to Arc-allocated data.
/// Holder of the pointer borrows the pointee.
#[allow(clippy::needless_lifetimes)]
Expand Down
10 changes: 5 additions & 5 deletions scylla-rust-wrapper/src/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use std::convert::TryInto;
use std::sync::Arc;

pub struct CassBatch {
pub state: Arc<CassBatchState>,
pub batch_request_timeout_ms: Option<cass_uint64_t>,
pub(crate) state: Arc<CassBatchState>,
pub(crate) batch_request_timeout_ms: Option<cass_uint64_t>,

pub(crate) exec_profile: Option<PerStatementExecProfile>,
}
Expand All @@ -27,9 +27,9 @@ impl FFI for CassBatch {
}

#[derive(Clone)]
pub struct CassBatchState {
pub batch: Batch,
pub bound_values: Vec<Vec<MaybeUnset<Option<CassCqlValue>>>>,
pub(crate) struct CassBatchState {
pub(crate) batch: Batch,
pub(crate) bound_values: Vec<Vec<MaybeUnset<Option<CassCqlValue>>>>,
}

#[unsafe(no_mangle)]
Expand Down
4 changes: 2 additions & 2 deletions scylla-rust-wrapper/src/cass_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub(crate) use crate::cass_error_types::{CassError, CassErrorSource};
use crate::execution_error::CassErrorResult;
use crate::statement::UnknownNamedParameterError;

pub trait ToCassError {
pub(crate) trait ToCassError {
fn to_cass_error(&self) -> CassError;
}

Expand Down Expand Up @@ -223,7 +223,7 @@ impl ToCassError for SerializationError {
}
}

pub trait CassErrorMessage {
pub(crate) trait CassErrorMessage {
fn msg(&self) -> String;
}

Expand Down
52 changes: 22 additions & 30 deletions scylla-rust-wrapper/src/cass_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ pub(crate) use crate::cass_data_types::CassValueType;

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub struct UdtDataType {
pub(crate) struct UdtDataType {
// Vec to preserve the order of types
pub field_types: Vec<(String, Arc<CassDataType>)>,
pub(crate) field_types: Vec<(String, Arc<CassDataType>)>,

pub keyspace: String,
pub name: String,
pub frozen: bool,
pub(crate) keyspace: String,
pub(crate) name: String,
pub(crate) frozen: bool,
}

impl UdtDataType {
pub fn new() -> UdtDataType {
pub(crate) fn new() -> UdtDataType {
UdtDataType {
field_types: Vec::new(),
keyspace: "".to_string(),
Expand All @@ -35,7 +35,7 @@ impl UdtDataType {
}
}

pub fn with_capacity(capacity: usize) -> UdtDataType {
pub(crate) fn with_capacity(capacity: usize) -> UdtDataType {
UdtDataType {
field_types: Vec::with_capacity(capacity),
keyspace: "".to_string(),
Expand All @@ -44,21 +44,13 @@ impl UdtDataType {
}
}

pub fn add_field(&mut self, name: String, field_type: Arc<CassDataType>) {
self.field_types.push((name, field_type));
}

pub fn get_field_by_name(&self, name: &str) -> Option<&Arc<CassDataType>> {
pub(crate) fn get_field_by_name(&self, name: &str) -> Option<&Arc<CassDataType>> {
self.field_types
.iter()
.find(|(field_name, _)| field_name == name)
.map(|(_, t)| t)
}

pub fn get_field_by_index(&self, index: usize) -> Option<&Arc<CassDataType>> {
self.field_types.get(index).map(|(_, b)| b)
}

fn typecheck_equals(&self, other: &UdtDataType) -> bool {
// See: https://github.com/scylladb/cpp-driver/blob/master/src/data_type.hpp#L354-L386

Expand Down Expand Up @@ -109,21 +101,21 @@ impl Default for UdtDataType {

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum MapDataType {
pub(crate) enum MapDataType {
Untyped,
Key(Arc<CassDataType>),
KeyAndValue(Arc<CassDataType>, Arc<CassDataType>),
}

#[derive(Debug)]
pub struct CassColumnSpec {
pub name: String,
pub data_type: Arc<CassDataType>,
pub(crate) struct CassColumnSpec {
pub(crate) name: String,
pub(crate) data_type: Arc<CassDataType>,
}

#[derive(Clone, Debug)]
#[cfg_attr(test, derive(PartialEq, Eq))]
pub enum CassDataTypeInner {
pub(crate) enum CassDataTypeInner {
Value(CassValueType),
Udt(UdtDataType),
List {
Expand Down Expand Up @@ -153,7 +145,7 @@ impl CassDataTypeInner {
/// Checks for equality during typechecks.
///
/// This takes into account the fact that tuples/collections may be untyped.
pub fn typecheck_equals(&self, other: &CassDataTypeInner) -> bool {
pub(crate) fn typecheck_equals(&self, other: &CassDataTypeInner) -> bool {
match self {
CassDataTypeInner::Value(t) => *t == other.get_value_type(),
CassDataTypeInner::Udt(udt) => match other {
Expand Down Expand Up @@ -248,20 +240,20 @@ impl Eq for CassDataType {}
unsafe impl Sync for CassDataType {}

impl CassDataType {
pub unsafe fn get_unchecked(&self) -> &CassDataTypeInner {
pub(crate) unsafe fn get_unchecked(&self) -> &CassDataTypeInner {
unsafe { &*self.0.get() }
}

#[allow(clippy::mut_from_ref)]
pub unsafe fn get_mut_unchecked(&self) -> &mut CassDataTypeInner {
pub(crate) unsafe fn get_mut_unchecked(&self) -> &mut CassDataTypeInner {
unsafe { &mut *self.0.get() }
}

pub const fn new(inner: CassDataTypeInner) -> CassDataType {
pub(crate) const fn new(inner: CassDataTypeInner) -> CassDataType {
CassDataType(UnsafeCell::new(inner))
}

pub fn new_arced(inner: CassDataTypeInner) -> Arc<CassDataType> {
pub(crate) fn new_arced(inner: CassDataTypeInner) -> Arc<CassDataType> {
Arc::new(CassDataType(UnsafeCell::new(inner)))
}
}
Expand Down Expand Up @@ -366,14 +358,14 @@ impl CassDataTypeInner {
}
}

pub fn get_udt_type(&self) -> &UdtDataType {
pub(crate) fn get_udt_type(&self) -> &UdtDataType {
match self {
CassDataTypeInner::Udt(udt) => udt,
_ => panic!("Can get UDT out of non-UDT data type"),
}
}

pub fn get_value_type(&self) -> CassValueType {
pub(crate) fn get_value_type(&self) -> CassValueType {
match &self {
CassDataTypeInner::Value(value_data_type) => *value_data_type,
CassDataTypeInner::Udt { .. } => CassValueType::CASS_VALUE_TYPE_UDT,
Expand All @@ -386,7 +378,7 @@ impl CassDataTypeInner {
}
}

pub fn get_column_type(column_type: &ColumnType) -> CassDataType {
pub(crate) fn get_column_type(column_type: &ColumnType) -> CassDataType {
use CollectionType::*;
use ColumnType::*;
let inner = match column_type {
Expand Down Expand Up @@ -935,7 +927,7 @@ impl TryFrom<CassConsistency> for SerialConsistency {
}
}

pub fn make_batch_type(type_: CassBatchType) -> Option<BatchType> {
pub(crate) fn make_batch_type(type_: CassBatchType) -> Option<BatchType> {
match type_ {
CassBatchType::CASS_BATCH_TYPE_LOGGED => Some(BatchType::Logged),
CassBatchType::CASS_BATCH_TYPE_UNLOGGED => Some(BatchType::Unlogged),
Expand Down
3 changes: 2 additions & 1 deletion scylla-rust-wrapper/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub struct CassCustomPayload;

// We want to make sure that the returned future does not depend
// on the provided &CassCluster, hence the `static here.
pub fn build_session_builder(
pub(crate) fn build_session_builder(
cluster: &CassCluster,
) -> impl Future<Output = SessionBuilder> + 'static {
let known_nodes = cluster
Expand Down Expand Up @@ -1194,6 +1194,7 @@ pub unsafe extern "C" fn cass_cluster_set_compression(
let compression = match compression_type {
CassCompressionType::CASS_COMPRESSION_LZ4 => Some(Compression::Lz4),
CassCompressionType::CASS_COMPRESSION_SNAPPY => Some(Compression::Snappy),
CassCompressionType::CASS_COMPRESSION_NONE => None,
_ => None,
};

Expand Down
11 changes: 4 additions & 7 deletions scylla-rust-wrapper/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ static UNTYPED_MAP_TYPE: LazyLock<Arc<CassDataType>> = LazyLock::new(|| {

#[derive(Clone)]
pub struct CassCollection {
pub collection_type: CassCollectionType,
pub data_type: Option<Arc<CassDataType>>,
pub capacity: usize,
pub items: Vec<CassCqlValue>,
pub(crate) collection_type: CassCollectionType,
pub(crate) data_type: Option<Arc<CassDataType>>,
pub(crate) items: Vec<CassCqlValue>,
}

impl FFI for CassCollection {
Expand Down Expand Up @@ -90,7 +89,7 @@ impl CassCollection {
CassError::CASS_OK
}

pub fn append_cql_value(&mut self, value: Option<CassCqlValue>) -> CassError {
pub(crate) fn append_cql_value(&mut self, value: Option<CassCqlValue>) -> CassError {
let err = self.typecheck_on_append(&value);
if err != CassError::CASS_OK {
return err;
Expand Down Expand Up @@ -150,7 +149,6 @@ pub unsafe extern "C" fn cass_collection_new(
BoxFFI::into_ptr(Box::new(CassCollection {
collection_type,
data_type: None,
capacity,
items: Vec::with_capacity(capacity),
}))
}
Expand Down Expand Up @@ -182,7 +180,6 @@ unsafe extern "C" fn cass_collection_new_from_data_type(
BoxFFI::into_ptr(Box::new(CassCollection {
collection_type,
data_type: Some(data_type),
capacity,
items: Vec::with_capacity(capacity),
}))
}
Expand Down
4 changes: 2 additions & 2 deletions scylla-rust-wrapper/src/exec_profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ impl CassExecProfile {

/// Represents a non-empty execution profile name.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ExecProfileName(String);
pub(crate) struct ExecProfileName(String);

#[derive(Debug, PartialEq, Eq)]
pub struct EmptyProfileName;
pub(crate) struct EmptyProfileName;

impl TryFrom<String> for ExecProfileName {
type Error = EmptyProfileName;
Expand Down
Loading