Skip to content

Commit 36a9151

Browse files
authored
Update to cargo edition 2024 (#27)
1 parent deeabd6 commit 36a9151

16 files changed

+275
-202
lines changed

Cargo.toml

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,31 @@
11
[package]
22
name = "couchbase_lite"
3-
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Doctolib release
3+
description = "Rust bindings for Couchbase Lite C"
4+
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Rust release
45
version = "3.2.1-0"
5-
edition = "2021"
6+
7+
edition = "2024"
8+
9+
license-file = "libcblite_enterprise/LICENSE.txt"
10+
keywords = ["couchbase"]
11+
categories = ["database"]
12+
publish = ["couchbase-lite-rust"]
613

714
[dependencies]
8-
enum_primitive = "*"
9-
tempdir = "*"
10-
lazy_static = "1.4.0"
11-
regex = "1.10.4"
15+
enum_primitive = "0.1.1"
16+
17+
[dev-dependencies]
18+
lazy_static = "1.5.0"
19+
regex = "1.11.1"
20+
tempdir = "0.3.7"
1221

1322
[dev-dependencies.cargo-husky]
1423
version = "1"
1524
default-features = false # Disable features which are enabled by default
1625
features = ["user-hooks"]
1726

1827
[build-dependencies]
19-
bindgen = "0.69.4"
28+
bindgen = "0.71.1"
2029
fs_extra = "1.2.0"
2130

2231
[lib]

build.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
// - https://doc.rust-lang.org/cargo/reference/build-scripts.html
2424

2525
#[cfg(all(not(feature = "community"), not(feature = "enterprise")))]
26-
compile_error!("You need to have one the following features activated: community, enterprise");
26+
compile_error!(
27+
"You need to have at least one the following features activated: community, enterprise"
28+
);
2729
#[cfg(all(feature = "community", feature = "enterprise"))]
28-
compile_error!("You need to have one the following features activated: community, enterprise");
30+
compile_error!(
31+
"You need to have at most one the following features activated: community, enterprise"
32+
);
2933

3034
extern crate bindgen;
3135
extern crate fs_extra;

src/blob.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -231,11 +231,7 @@ impl std::io::Write for BlobWriter<'_> {
231231
data.len(),
232232
err,
233233
);
234-
if ok {
235-
data.len() as i32
236-
} else {
237-
-1
238-
}
234+
if ok { data.len() as i32 } else { -1 }
239235
})
240236
}
241237
}

src/collection.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,21 @@ impl Clone for Collection {
148148
/// A collection change listener callback, invoked after one or more documents are changed on disk.
149149
pub type CollectionChangeListener = Box<dyn Fn(Collection, Vec<String>)>;
150150

151-
#[no_mangle]
151+
#[unsafe(no_mangle)]
152152
unsafe extern "C" fn c_collection_change_listener(
153153
context: *mut ::std::os::raw::c_void,
154154
change: *const CBLCollectionChange,
155155
) {
156156
let callback = context as *const CollectionChangeListener;
157-
if let Some(change) = change.as_ref() {
158-
let collection = Collection::reference(change.collection as *mut CBLCollection);
159-
let doc_ids = std::slice::from_raw_parts(change.docIDs, change.numDocs as usize)
160-
.iter()
161-
.filter_map(|doc_id| doc_id.to_string())
162-
.collect();
163-
164-
(*callback)(collection, doc_ids);
157+
unsafe {
158+
if let Some(change) = change.as_ref() {
159+
let collection = Collection::reference(change.collection as *mut CBLCollection);
160+
let doc_ids = std::slice::from_raw_parts(change.docIDs, change.numDocs as usize)
161+
.iter()
162+
.filter_map(|doc_id| doc_id.to_string())
163+
.collect();
164+
165+
(*callback)(collection, doc_ids);
166+
}
165167
}
166168
}

src/database.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ enum_from_primitive! {
132132
#[deprecated(note = "please use `CollectionChangeListener` on default collection instead")]
133133
type DatabaseChangeListener = Box<dyn Fn(&Database, Vec<String>)>;
134134

135-
#[no_mangle]
135+
#[unsafe(no_mangle)]
136136
unsafe extern "C" fn c_database_change_listener(
137137
context: *mut ::std::os::raw::c_void,
138138
db: *const CBLDatabase,
@@ -142,26 +142,30 @@ unsafe extern "C" fn c_database_change_listener(
142142
let callback = context as *const DatabaseChangeListener;
143143
let database = Database::reference(db as *mut CBLDatabase);
144144

145-
let doc_ids = std::slice::from_raw_parts(c_doc_ids, num_docs as usize)
146-
.iter()
147-
.filter_map(|doc_id| doc_id.to_string())
148-
.collect();
145+
unsafe {
146+
let doc_ids = std::slice::from_raw_parts(c_doc_ids, num_docs as usize)
147+
.iter()
148+
.filter_map(|doc_id| doc_id.to_string())
149+
.collect();
149150

150-
(*callback)(&database, doc_ids);
151+
(*callback)(&database, doc_ids);
152+
}
151153
}
152154

153155
/// Callback indicating that the database (or an object belonging to it) is ready to call one or more listeners.
154156
type BufferNotifications = fn(db: &Database);
155-
#[no_mangle]
157+
#[unsafe(no_mangle)]
156158
unsafe extern "C" fn c_database_buffer_notifications(
157159
context: *mut ::std::os::raw::c_void,
158160
db: *mut CBLDatabase,
159161
) {
160-
let callback: BufferNotifications = std::mem::transmute(context);
162+
unsafe {
163+
let callback: BufferNotifications = std::mem::transmute(context);
161164

162-
let database = Database::reference(db.cast::<CBLDatabase>());
165+
let database = Database::reference(db.cast::<CBLDatabase>());
163166

164-
callback(&database);
167+
callback(&database);
168+
}
165169
}
166170

167171
/// A connection to an open database
@@ -213,11 +217,13 @@ impl Database {
213217

214218
unsafe fn _open(name: &str, config_ptr: *const CBLDatabaseConfiguration) -> Result<Self> {
215219
let mut err = CBLError::default();
216-
let db_ref = CBLDatabase_Open(from_str(name).get_ref(), config_ptr, &mut err);
217-
if db_ref.is_null() {
218-
return failure(err);
220+
unsafe {
221+
let db_ref = CBLDatabase_Open(from_str(name).get_ref(), config_ptr, &mut err);
222+
if db_ref.is_null() {
223+
return failure(err);
224+
}
225+
Ok(Self::take_ownership(db_ref))
219226
}
220-
Ok(Self::take_ownership(db_ref))
221227
}
222228

223229
//////// OTHER STATIC METHODS:

src/document.rs

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,34 +66,38 @@ pub enum ConcurrencyControl {
6666
/// (probably by a pull replicator, or by application code on another thread)
6767
/// since it was loaded into the CBLDocument being saved.
6868
type ConflictHandler = fn(&mut Document, &Document) -> bool;
69-
#[no_mangle]
69+
#[unsafe(no_mangle)]
7070
unsafe extern "C" fn c_conflict_handler(
7171
context: *mut ::std::os::raw::c_void,
7272
document_being_saved: *mut CBLDocument,
7373
conflicting_document: *const CBLDocument,
7474
) -> bool {
75-
let callback: ConflictHandler = std::mem::transmute(context);
75+
unsafe {
76+
let callback: ConflictHandler = std::mem::transmute(context);
7677

77-
callback(
78-
&mut Document::reference(document_being_saved),
79-
&Document::reference(conflicting_document as *mut CBLDocument),
80-
)
78+
callback(
79+
&mut Document::reference(document_being_saved),
80+
&Document::reference(conflicting_document as *mut CBLDocument),
81+
)
82+
}
8183
}
8284

8385
/// A document change listener lets you detect changes made to a specific document after they
8486
/// are persisted to the database.
8587
#[deprecated(note = "please use `CollectionDocumentChangeListener` instead")]
8688
type DatabaseDocumentChangeListener = Box<dyn Fn(&Database, Option<String>)>;
8789

88-
#[no_mangle]
90+
#[unsafe(no_mangle)]
8991
unsafe extern "C" fn c_database_document_change_listener(
9092
context: *mut ::std::os::raw::c_void,
9193
db: *const CBLDatabase,
9294
c_doc_id: FLString,
9395
) {
94-
let callback = context as *const DatabaseDocumentChangeListener;
95-
let database = Database::reference(db as *mut CBLDatabase);
96-
(*callback)(&database, c_doc_id.to_string());
96+
unsafe {
97+
let callback = context as *const DatabaseDocumentChangeListener;
98+
let database = Database::reference(db as *mut CBLDatabase);
99+
(*callback)(&database, c_doc_id.to_string());
100+
}
97101
}
98102

99103
//////// DATABASE'S DOCUMENT API:
@@ -308,15 +312,17 @@ impl Database {
308312
/// are persisted to the collection.
309313
type CollectionDocumentChangeListener = Box<dyn Fn(Collection, Option<String>)>;
310314

311-
#[no_mangle]
315+
#[unsafe(no_mangle)]
312316
unsafe extern "C" fn c_collection_document_change_listener(
313317
context: *mut ::std::os::raw::c_void,
314318
change: *const CBLDocumentChange,
315319
) {
316320
let callback = context as *const CollectionDocumentChangeListener;
317-
if let Some(change) = change.as_ref() {
318-
let collection = Collection::reference(change.collection as *mut CBLCollection);
319-
(*callback)(collection, change.docID.to_string());
321+
unsafe {
322+
if let Some(change) = change.as_ref() {
323+
let collection = Collection::reference(change.collection as *mut CBLCollection);
324+
(*callback)(collection, change.docID.to_string());
325+
}
320326
}
321327
}
322328

src/fleece_mutable.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ impl MutableArray {
8686
}
8787

8888
pub(crate) unsafe fn adopt(array: FLMutableArray) -> Self {
89-
FLValue_Retain(array as FLValue);
89+
unsafe {
90+
FLValue_Retain(array as FLValue);
91+
}
9092
Self { cbl_ref: array }
9193
}
9294

@@ -278,7 +280,9 @@ impl MutableDict {
278280
}
279281

280282
pub(crate) unsafe fn adopt(dict: FLMutableDict) -> Self {
281-
FLValue_Retain(dict as FLValue);
283+
unsafe {
284+
FLValue_Retain(dict as FLValue);
285+
}
282286
Self { cbl_ref: dict }
283287
}
284288

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,13 @@ pub fn dump_instances() {
145145
//////// REFCOUNT SUPPORT (INTERNAL)
146146

147147
pub(crate) unsafe fn retain<T>(cbl_ref: *mut T) -> *mut T {
148-
CBL_Retain(cbl_ref.cast::<CBLRefCounted>()).cast::<T>()
148+
unsafe { CBL_Retain(cbl_ref.cast::<CBLRefCounted>()).cast::<T>() }
149149
}
150150

151151
pub(crate) unsafe fn release<T>(cbl_ref: *mut T) {
152-
CBL_Release(cbl_ref.cast::<CBLRefCounted>());
152+
unsafe {
153+
CBL_Release(cbl_ref.cast::<CBLRefCounted>());
154+
}
153155
}
154156

155157
//////// ANDROID INIT

src/logging.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,11 @@ unsafe extern "C" fn invoke_log_callback(
149149
c_level: CBLLogLevel,
150150
msg: FLString,
151151
) {
152-
if let Some(cb) = LOG_CALLBACK {
153-
let domain = Domain::from_u8(c_domain).unwrap_or(Domain::None);
154-
let level = Level::from_u8(c_level).unwrap_or(Level::None);
155-
cb(domain, level, msg.as_str().unwrap_or("Empty error"));
152+
unsafe {
153+
if let Some(cb) = LOG_CALLBACK {
154+
let domain = Domain::from_u8(c_domain).unwrap_or(Domain::None);
155+
let level = Level::from_u8(c_level).unwrap_or(Level::None);
156+
cb(domain, level, msg.as_str().unwrap_or("Empty error"));
157+
}
156158
}
157159
}

src/query.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub enum QueryLanguage {
4141

4242
type ChangeListener = Box<dyn Fn(&Query, &ListenerToken)>;
4343

44-
#[no_mangle]
44+
#[unsafe(no_mangle)]
4545
unsafe extern "C" fn c_query_change_listener(
4646
context: *mut ::std::os::raw::c_void,
4747
query: *mut CBLQuery,
@@ -51,7 +51,9 @@ unsafe extern "C" fn c_query_change_listener(
5151
let query = Query::reference(query.cast::<CBLQuery>());
5252
let token = ListenerToken::new(token);
5353

54-
(*callback)(&query, &token);
54+
unsafe {
55+
(*callback)(&query, &token);
56+
}
5557
}
5658

5759
/** A compiled database query. */

0 commit comments

Comments
 (0)