Skip to content

Commit 2b32ca7

Browse files
committed
Clarify Id::retain safety when T is not 'static
1 parent 80e2451 commit 2b32ca7

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

objc2/src/rc/id.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,14 @@ impl<T: Message + ?Sized, O: Ownership> Id<T, O> {
175175
///
176176
/// Returns `None` if the pointer was null.
177177
///
178+
///
178179
/// # Safety
179180
///
180181
/// The caller must ensure the given object has +1 retain count, and that
181182
/// the object pointer otherwise follows the same safety requirements as
182183
/// in [`Id::retain`].
183184
///
185+
///
184186
/// # Example
185187
///
186188
/// ```no_run
@@ -309,6 +311,7 @@ impl<T: Message, O: Ownership> Id<T, O> {
309311
///
310312
/// Returns `None` if the pointer was null.
311313
///
314+
///
312315
/// # Safety
313316
///
314317
/// The caller must ensure that the ownership is correct; that is, there
@@ -320,6 +323,10 @@ impl<T: Message, O: Ownership> Id<T, O> {
320323
/// dereferencable and initialized, see the [`std::ptr`] module for more
321324
/// information).
322325
///
326+
/// Finally, if you do not know the concrete type of `T`, it may not be
327+
/// `'static`, and hence you must ensure that the data that `T` references
328+
/// lives for as long as `T`.
329+
///
323330
/// [`std::ptr`]: core::ptr
324331
//
325332
// This would be illegal:
@@ -580,13 +587,17 @@ impl<T: Message> Id<T, Owned> {
580587

581588
/// Promote a shared [`Id`] to an owned one, allowing it to be mutated.
582589
///
590+
///
583591
/// # Safety
584592
///
585593
/// The caller must ensure that there are no other pointers (including
586594
/// [`WeakId`][`super::WeakId`] pointers) to the same object.
587595
///
588596
/// This also means that the given [`Id`] should have a retain count of
589597
/// exactly 1 (except when autoreleases are involved).
598+
///
599+
/// In general, this is wildly unsafe, do see if you can find a different
600+
/// solution!
590601
#[inline]
591602
pub unsafe fn from_shared(obj: Id<T, Shared>) -> Self {
592603
// Note: We can't debug_assert retainCount because of autoreleases

0 commit comments

Comments
 (0)