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
23 changes: 16 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
[package]
name = "couchbase_lite"
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Doctolib release
description = "Rust bindings for Couchbase Lite C"
# The first three numbers correspond to the Couchbase Lite C release, the fourth number corresponds to the Rust release
version = "3.2.1-0"
edition = "2021"

edition = "2024"

license-file = "libcblite_enterprise/LICENSE.txt"
keywords = ["couchbase"]
categories = ["database"]
publish = ["couchbase-lite-rust"]

[dependencies]
enum_primitive = "*"
tempdir = "*"
lazy_static = "1.4.0"
regex = "1.10.4"
enum_primitive = "0.1.1"

[dev-dependencies]
lazy_static = "1.5.0"
regex = "1.11.1"
tempdir = "0.3.7"

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

[build-dependencies]
bindgen = "0.69.4"
bindgen = "0.71.1"
fs_extra = "1.2.0"

[lib]
Expand Down
8 changes: 6 additions & 2 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,13 @@
// - https://doc.rust-lang.org/cargo/reference/build-scripts.html

#[cfg(all(not(feature = "community"), not(feature = "enterprise")))]
compile_error!("You need to have one the following features activated: community, enterprise");
compile_error!(
"You need to have at least one the following features activated: community, enterprise"
);
#[cfg(all(feature = "community", feature = "enterprise"))]
compile_error!("You need to have one the following features activated: community, enterprise");
compile_error!(
"You need to have at most one the following features activated: community, enterprise"
);

extern crate bindgen;
extern crate fs_extra;
Expand Down
6 changes: 1 addition & 5 deletions src/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,7 @@ impl std::io::Write for BlobWriter<'_> {
data.len(),
err,
);
if ok {
data.len() as i32
} else {
-1
}
if ok { data.len() as i32 } else { -1 }
})
}
}
Expand Down
20 changes: 11 additions & 9 deletions src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,21 @@ impl Clone for Collection {
/// A collection change listener callback, invoked after one or more documents are changed on disk.
pub type CollectionChangeListener = Box<dyn Fn(Collection, Vec<String>)>;

#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn c_collection_change_listener(
context: *mut ::std::os::raw::c_void,
change: *const CBLCollectionChange,
) {
let callback = context as *const CollectionChangeListener;
if let Some(change) = change.as_ref() {
let collection = Collection::reference(change.collection as *mut CBLCollection);
let doc_ids = std::slice::from_raw_parts(change.docIDs, change.numDocs as usize)
.iter()
.filter_map(|doc_id| doc_id.to_string())
.collect();

(*callback)(collection, doc_ids);
unsafe {
if let Some(change) = change.as_ref() {
let collection = Collection::reference(change.collection as *mut CBLCollection);
let doc_ids = std::slice::from_raw_parts(change.docIDs, change.numDocs as usize)
.iter()
.filter_map(|doc_id| doc_id.to_string())
.collect();

(*callback)(collection, doc_ids);
}
}
}
34 changes: 20 additions & 14 deletions src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ enum_from_primitive! {
#[deprecated(note = "please use `CollectionChangeListener` on default collection instead")]
type DatabaseChangeListener = Box<dyn Fn(&Database, Vec<String>)>;

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

let doc_ids = std::slice::from_raw_parts(c_doc_ids, num_docs as usize)
.iter()
.filter_map(|doc_id| doc_id.to_string())
.collect();
unsafe {
let doc_ids = std::slice::from_raw_parts(c_doc_ids, num_docs as usize)
.iter()
.filter_map(|doc_id| doc_id.to_string())
.collect();

(*callback)(&database, doc_ids);
(*callback)(&database, doc_ids);
}
}

/// Callback indicating that the database (or an object belonging to it) is ready to call one or more listeners.
type BufferNotifications = fn(db: &Database);
#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn c_database_buffer_notifications(
context: *mut ::std::os::raw::c_void,
db: *mut CBLDatabase,
) {
let callback: BufferNotifications = std::mem::transmute(context);
unsafe {
let callback: BufferNotifications = std::mem::transmute(context);

let database = Database::reference(db.cast::<CBLDatabase>());
let database = Database::reference(db.cast::<CBLDatabase>());

callback(&database);
callback(&database);
}
}

/// A connection to an open database
Expand Down Expand Up @@ -213,11 +217,13 @@ impl Database {

unsafe fn _open(name: &str, config_ptr: *const CBLDatabaseConfiguration) -> Result<Self> {
let mut err = CBLError::default();
let db_ref = CBLDatabase_Open(from_str(name).get_ref(), config_ptr, &mut err);
if db_ref.is_null() {
return failure(err);
unsafe {
let db_ref = CBLDatabase_Open(from_str(name).get_ref(), config_ptr, &mut err);
if db_ref.is_null() {
return failure(err);
}
Ok(Self::take_ownership(db_ref))
}
Ok(Self::take_ownership(db_ref))
}

//////// OTHER STATIC METHODS:
Expand Down
34 changes: 20 additions & 14 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,34 +66,38 @@ pub enum ConcurrencyControl {
/// (probably by a pull replicator, or by application code on another thread)
/// since it was loaded into the CBLDocument being saved.
type ConflictHandler = fn(&mut Document, &Document) -> bool;
#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn c_conflict_handler(
context: *mut ::std::os::raw::c_void,
document_being_saved: *mut CBLDocument,
conflicting_document: *const CBLDocument,
) -> bool {
let callback: ConflictHandler = std::mem::transmute(context);
unsafe {
let callback: ConflictHandler = std::mem::transmute(context);

callback(
&mut Document::reference(document_being_saved),
&Document::reference(conflicting_document as *mut CBLDocument),
)
callback(
&mut Document::reference(document_being_saved),
&Document::reference(conflicting_document as *mut CBLDocument),
)
}
}

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

#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn c_database_document_change_listener(
context: *mut ::std::os::raw::c_void,
db: *const CBLDatabase,
c_doc_id: FLString,
) {
let callback = context as *const DatabaseDocumentChangeListener;
let database = Database::reference(db as *mut CBLDatabase);
(*callback)(&database, c_doc_id.to_string());
unsafe {
let callback = context as *const DatabaseDocumentChangeListener;
let database = Database::reference(db as *mut CBLDatabase);
(*callback)(&database, c_doc_id.to_string());
}
}

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

#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn c_collection_document_change_listener(
context: *mut ::std::os::raw::c_void,
change: *const CBLDocumentChange,
) {
let callback = context as *const CollectionDocumentChangeListener;
if let Some(change) = change.as_ref() {
let collection = Collection::reference(change.collection as *mut CBLCollection);
(*callback)(collection, change.docID.to_string());
unsafe {
if let Some(change) = change.as_ref() {
let collection = Collection::reference(change.collection as *mut CBLCollection);
(*callback)(collection, change.docID.to_string());
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions src/fleece_mutable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ impl MutableArray {
}

pub(crate) unsafe fn adopt(array: FLMutableArray) -> Self {
FLValue_Retain(array as FLValue);
unsafe {
FLValue_Retain(array as FLValue);
}
Self { cbl_ref: array }
}

Expand Down Expand Up @@ -278,7 +280,9 @@ impl MutableDict {
}

pub(crate) unsafe fn adopt(dict: FLMutableDict) -> Self {
FLValue_Retain(dict as FLValue);
unsafe {
FLValue_Retain(dict as FLValue);
}
Self { cbl_ref: dict }
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ pub fn dump_instances() {
//////// REFCOUNT SUPPORT (INTERNAL)

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

pub(crate) unsafe fn release<T>(cbl_ref: *mut T) {
CBL_Release(cbl_ref.cast::<CBLRefCounted>());
unsafe {
CBL_Release(cbl_ref.cast::<CBLRefCounted>());
}
}

//////// ANDROID INIT
Expand Down
10 changes: 6 additions & 4 deletions src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,11 @@ unsafe extern "C" fn invoke_log_callback(
c_level: CBLLogLevel,
msg: FLString,
) {
if let Some(cb) = LOG_CALLBACK {
let domain = Domain::from_u8(c_domain).unwrap_or(Domain::None);
let level = Level::from_u8(c_level).unwrap_or(Level::None);
cb(domain, level, msg.as_str().unwrap_or("Empty error"));
unsafe {
if let Some(cb) = LOG_CALLBACK {
let domain = Domain::from_u8(c_domain).unwrap_or(Domain::None);
let level = Level::from_u8(c_level).unwrap_or(Level::None);
cb(domain, level, msg.as_str().unwrap_or("Empty error"));
}
}
}
6 changes: 4 additions & 2 deletions src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ pub enum QueryLanguage {

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

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

(*callback)(&query, &token);
unsafe {
(*callback)(&query, &token);
}
}

/** A compiled database query. */
Expand Down
Loading