Skip to content

Commit 7b8bb5a

Browse files
committed
argconv: make FFI implementation mutually exclusive
Disallow implementing two different APIs for specific type.
1 parent e13a3c5 commit 7b8bb5a

19 files changed

+114
-32
lines changed

scylla-rust-wrapper/src/argconv.rs

+25-3
Original file line numberDiff line numberDiff line change
@@ -289,12 +289,18 @@ impl<T: Sized> CassPtr<T, (Borrowed, Const)> {
289289
}
290290
}
291291

292+
mod own_sealed {
293+
pub trait ExclusiveSealed {}
294+
pub trait SharedSealed {}
295+
pub trait BorrowedSealed {}
296+
}
297+
292298
/// Defines a pointer manipulation API for non-shared heap-allocated data.
293299
///
294300
/// Implement this trait for types that are allocated by the driver via [`Box::new`],
295301
/// and then returned to the user as a pointer. The user is responsible for freeing
296302
/// the memory associated with the pointer using corresponding driver's API function.
297-
pub trait BoxFFI: Sized {
303+
pub trait BoxFFI: Sized + own_sealed::ExclusiveSealed {
298304
fn into_ptr<M: Mutability>(self: Box<Self>) -> CassExclusivePtr<Self, M> {
299305
CassExclusivePtr::new(self)
300306
}
@@ -325,7 +331,7 @@ pub trait BoxFFI: Sized {
325331
/// The data should be allocated via [`Arc::new`], and then returned to the user as a pointer.
326332
/// The user is responsible for freeing the memory associated
327333
/// with the pointer using corresponding driver's API function.
328-
pub trait ArcFFI: Sized {
334+
pub trait ArcFFI: Sized + own_sealed::SharedSealed {
329335
fn as_ptr(self: &Arc<Self>) -> CassSharedPtr<Self> {
330336
CassSharedPtr::from_ref(self)
331337
}
@@ -360,7 +366,7 @@ pub trait ArcFFI: Sized {
360366
/// For example: lifetime of CassRow is bound by the lifetime of CassResult.
361367
/// There is no API function that frees the CassRow. It should be automatically
362368
/// freed when user calls cass_result_free.
363-
pub trait RefFFI: Sized {
369+
pub trait RefFFI: Sized + own_sealed::BorrowedSealed {
364370
fn as_ptr(&self) -> CassBorrowedPtr<Self> {
365371
CassBorrowedPtr::from_ref(self)
366372
}
@@ -374,3 +380,19 @@ pub trait RefFFI: Sized {
374380
ptr.is_null()
375381
}
376382
}
383+
384+
pub trait FFI {
385+
type Ownerhsip;
386+
}
387+
388+
pub struct OwnershipExclusive;
389+
impl<T> own_sealed::ExclusiveSealed for T where T: FFI<Ownerhsip = OwnershipExclusive> {}
390+
impl<T> BoxFFI for T where T: FFI<Ownerhsip = OwnershipExclusive> {}
391+
392+
pub struct OwnershipShared;
393+
impl<T> own_sealed::SharedSealed for T where T: FFI<Ownerhsip = OwnershipShared> {}
394+
impl<T> ArcFFI for T where T: FFI<Ownerhsip = OwnershipShared> {}
395+
396+
pub struct OwnershipBorrowed;
397+
impl<T> own_sealed::BorrowedSealed for T where T: FFI<Ownerhsip = OwnershipBorrowed> {}
398+
impl<T> RefFFI for T where T: FFI<Ownerhsip = OwnershipBorrowed> {}

scylla-rust-wrapper/src/batch.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::argconv::{ArcFFI, BoxFFI, CassExclusiveConstPtr, CassExclusiveMutPtr, CassSharedPtr};
1+
use crate::argconv::{
2+
ArcFFI, BoxFFI, CassExclusiveConstPtr, CassExclusiveMutPtr, CassSharedPtr, OwnershipExclusive,
3+
FFI,
4+
};
25
use crate::cass_error::CassError;
36
use crate::cass_types::CassConsistency;
47
use crate::cass_types::{make_batch_type, CassBatchType};
@@ -19,7 +22,9 @@ pub struct CassBatch {
1922
pub(crate) exec_profile: Option<PerStatementExecProfile>,
2023
}
2124

22-
impl BoxFFI for CassBatch {}
25+
impl FFI for CassBatch {
26+
type Ownerhsip = OwnershipExclusive;
27+
}
2328

2429
#[derive(Clone)]
2530
pub struct CassBatchState {

scylla-rust-wrapper/src/cass_types.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,9 @@ pub enum CassDataTypeInner {
175175
Custom(String),
176176
}
177177

178-
impl ArcFFI for CassDataType {}
178+
impl FFI for CassDataType {
179+
type Ownerhsip = OwnershipShared;
180+
}
179181

180182
impl CassDataTypeInner {
181183
/// Checks for equality during typechecks.

scylla-rust-wrapper/src/cluster.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,9 @@ impl CassCluster {
165165
}
166166
}
167167

168-
impl BoxFFI for CassCluster {}
168+
impl FFI for CassCluster {
169+
type Ownerhsip = OwnershipExclusive;
170+
}
169171

170172
pub struct CassCustomPayload;
171173

scylla-rust-wrapper/src/collection.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ pub struct CassCollection {
3535
pub items: Vec<CassCqlValue>,
3636
}
3737

38-
impl BoxFFI for CassCollection {}
38+
impl FFI for CassCollection {
39+
type Ownerhsip = OwnershipExclusive;
40+
}
3941

4042
impl CassCollection {
4143
fn typecheck_on_append(&self, value: &Option<CassCqlValue>) -> CassError {

scylla-rust-wrapper/src/exec_profile.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use scylla::retry_policy::RetryPolicy;
1313
use scylla::speculative_execution::SimpleSpeculativeExecutionPolicy;
1414
use scylla::statement::Consistency;
1515

16-
use crate::argconv::{ptr_to_cstr_n, strlen, ArcFFI, BoxFFI, CassExclusiveMutPtr, CassSharedPtr};
16+
use crate::argconv::{
17+
ptr_to_cstr_n, strlen, ArcFFI, BoxFFI, CassExclusiveMutPtr, CassSharedPtr, OwnershipExclusive,
18+
FFI,
19+
};
1720
use crate::batch::CassBatch;
1821
use crate::cass_error::CassError;
1922
use crate::cass_types::CassConsistency;
@@ -37,7 +40,9 @@ pub struct CassExecProfile {
3740
load_balancing_config: LoadBalancingConfig,
3841
}
3942

40-
impl BoxFFI for CassExecProfile {}
43+
impl FFI for CassExecProfile {
44+
type Ownerhsip = OwnershipExclusive;
45+
}
4146

4247
impl CassExecProfile {
4348
fn new() -> Self {

scylla-rust-wrapper/src/future.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ pub struct CassFuture {
6060
wait_for_value: Condvar,
6161
}
6262

63-
impl ArcFFI for CassFuture {}
63+
impl FFI for CassFuture {
64+
type Ownerhsip = OwnershipShared;
65+
}
6466

6567
/// An error that can appear during `cass_future_wait_timed`.
6668
enum FutureError {

scylla-rust-wrapper/src/logging.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use crate::argconv::{arr_to_cstr, ptr_to_cstr, str_to_arr, CassBorrowedPtr, RefFFI};
1+
use crate::argconv::{
2+
arr_to_cstr, ptr_to_cstr, str_to_arr, CassBorrowedPtr, OwnershipBorrowed, RefFFI, FFI,
3+
};
24
use crate::cass_log_types::{CassLogLevel, CassLogMessage};
35
use crate::types::size_t;
46
use crate::LOGGER;
@@ -14,7 +16,9 @@ use tracing_subscriber::layer::Context;
1416
use tracing_subscriber::prelude::*;
1517
use tracing_subscriber::Layer;
1618

17-
impl RefFFI for CassLogMessage {}
19+
impl FFI for CassLogMessage {
20+
type Ownerhsip = OwnershipBorrowed;
21+
}
1822

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

scylla-rust-wrapper/src/metadata.rs

+15-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ pub struct CassSchemaMeta {
1313
pub keyspaces: HashMap<String, CassKeyspaceMeta>,
1414
}
1515

16-
impl BoxFFI for CassSchemaMeta {}
16+
impl FFI for CassSchemaMeta {
17+
type Ownerhsip = OwnershipExclusive;
18+
}
1719

1820
pub struct CassKeyspaceMeta {
1921
pub name: String,
@@ -25,7 +27,9 @@ pub struct CassKeyspaceMeta {
2527
}
2628

2729
// Owned by CassSchemaMeta
28-
impl RefFFI for CassKeyspaceMeta {}
30+
impl FFI for CassKeyspaceMeta {
31+
type Ownerhsip = OwnershipBorrowed;
32+
}
2933

3034
pub struct CassTableMeta {
3135
pub name: String,
@@ -38,7 +42,9 @@ pub struct CassTableMeta {
3842
// Either:
3943
// - owned by CassMaterializedViewMeta - won't be given to user
4044
// - Owned by CassKeyspaceMeta (in Arc), referenced (Weak) by CassMaterializedViewMeta
41-
impl RefFFI for CassTableMeta {}
45+
impl FFI for CassTableMeta {
46+
type Ownerhsip = OwnershipBorrowed;
47+
}
4248

4349
pub struct CassMaterializedViewMeta {
4450
pub name: String,
@@ -47,7 +53,9 @@ pub struct CassMaterializedViewMeta {
4753
}
4854

4955
// Shared ownership by CassKeyspaceMeta and CassTableMeta
50-
impl RefFFI for CassMaterializedViewMeta {}
56+
impl FFI for CassMaterializedViewMeta {
57+
type Ownerhsip = OwnershipBorrowed;
58+
}
5159

5260
pub struct CassColumnMeta {
5361
pub name: String,
@@ -56,7 +64,9 @@ pub struct CassColumnMeta {
5664
}
5765

5866
// Owned by CassTableMeta
59-
impl RefFFI for CassColumnMeta {}
67+
impl FFI for CassColumnMeta {
68+
type Ownerhsip = OwnershipBorrowed;
69+
}
6070

6171
pub unsafe fn create_table_metadata(
6272
keyspace_name: &str,

scylla-rust-wrapper/src/prepared.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ impl CassPrepared {
6262
}
6363
}
6464

65-
impl ArcFFI for CassPrepared {}
65+
impl FFI for CassPrepared {
66+
type Ownerhsip = OwnershipShared;
67+
}
6668

6769
#[no_mangle]
6870
pub unsafe extern "C" fn cass_prepared_free(prepared_raw: CassSharedPtr<CassPrepared>) {

scylla-rust-wrapper/src/query_error.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pub enum CassErrorResult {
1919
Deserialization(#[from] DeserializationError),
2020
}
2121

22-
impl ArcFFI for CassErrorResult {}
22+
impl FFI for CassErrorResult {
23+
type Ownerhsip = OwnershipShared;
24+
}
2325

2426
impl From<Consistency> for CassConsistency {
2527
fn from(c: Consistency) -> CassConsistency {

scylla-rust-wrapper/src/query_result.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ impl CassResult {
9595
}
9696
}
9797

98-
impl ArcFFI for CassResult {}
98+
impl FFI for CassResult {
99+
type Ownerhsip = OwnershipShared;
100+
}
99101

100102
#[derive(Debug)]
101103
pub struct CassResultMetadata {
@@ -149,7 +151,9 @@ pub struct CassRow {
149151
pub result_metadata: Arc<CassResultMetadata>,
150152
}
151153

152-
impl RefFFI for CassRow {}
154+
impl FFI for CassRow {
155+
type Ownerhsip = OwnershipBorrowed;
156+
}
153157

154158
pub fn create_cass_rows_from_rows(
155159
rows: Vec<Row>,
@@ -185,7 +189,9 @@ pub struct CassValue {
185189
pub value_type: Arc<CassDataType>,
186190
}
187191

188-
impl RefFFI for CassValue {}
192+
impl FFI for CassValue {
193+
type Ownerhsip = OwnershipBorrowed;
194+
}
189195

190196
fn create_cass_row_columns(row: Row, metadata: &Arc<CassResultMetadata>) -> Vec<CassValue> {
191197
row.columns
@@ -367,7 +373,9 @@ pub enum CassIterator {
367373
CassViewMetaIterator(CassViewMetaIterator),
368374
}
369375

370-
impl BoxFFI for CassIterator {}
376+
impl FFI for CassIterator {
377+
type Ownerhsip = OwnershipExclusive;
378+
}
371379

372380
#[no_mangle]
373381
pub unsafe extern "C" fn cass_iterator_free(iterator: CassExclusiveMutPtr<CassIterator>) {

scylla-rust-wrapper/src/retry_policy.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use scylla::retry_policy::{DefaultRetryPolicy, FallthroughRetryPolicy};
22
use scylla::transport::downgrading_consistency_retry_policy::DowngradingConsistencyRetryPolicy;
33
use std::sync::Arc;
44

5-
use crate::argconv::{ArcFFI, CassSharedPtr};
5+
use crate::argconv::{ArcFFI, CassSharedPtr, OwnershipShared, FFI};
66

77
pub enum RetryPolicy {
88
DefaultRetryPolicy(Arc<DefaultRetryPolicy>),
@@ -12,7 +12,9 @@ pub enum RetryPolicy {
1212

1313
pub type CassRetryPolicy = RetryPolicy;
1414

15-
impl ArcFFI for CassRetryPolicy {}
15+
impl FFI for CassRetryPolicy {
16+
type Ownerhsip = OwnershipShared;
17+
}
1618

1719
#[no_mangle]
1820
pub extern "C" fn cass_retry_policy_default_new() -> CassSharedPtr<CassRetryPolicy> {

scylla-rust-wrapper/src/session.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ impl CassSessionInner {
139139

140140
pub type CassSession = RwLock<Option<CassSessionInner>>;
141141

142-
impl ArcFFI for CassSession {}
142+
impl FFI for CassSession {
143+
type Ownerhsip = OwnershipShared;
144+
}
143145

144146
#[no_mangle]
145147
pub unsafe extern "C" fn cass_session_new() -> CassSharedPtr<CassSession> {

scylla-rust-wrapper/src/ssl.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::argconv::ArcFFI;
22
use crate::argconv::CassSharedPtr;
3+
use crate::argconv::OwnershipShared;
4+
use crate::argconv::FFI;
35
use crate::cass_error::CassError;
46
use crate::types::size_t;
57
use libc::{c_int, strlen};
@@ -20,7 +22,9 @@ pub struct CassSsl {
2022
pub(crate) trusted_store: *mut X509_STORE,
2123
}
2224

23-
impl ArcFFI for CassSsl {}
25+
impl FFI for CassSsl {
26+
type Ownerhsip = OwnershipShared;
27+
}
2428

2529
pub const CASS_SSL_VERIFY_NONE: i32 = 0x00;
2630
pub const CASS_SSL_VERIFY_PEER_CERT: i32 = 0x01;

scylla-rust-wrapper/src/statement.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ pub struct CassStatement {
4242
pub(crate) exec_profile: Option<PerStatementExecProfile>,
4343
}
4444

45-
impl BoxFFI for CassStatement {}
45+
impl FFI for CassStatement {
46+
type Ownerhsip = OwnershipExclusive;
47+
}
4648

4749
impl CassStatement {
4850
fn bind_cql_value(&mut self, index: usize, value: Option<CassCqlValue>) -> CassError {

scylla-rust-wrapper/src/tuple.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ pub struct CassTuple {
1919
pub items: Vec<Option<CassCqlValue>>,
2020
}
2121

22-
impl BoxFFI for CassTuple {}
22+
impl FFI for CassTuple {
23+
type Ownerhsip = OwnershipExclusive;
24+
}
2325

2426
impl CassTuple {
2527
fn get_types(&self) -> Option<&Vec<Arc<CassDataType>>> {

scylla-rust-wrapper/src/user_type.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ pub struct CassUserType {
1414
pub field_values: Vec<Option<CassCqlValue>>,
1515
}
1616

17-
impl BoxFFI for CassUserType {}
17+
impl FFI for CassUserType {
18+
type Ownerhsip = OwnershipExclusive;
19+
}
1820

1921
impl CassUserType {
2022
fn set_field_by_index(&mut self, index: usize, value: Option<CassCqlValue>) -> CassError {

scylla-rust-wrapper/src/uuid.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ pub struct CassUuidGen {
1717
pub last_timestamp: AtomicU64,
1818
}
1919

20-
impl BoxFFI for CassUuidGen {}
20+
impl FFI for CassUuidGen {
21+
type Ownerhsip = OwnershipExclusive;
22+
}
2123

2224
// Implementation directly ported from Cpp Driver implementation:
2325

0 commit comments

Comments
 (0)