Skip to content

Commit bdd7a04

Browse files
committed
argconv: pointers
This commit introduces a `CassPtr` type, generic over pointer `Properties`. It allows specific pointer-to-reference conversions based on the guarantees provided by the pointer type. Existing `Ref/Box/Arc`FFIs are adjusted, so they now allow interaction with new pointer type. You can say, that for a user of `argconv` API, new type is opaque. The only way to operate on it is to use the corresponding ffi API.
1 parent e5efd2d commit bdd7a04

21 files changed

+1361
-938
lines changed

scylla-rust-wrapper/src/argconv.rs

Lines changed: 319 additions & 43 deletions
Large diffs are not rendered by default.

scylla-rust-wrapper/src/batch.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::argconv::{ArcFFI, BoxFFI};
1+
use crate::argconv::{ArcFFI, BoxFFI, CassExclusiveConstPtr, CassExclusiveMutPtr, CassSharedPtr};
22
use crate::cass_error::CassError;
33
use crate::cass_types::CassConsistency;
44
use crate::cass_types::{make_batch_type, CassBatchType};
@@ -28,7 +28,7 @@ pub struct CassBatchState {
2828
}
2929

3030
#[no_mangle]
31-
pub unsafe extern "C" fn cass_batch_new(type_: CassBatchType) -> *mut CassBatch {
31+
pub unsafe extern "C" fn cass_batch_new(type_: CassBatchType) -> CassExclusiveMutPtr<CassBatch> {
3232
if let Some(batch_type) = make_batch_type(type_) {
3333
BoxFFI::into_ptr(Box::new(CassBatch {
3434
state: Arc::new(CassBatchState {
@@ -39,21 +39,21 @@ pub unsafe extern "C" fn cass_batch_new(type_: CassBatchType) -> *mut CassBatch
3939
exec_profile: None,
4040
}))
4141
} else {
42-
std::ptr::null_mut()
42+
BoxFFI::null_mut()
4343
}
4444
}
4545

4646
#[no_mangle]
47-
pub unsafe extern "C" fn cass_batch_free(batch: *mut CassBatch) {
47+
pub unsafe extern "C" fn cass_batch_free(batch: CassExclusiveMutPtr<CassBatch>) {
4848
BoxFFI::free(batch);
4949
}
5050

5151
#[no_mangle]
5252
pub unsafe extern "C" fn cass_batch_set_consistency(
53-
batch: *mut CassBatch,
53+
mut batch: CassExclusiveMutPtr<CassBatch>,
5454
consistency: CassConsistency,
5555
) -> CassError {
56-
let batch = BoxFFI::as_mut_ref(batch);
56+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
5757
let consistency = match consistency.try_into().ok() {
5858
Some(c) => c,
5959
None => return CassError::CASS_ERROR_LIB_BAD_PARAMS,
@@ -67,10 +67,10 @@ pub unsafe extern "C" fn cass_batch_set_consistency(
6767

6868
#[no_mangle]
6969
pub unsafe extern "C" fn cass_batch_set_serial_consistency(
70-
batch: *mut CassBatch,
70+
mut batch: CassExclusiveMutPtr<CassBatch>,
7171
serial_consistency: CassConsistency,
7272
) -> CassError {
73-
let batch = BoxFFI::as_mut_ref(batch);
73+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
7474
let serial_consistency = match serial_consistency.try_into().ok() {
7575
Some(c) => c,
7676
None => return CassError::CASS_ERROR_LIB_BAD_PARAMS,
@@ -84,13 +84,13 @@ pub unsafe extern "C" fn cass_batch_set_serial_consistency(
8484

8585
#[no_mangle]
8686
pub unsafe extern "C" fn cass_batch_set_retry_policy(
87-
batch: *mut CassBatch,
88-
retry_policy: *const CassRetryPolicy,
87+
mut batch: CassExclusiveMutPtr<CassBatch>,
88+
retry_policy: CassSharedPtr<CassRetryPolicy>,
8989
) -> CassError {
90-
let batch = BoxFFI::as_mut_ref(batch);
90+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
9191

9292
let maybe_arced_retry_policy: Option<Arc<dyn scylla::retry_policy::RetryPolicy>> =
93-
ArcFFI::as_maybe_ref(retry_policy).map(|policy| match policy {
93+
ArcFFI::as_ref(&retry_policy).map(|policy| match policy {
9494
CassRetryPolicy::DefaultRetryPolicy(default) => {
9595
default.clone() as Arc<dyn scylla::retry_policy::RetryPolicy>
9696
}
@@ -107,10 +107,10 @@ pub unsafe extern "C" fn cass_batch_set_retry_policy(
107107

108108
#[no_mangle]
109109
pub unsafe extern "C" fn cass_batch_set_timestamp(
110-
batch: *mut CassBatch,
110+
mut batch: CassExclusiveMutPtr<CassBatch>,
111111
timestamp: cass_int64_t,
112112
) -> CassError {
113-
let batch = BoxFFI::as_mut_ref(batch);
113+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
114114

115115
Arc::make_mut(&mut batch.state)
116116
.batch
@@ -121,21 +121,21 @@ pub unsafe extern "C" fn cass_batch_set_timestamp(
121121

122122
#[no_mangle]
123123
pub unsafe extern "C" fn cass_batch_set_request_timeout(
124-
batch: *mut CassBatch,
124+
mut batch: CassExclusiveMutPtr<CassBatch>,
125125
timeout_ms: cass_uint64_t,
126126
) -> CassError {
127-
let batch = BoxFFI::as_mut_ref(batch);
127+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
128128
batch.batch_request_timeout_ms = Some(timeout_ms);
129129

130130
CassError::CASS_OK
131131
}
132132

133133
#[no_mangle]
134134
pub unsafe extern "C" fn cass_batch_set_is_idempotent(
135-
batch: *mut CassBatch,
135+
mut batch: CassExclusiveMutPtr<CassBatch>,
136136
is_idempotent: cass_bool_t,
137137
) -> CassError {
138-
let batch = BoxFFI::as_mut_ref(batch);
138+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
139139
Arc::make_mut(&mut batch.state)
140140
.batch
141141
.set_is_idempotent(is_idempotent != 0);
@@ -145,10 +145,10 @@ pub unsafe extern "C" fn cass_batch_set_is_idempotent(
145145

146146
#[no_mangle]
147147
pub unsafe extern "C" fn cass_batch_set_tracing(
148-
batch: *mut CassBatch,
148+
mut batch: CassExclusiveMutPtr<CassBatch>,
149149
enabled: cass_bool_t,
150150
) -> CassError {
151-
let batch = BoxFFI::as_mut_ref(batch);
151+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
152152
Arc::make_mut(&mut batch.state)
153153
.batch
154154
.set_tracing(enabled != 0);
@@ -158,12 +158,12 @@ pub unsafe extern "C" fn cass_batch_set_tracing(
158158

159159
#[no_mangle]
160160
pub unsafe extern "C" fn cass_batch_add_statement(
161-
batch: *mut CassBatch,
162-
statement: *const CassStatement,
161+
mut batch: CassExclusiveMutPtr<CassBatch>,
162+
statement: CassExclusiveConstPtr<CassStatement>,
163163
) -> CassError {
164-
let batch = BoxFFI::as_mut_ref(batch);
164+
let batch = BoxFFI::as_mut_ref(&mut batch).unwrap();
165165
let state = Arc::make_mut(&mut batch.state);
166-
let statement = BoxFFI::as_ref(statement);
166+
let statement = BoxFFI::as_ref(&statement).unwrap();
167167

168168
match &statement.statement {
169169
Statement::Simple(q) => state.batch.append_statement(q.query.clone()),

scylla-rust-wrapper/src/binding.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ macro_rules! make_index_binder {
5353
#[no_mangle]
5454
#[allow(clippy::redundant_closure_call)]
5555
pub unsafe extern "C" fn $fn_by_idx(
56-
this: *mut $this,
56+
mut this: CassExclusiveMutPtr<$this>,
5757
index: size_t,
5858
$($arg: $t), *
5959
) -> CassError {
6060
// For some reason detected as unused, which is not true
6161
#[allow(unused_imports)]
6262
use crate::value::CassCqlValue::*;
6363
match ($e)($($arg), *) {
64-
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this), index as usize, v),
64+
Ok(v) => $consume_v(BoxFFI::as_mut_ref(&mut this).unwrap(), index as usize, v),
6565
Err(e) => e,
6666
}
6767
}
@@ -73,7 +73,7 @@ macro_rules! make_name_binder {
7373
#[no_mangle]
7474
#[allow(clippy::redundant_closure_call)]
7575
pub unsafe extern "C" fn $fn_by_name(
76-
this: *mut $this,
76+
mut this: CassExclusiveMutPtr<$this>,
7777
name: *const c_char,
7878
$($arg: $t), *
7979
) -> CassError {
@@ -82,7 +82,7 @@ macro_rules! make_name_binder {
8282
use crate::value::CassCqlValue::*;
8383
let name = ptr_to_cstr(name).unwrap();
8484
match ($e)($($arg), *) {
85-
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this), name, v),
85+
Ok(v) => $consume_v(BoxFFI::as_mut_ref(&mut this).unwrap(), name, v),
8686
Err(e) => e,
8787
}
8888
}
@@ -94,7 +94,7 @@ macro_rules! make_name_n_binder {
9494
#[no_mangle]
9595
#[allow(clippy::redundant_closure_call)]
9696
pub unsafe extern "C" fn $fn_by_name_n(
97-
this: *mut $this,
97+
mut this: CassExclusiveMutPtr<$this>,
9898
name: *const c_char,
9999
name_length: size_t,
100100
$($arg: $t), *
@@ -104,7 +104,7 @@ macro_rules! make_name_n_binder {
104104
use crate::value::CassCqlValue::*;
105105
let name = ptr_to_cstr_n(name, name_length).unwrap();
106106
match ($e)($($arg), *) {
107-
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this), name, v),
107+
Ok(v) => $consume_v(BoxFFI::as_mut_ref(&mut this).unwrap(), name, v),
108108
Err(e) => e,
109109
}
110110
}
@@ -116,14 +116,14 @@ macro_rules! make_appender {
116116
#[no_mangle]
117117
#[allow(clippy::redundant_closure_call)]
118118
pub unsafe extern "C" fn $fn_append(
119-
this: *mut $this,
119+
mut this: CassExclusiveMutPtr<$this>,
120120
$($arg: $t), *
121121
) -> CassError {
122122
// For some reason detected as unused, which is not true
123123
#[allow(unused_imports)]
124124
use crate::value::CassCqlValue::*;
125125
match ($e)($($arg), *) {
126-
Ok(v) => $consume_v(BoxFFI::as_mut_ref(this), v),
126+
Ok(v) => $consume_v(BoxFFI::as_mut_ref(&mut this).unwrap(), v),
127127
Err(e) => e,
128128
}
129129
}
@@ -302,33 +302,35 @@ macro_rules! invoke_binder_maker_macro_with_type {
302302
$this,
303303
$consume_v,
304304
$fn,
305-
|p: *const crate::collection::CassCollection| {
306-
match std::convert::TryInto::try_into(BoxFFI::as_ref(p)) {
305+
|p: CassExclusiveConstPtr<crate::collection::CassCollection>| {
306+
match std::convert::TryInto::try_into(BoxFFI::as_ref(&p).unwrap()) {
307307
Ok(v) => Ok(Some(v)),
308308
Err(_) => Err(CassError::CASS_ERROR_LIB_INVALID_VALUE_TYPE),
309309
}
310310
},
311-
[p @ *const crate::collection::CassCollection]
311+
[p @ CassExclusiveConstPtr<crate::collection::CassCollection>]
312312
);
313313
};
314314
(tuple, $macro_name:ident, $this:ty, $consume_v:expr, $fn:ident) => {
315315
$macro_name!(
316316
$this,
317317
$consume_v,
318318
$fn,
319-
|p: *const crate::tuple::CassTuple| {
320-
Ok(Some(BoxFFI::as_ref(p).into()))
319+
|p: CassExclusiveConstPtr<crate::tuple::CassTuple>| {
320+
Ok(Some(BoxFFI::as_ref(&p).unwrap().into()))
321321
},
322-
[p @ *const crate::tuple::CassTuple]
322+
[p @ CassExclusiveConstPtr<crate::tuple::CassTuple>]
323323
);
324324
};
325325
(user_type, $macro_name:ident, $this:ty, $consume_v:expr, $fn:ident) => {
326326
$macro_name!(
327327
$this,
328328
$consume_v,
329329
$fn,
330-
|p: *const crate::user_type::CassUserType| Ok(Some(BoxFFI::as_ref(p).into())),
331-
[p @ *const crate::user_type::CassUserType]
330+
|p: CassExclusiveConstPtr<crate::user_type::CassUserType>| {
331+
Ok(Some(BoxFFI::as_ref(&p).unwrap().into()))
332+
},
333+
[p @ CassExclusiveConstPtr<crate::user_type::CassUserType>]
332334
);
333335
};
334336
}

0 commit comments

Comments
 (0)