Skip to content

Commit 689883a

Browse files
committed
Rename retain to reference, and wrap to take_ownership, and update doc comments
1 parent d7ec7f2 commit 689883a

File tree

10 files changed

+67
-60
lines changed

10 files changed

+67
-60
lines changed

src/collection.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,23 +56,23 @@ impl Collection {
5656

5757
//////// CONSTRUCTORS:
5858

59-
/// Takes ownership of the object and increase it's reference counter.
60-
pub(crate) fn retain(cbl_ref: *mut CBLCollection) -> Self {
59+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
60+
pub(crate) fn reference(cbl_ref: *mut CBLCollection) -> Self {
6161
Self {
6262
cbl_ref: unsafe { retain(cbl_ref) },
6363
}
6464
}
6565

66-
/// References the object without taking ownership and increasing it's reference counter
67-
pub(crate) const fn wrap(cbl_ref: *mut CBLCollection) -> Self {
66+
/// Takes ownership of the CBL ref, the reference counter is not increased so dropping the instance will free the ref.
67+
pub(crate) const fn take_ownership(cbl_ref: *mut CBLCollection) -> Self {
6868
Self { cbl_ref }
6969
}
7070

7171
////////
7272

7373
/// Returns the scope of the collection.
7474
pub fn scope(&self) -> Scope {
75-
unsafe { Scope::wrap(CBLCollection_Scope(self.get_ref())) }
75+
unsafe { Scope::take_ownership(CBLCollection_Scope(self.get_ref())) }
7676
}
7777

7878
/// Returns the collection name.
@@ -95,7 +95,7 @@ impl Collection {
9595

9696
/// Returns the collection's database.
9797
pub fn database(&self) -> Database {
98-
unsafe { Database::retain(CBLCollection_Database(self.get_ref())) }
98+
unsafe { Database::reference(CBLCollection_Database(self.get_ref())) }
9999
}
100100

101101
/// Returns the number of documents in the collection.
@@ -141,7 +141,7 @@ impl Drop for Collection {
141141

142142
impl Clone for Collection {
143143
fn clone(&self) -> Self {
144-
Self::retain(self.get_ref())
144+
Self::reference(self.get_ref())
145145
}
146146
}
147147

@@ -155,7 +155,7 @@ unsafe extern "C" fn c_collection_change_listener(
155155
) {
156156
let callback = context as *const CollectionChangeListener;
157157
if let Some(change) = change.as_ref() {
158-
let collection = Collection::retain(change.collection as *mut CBLCollection);
158+
let collection = Collection::reference(change.collection as *mut CBLCollection);
159159
let doc_ids = std::slice::from_raw_parts(change.docIDs, change.numDocs as usize)
160160
.iter()
161161
.filter_map(|doc_id| doc_id.to_string())

src/database.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ unsafe extern "C" fn c_database_change_listener(
140140
c_doc_ids: *mut FLString,
141141
) {
142142
let callback = context as *const DatabaseChangeListener;
143-
let database = Database::retain(db as *mut CBLDatabase);
143+
let database = Database::reference(db as *mut CBLDatabase);
144144

145145
let doc_ids = std::slice::from_raw_parts(c_doc_ids, num_docs as usize)
146146
.iter()
@@ -159,7 +159,7 @@ unsafe extern "C" fn c_database_buffer_notifications(
159159
) {
160160
let callback: BufferNotifications = std::mem::transmute(context);
161161

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

164164
callback(&database);
165165
}
@@ -180,15 +180,15 @@ impl CblRef for Database {
180180
impl Database {
181181
//////// CONSTRUCTORS:
182182

183-
/// Takes ownership of the object and increase it's reference counter.
184-
pub(crate) fn retain(cbl_ref: *mut CBLDatabase) -> Self {
183+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
184+
pub(crate) fn reference(cbl_ref: *mut CBLDatabase) -> Self {
185185
Self {
186186
cbl_ref: unsafe { retain(cbl_ref) },
187187
}
188188
}
189189

190-
/// References the object without taking ownership and increasing it's reference counter
191-
pub(crate) const fn wrap(cbl_ref: *mut CBLDatabase) -> Self {
190+
/// Takes ownership of the CBL ref, the reference counter is not increased so dropping the instance will free the ref.
191+
pub(crate) const fn take_ownership(cbl_ref: *mut CBLDatabase) -> Self {
192192
Self { cbl_ref }
193193
}
194194

@@ -217,7 +217,7 @@ impl Database {
217217
if db_ref.is_null() {
218218
return failure(err);
219219
}
220-
Ok(Self::wrap(db_ref))
220+
Ok(Self::take_ownership(db_ref))
221221
}
222222

223223
//////// OTHER STATIC METHODS:
@@ -418,7 +418,7 @@ impl Database {
418418
if scope.is_null() {
419419
None
420420
} else {
421-
Some(Scope::wrap(scope))
421+
Some(Scope::take_ownership(scope))
422422
}
423423
})
424424
}
@@ -445,7 +445,7 @@ impl Database {
445445
if collection.is_null() {
446446
None
447447
} else {
448-
Some(Collection::wrap(collection))
448+
Some(Collection::take_ownership(collection))
449449
}
450450
})
451451
}
@@ -474,7 +474,7 @@ impl Database {
474474
)
475475
};
476476

477-
check_error(&error).map(|()| Collection::wrap(collection))
477+
check_error(&error).map(|()| Collection::take_ownership(collection))
478478
}
479479

480480
/// Delete an existing collection.
@@ -499,7 +499,7 @@ impl Database {
499499
let mut error = CBLError::default();
500500
let scope = unsafe { CBLDatabase_DefaultScope(self.get_ref(), &mut error) };
501501

502-
check_error(&error).map(|()| Scope::wrap(scope))
502+
check_error(&error).map(|()| Scope::take_ownership(scope))
503503
}
504504

505505
/// Returns the default collection.
@@ -511,7 +511,7 @@ impl Database {
511511
if collection.is_null() {
512512
None
513513
} else {
514-
Some(Collection::wrap(collection))
514+
Some(Collection::take_ownership(collection))
515515
}
516516
})
517517
}
@@ -526,7 +526,7 @@ impl Database {
526526
if collection.is_null() {
527527
Err(Error::cbl_error(CouchbaseLiteError::NotFound))
528528
} else {
529-
Ok(Collection::wrap(collection))
529+
Ok(Collection::take_ownership(collection))
530530
}
531531
}
532532

@@ -596,6 +596,6 @@ impl Drop for Database {
596596

597597
impl Clone for Database {
598598
fn clone(&self) -> Self {
599-
Self::retain(self.get_ref())
599+
Self::reference(self.get_ref())
600600
}
601601
}

src/document.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ unsafe extern "C" fn c_conflict_handler(
7575
let callback: ConflictHandler = std::mem::transmute(context);
7676

7777
callback(
78-
&mut Document::retain(document_being_saved),
79-
&Document::retain(conflicting_document as *mut CBLDocument),
78+
&mut Document::reference(document_being_saved),
79+
&Document::reference(conflicting_document as *mut CBLDocument),
8080
)
8181
}
8282

@@ -92,7 +92,7 @@ unsafe extern "C" fn c_database_document_change_listener(
9292
c_doc_id: FLString,
9393
) {
9494
let callback = context as *const DatabaseDocumentChangeListener;
95-
let database = Database::retain(db as *mut CBLDatabase);
95+
let database = Database::reference(db as *mut CBLDatabase);
9696
(*callback)(&database, c_doc_id.to_string());
9797
}
9898

@@ -116,7 +116,7 @@ impl Database {
116116
failure(error)
117117
};
118118
}
119-
Ok(Document::wrap(doc))
119+
Ok(Document::take_ownership(doc))
120120
}
121121
}
122122

@@ -315,7 +315,7 @@ unsafe extern "C" fn c_collection_document_change_listener(
315315
) {
316316
let callback = context as *const CollectionDocumentChangeListener;
317317
if let Some(change) = change.as_ref() {
318-
let collection = Collection::retain(change.collection as *mut CBLCollection);
318+
let collection = Collection::reference(change.collection as *mut CBLCollection);
319319
(*callback)(collection, change.docID.to_string());
320320
}
321321
}
@@ -340,7 +340,7 @@ impl Collection {
340340
failure(error)
341341
};
342342
}
343-
Ok(Document::wrap(doc))
343+
Ok(Document::take_ownership(doc))
344344
}
345345
}
346346

@@ -515,26 +515,26 @@ impl Document {
515515
/// Creates a new, empty document in memory, with an automatically generated unique ID.
516516
/// It will not be added to a database until saved.
517517
pub fn new() -> Self {
518-
unsafe { Self::wrap(CBLDocument_Create()) }
518+
unsafe { Self::take_ownership(CBLDocument_Create()) }
519519
}
520520

521521
/// Creates a new, empty document in memory, with the given ID.
522522
/// It will not be added to a database until saved.
523523
pub fn new_with_id(id: &str) -> Self {
524-
unsafe { Self::wrap(CBLDocument_CreateWithID(from_str(id).get_ref())) }
524+
unsafe { Self::take_ownership(CBLDocument_CreateWithID(from_str(id).get_ref())) }
525525
}
526526

527-
/// Takes ownership of the object and increase it's reference counter.
528-
pub(crate) fn retain(cbl_ref: *mut CBLDocument) -> Self {
527+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
528+
pub(crate) fn reference(cbl_ref: *mut CBLDocument) -> Self {
529529
unsafe {
530530
Self {
531531
cbl_ref: retain(cbl_ref),
532532
}
533533
}
534534
}
535535

536-
/// References the object without taking ownership and increasing it's reference counter
537-
pub(crate) const fn wrap(cbl_ref: *mut CBLDocument) -> Self {
536+
/// Takes ownership of the CBL ref, the reference counter is not increased so dropping the instance will free the ref.
537+
pub(crate) const fn take_ownership(cbl_ref: *mut CBLDocument) -> Self {
538538
Self { cbl_ref }
539539
}
540540

@@ -607,6 +607,6 @@ impl Drop for Document {
607607

608608
impl Clone for Document {
609609
fn clone(&self) -> Self {
610-
Self::retain(self.get_ref())
610+
Self::reference(self.get_ref())
611611
}
612612
}

src/encryptable.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ impl CblRef for Encryptable {
6666

6767
impl From<*mut CBLEncryptable> for Encryptable {
6868
fn from(cbl_ref: *mut CBLEncryptable) -> Self {
69-
Self::retain(cbl_ref)
69+
Self::reference(cbl_ref)
7070
}
7171
}
7272

7373
impl Encryptable {
7474
//////// CONSTRUCTORS:
7575

76-
/// Takes ownership of the object and increase it's reference counter.
77-
pub(crate) fn retain(cbl_ref: *mut CBLEncryptable) -> Self {
76+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
77+
pub(crate) fn reference(cbl_ref: *mut CBLEncryptable) -> Self {
7878
Self {
7979
cbl_ref: unsafe { retain(cbl_ref) },
8080
}

src/fleece.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl Value {
323323
pub fn get_encryptable_value(&self) -> Encryptable {
324324
unsafe {
325325
let encryptable = FLDict_GetEncryptableValue(FLValue_AsDict(self.get_ref()));
326-
Encryptable::retain(encryptable as *mut CBLEncryptable)
326+
Encryptable::reference(encryptable as *mut CBLEncryptable)
327327
}
328328
}
329329

@@ -598,7 +598,7 @@ impl Dict {
598598
pub fn get_encryptable_value(&self) -> Encryptable {
599599
unsafe {
600600
let encryptable = FLDict_GetEncryptableValue(self.get_ref());
601-
Encryptable::retain(encryptable as *mut CBLEncryptable)
601+
Encryptable::reference(encryptable as *mut CBLEncryptable)
602602
}
603603
}
604604

src/index.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,16 +148,16 @@ impl CblRef for QueryIndex {
148148
impl QueryIndex {
149149
//////// CONSTRUCTORS:
150150

151-
/// Takes ownership of the object and increase it's reference counter.
151+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
152152
#[allow(dead_code)]
153-
pub(crate) fn retain(cbl_ref: *mut CBLQueryIndex) -> Self {
153+
pub(crate) fn reference(cbl_ref: *mut CBLQueryIndex) -> Self {
154154
Self {
155155
cbl_ref: unsafe { retain(cbl_ref) },
156156
}
157157
}
158158

159-
/// References the object without taking ownership and increasing it's reference counter
160-
pub(crate) const fn wrap(cbl_ref: *mut CBLQueryIndex) -> Self {
159+
/// Takes ownership of the CBL ref, the reference counter is not increased so dropping the instance will free the ref.
160+
pub(crate) const fn take_ownership(cbl_ref: *mut CBLQueryIndex) -> Self {
161161
Self { cbl_ref }
162162
}
163163

@@ -174,7 +174,7 @@ impl QueryIndex {
174174

175175
/// Returns the collection that the index belongs to.
176176
pub fn collection(&self) -> Collection {
177-
unsafe { Collection::retain(CBLQueryIndex_Collection(self.get_ref())) }
177+
unsafe { Collection::reference(CBLQueryIndex_Collection(self.get_ref())) }
178178
}
179179
}
180180

@@ -292,7 +292,7 @@ impl Collection {
292292
let slice = from_str(name);
293293
let index = unsafe { CBLCollection_GetIndex(self.get_ref(), slice.get_ref(), &mut err) };
294294
if !err {
295-
return Ok(QueryIndex::wrap(index));
295+
return Ok(QueryIndex::take_ownership(index));
296296
}
297297
failure(err)
298298
}

src/query.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ unsafe extern "C" fn c_query_change_listener(
4848
token: *mut CBLListenerToken,
4949
) {
5050
let callback = context as *const ChangeListener;
51-
let query = Query::wrap(query.cast::<CBLQuery>());
51+
let query = Query::reference(query.cast::<CBLQuery>());
5252
let token = ListenerToken::new(token);
5353

5454
(*callback)(&query, &token);
@@ -88,16 +88,22 @@ impl Query {
8888
return failure(err);
8989
}
9090

91-
Ok(Self { cbl_ref: q })
91+
Ok(Self::take_ownership(q))
9292
}
9393
}
9494

95-
pub(crate) fn wrap(cbl_ref: *mut CBLQuery) -> Self {
95+
/// Increase the reference counter of the CBL ref, so dropping the instance will NOT free the ref.
96+
pub(crate) fn reference(cbl_ref: *mut CBLQuery) -> Self {
9697
Self {
9798
cbl_ref: unsafe { retain(cbl_ref) },
9899
}
99100
}
100101

102+
/// Takes ownership of the CBL ref, the reference counter is not increased so dropping the instance will free the ref.
103+
pub(crate) const fn take_ownership(cbl_ref: *mut CBLQuery) -> Self {
104+
Self { cbl_ref }
105+
}
106+
101107
/** Assigns values to the query's parameters.
102108
These values will be substited for those parameters whenever the query is executed,
103109
until they are next assigned.

src/replicator.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ unsafe extern "C" fn c_replication_push_filter(
262262
flags: CBLDocumentFlags,
263263
) -> bool {
264264
let repl_conf_context = context as *const ReplicationConfigurationContext;
265-
let document = Document::retain(document.cast::<CBLDocument>());
265+
let document = Document::reference(document.cast::<CBLDocument>());
266266
let (is_deleted, is_access_removed) = read_document_flags(flags);
267267

268268
(*repl_conf_context)
@@ -276,7 +276,7 @@ unsafe extern "C" fn c_replication_pull_filter(
276276
flags: CBLDocumentFlags,
277277
) -> bool {
278278
let repl_conf_context = context as *const ReplicationConfigurationContext;
279-
let document = Document::retain(document.cast::<CBLDocument>());
279+
let document = Document::reference(document.cast::<CBLDocument>());
280280
let (is_deleted, is_access_removed) = read_document_flags(flags);
281281

282282
(*repl_conf_context)
@@ -307,12 +307,12 @@ unsafe extern "C" fn c_replication_conflict_resolver(
307307
let local_document = if local_document.is_null() {
308308
None
309309
} else {
310-
Some(Document::retain(local_document as *mut CBLDocument))
310+
Some(Document::reference(local_document as *mut CBLDocument))
311311
};
312312
let remote_document = if remote_document.is_null() {
313313
None
314314
} else {
315-
Some(Document::retain(remote_document as *mut CBLDocument))
315+
Some(Document::reference(remote_document as *mut CBLDocument))
316316
};
317317

318318
(*repl_conf_context)

0 commit comments

Comments
 (0)