Skip to content

Commit 283fd5f

Browse files
authored
Merge pull request #208 from muzarski/pointer-kinds
safety: introduce pointer types and their restrictions
2 parents 937c737 + d5df8a1 commit 283fd5f

25 files changed

+2223
-1147
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ CASSANDRA_TEST_FILTER := $(subst ${SPACE},${EMPTY},ClusterTests.*\
6363
endif
6464

6565
ifndef CCM_COMMIT_ID
66-
export CCM_COMMIT_ID := master
66+
# TODO: change it back to master/next when https://github.com/scylladb/scylla-ccm/issues/646 is fixed.
67+
export CCM_COMMIT_ID := 5392dd68
6768
endif
6869

6970
ifndef SCYLLA_VERSION

scylla-rust-wrapper/src/argconv.rs

+601-45
Large diffs are not rendered by default.

scylla-rust-wrapper/src/batch.rs

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use crate::argconv::{ArcFFI, BoxFFI};
1+
use crate::argconv::{
2+
ArcFFI, BoxFFI, CMut, CassBorrowedExclusivePtr, CassBorrowedSharedPtr, CassOwnedExclusivePtr,
3+
FromBox, 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 Origin = FromBox;
27+
}
2328

2429
#[derive(Clone)]
2530
pub struct CassBatchState {
@@ -28,7 +33,9 @@ pub struct CassBatchState {
2833
}
2934

3035
#[no_mangle]
31-
pub unsafe extern "C" fn cass_batch_new(type_: CassBatchType) -> *mut CassBatch {
36+
pub unsafe extern "C" fn cass_batch_new(
37+
type_: CassBatchType,
38+
) -> CassOwnedExclusivePtr<CassBatch, CMut> {
3239
if let Some(batch_type) = make_batch_type(type_) {
3340
BoxFFI::into_ptr(Box::new(CassBatch {
3441
state: Arc::new(CassBatchState {
@@ -39,21 +46,21 @@ pub unsafe extern "C" fn cass_batch_new(type_: CassBatchType) -> *mut CassBatch
3946
exec_profile: None,
4047
}))
4148
} else {
42-
std::ptr::null_mut()
49+
BoxFFI::null_mut()
4350
}
4451
}
4552

4653
#[no_mangle]
47-
pub unsafe extern "C" fn cass_batch_free(batch: *mut CassBatch) {
54+
pub unsafe extern "C" fn cass_batch_free(batch: CassOwnedExclusivePtr<CassBatch, CMut>) {
4855
BoxFFI::free(batch);
4956
}
5057

5158
#[no_mangle]
5259
pub unsafe extern "C" fn cass_batch_set_consistency(
53-
batch: *mut CassBatch,
60+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
5461
consistency: CassConsistency,
5562
) -> CassError {
56-
let batch = BoxFFI::as_mut_ref(batch);
63+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
5764
let consistency = match consistency.try_into().ok() {
5865
Some(c) => c,
5966
None => return CassError::CASS_ERROR_LIB_BAD_PARAMS,
@@ -67,10 +74,10 @@ pub unsafe extern "C" fn cass_batch_set_consistency(
6774

6875
#[no_mangle]
6976
pub unsafe extern "C" fn cass_batch_set_serial_consistency(
70-
batch: *mut CassBatch,
77+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
7178
serial_consistency: CassConsistency,
7279
) -> CassError {
73-
let batch = BoxFFI::as_mut_ref(batch);
80+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
7481
let serial_consistency = match serial_consistency.try_into().ok() {
7582
Some(c) => c,
7683
None => return CassError::CASS_ERROR_LIB_BAD_PARAMS,
@@ -84,13 +91,13 @@ pub unsafe extern "C" fn cass_batch_set_serial_consistency(
8491

8592
#[no_mangle]
8693
pub unsafe extern "C" fn cass_batch_set_retry_policy(
87-
batch: *mut CassBatch,
88-
retry_policy: *const CassRetryPolicy,
94+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
95+
retry_policy: CassBorrowedSharedPtr<CassRetryPolicy, CMut>,
8996
) -> CassError {
90-
let batch = BoxFFI::as_mut_ref(batch);
97+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
9198

9299
let maybe_arced_retry_policy: Option<Arc<dyn scylla::policies::retry::RetryPolicy>> =
93-
ArcFFI::as_maybe_ref(retry_policy).map(|policy| match policy {
100+
ArcFFI::as_ref(retry_policy).map(|policy| match policy {
94101
CassRetryPolicy::DefaultRetryPolicy(default) => {
95102
default.clone() as Arc<dyn scylla::policies::retry::RetryPolicy>
96103
}
@@ -107,10 +114,10 @@ pub unsafe extern "C" fn cass_batch_set_retry_policy(
107114

108115
#[no_mangle]
109116
pub unsafe extern "C" fn cass_batch_set_timestamp(
110-
batch: *mut CassBatch,
117+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
111118
timestamp: cass_int64_t,
112119
) -> CassError {
113-
let batch = BoxFFI::as_mut_ref(batch);
120+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
114121

115122
Arc::make_mut(&mut batch.state)
116123
.batch
@@ -121,21 +128,21 @@ pub unsafe extern "C" fn cass_batch_set_timestamp(
121128

122129
#[no_mangle]
123130
pub unsafe extern "C" fn cass_batch_set_request_timeout(
124-
batch: *mut CassBatch,
131+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
125132
timeout_ms: cass_uint64_t,
126133
) -> CassError {
127-
let batch = BoxFFI::as_mut_ref(batch);
134+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
128135
batch.batch_request_timeout_ms = Some(timeout_ms);
129136

130137
CassError::CASS_OK
131138
}
132139

133140
#[no_mangle]
134141
pub unsafe extern "C" fn cass_batch_set_is_idempotent(
135-
batch: *mut CassBatch,
142+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
136143
is_idempotent: cass_bool_t,
137144
) -> CassError {
138-
let batch = BoxFFI::as_mut_ref(batch);
145+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
139146
Arc::make_mut(&mut batch.state)
140147
.batch
141148
.set_is_idempotent(is_idempotent != 0);
@@ -145,10 +152,10 @@ pub unsafe extern "C" fn cass_batch_set_is_idempotent(
145152

146153
#[no_mangle]
147154
pub unsafe extern "C" fn cass_batch_set_tracing(
148-
batch: *mut CassBatch,
155+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
149156
enabled: cass_bool_t,
150157
) -> CassError {
151-
let batch = BoxFFI::as_mut_ref(batch);
158+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
152159
Arc::make_mut(&mut batch.state)
153160
.batch
154161
.set_tracing(enabled != 0);
@@ -158,12 +165,12 @@ pub unsafe extern "C" fn cass_batch_set_tracing(
158165

159166
#[no_mangle]
160167
pub unsafe extern "C" fn cass_batch_add_statement(
161-
batch: *mut CassBatch,
162-
statement: *const CassStatement,
168+
batch: CassBorrowedExclusivePtr<CassBatch, CMut>,
169+
statement: CassBorrowedSharedPtr<CassStatement, CMut>,
163170
) -> CassError {
164-
let batch = BoxFFI::as_mut_ref(batch);
171+
let batch = BoxFFI::as_mut_ref(batch).unwrap();
165172
let state = Arc::make_mut(&mut batch.state);
166-
let statement = BoxFFI::as_ref(statement);
173+
let statement = BoxFFI::as_ref(statement).unwrap();
167174

168175
match &statement.statement {
169176
BoundStatement::Simple(q) => {

scylla-rust-wrapper/src/binding.rs

+18-16
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+
this: CassBorrowedExclusivePtr<$this, CMut>,
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(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+
this: CassBorrowedExclusivePtr<$this, CMut>,
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(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+
this: CassBorrowedExclusivePtr<$this, CMut>,
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(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+
this: CassBorrowedExclusivePtr<$this, CMut>,
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(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: CassBorrowedSharedPtr<crate::collection::CassCollection, CConst>| {
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 @ CassBorrowedSharedPtr<crate::collection::CassCollection, CConst>]
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: CassBorrowedSharedPtr<crate::tuple::CassTuple, CConst>| {
320+
Ok(Some(BoxFFI::as_ref(p).unwrap().into()))
321321
},
322-
[p @ *const crate::tuple::CassTuple]
322+
[p @ CassBorrowedSharedPtr<crate::tuple::CassTuple, CConst>]
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: CassBorrowedSharedPtr<crate::user_type::CassUserType, CConst>| {
331+
Ok(Some(BoxFFI::as_ref(p).unwrap().into()))
332+
},
333+
[p @ CassBorrowedSharedPtr<crate::user_type::CassUserType, CConst>]
332334
);
333335
};
334336
}

0 commit comments

Comments
 (0)