Skip to content

Commit 4dd5852

Browse files
authored
Merge pull request #41 from madsmtm/id-traits
`Id` traits
2 parents 4ef1c0e + 8500794 commit 4dd5852

File tree

8 files changed

+403
-142
lines changed

8 files changed

+403
-142
lines changed

objc2/src/rc/id.rs

Lines changed: 16 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
use alloc::borrow;
21
use core::fmt;
3-
use core::hash;
4-
use core::iter::FusedIterator;
52
use core::marker::PhantomData;
63
use core::mem::ManuallyDrop;
74
use core::ops::{Deref, DerefMut};
85
use core::ptr::NonNull;
6+
use std::panic::{RefUnwindSafe, UnwindSafe};
97

108
use super::AutoreleasePool;
119
use super::{Owned, Ownership, Shared};
@@ -273,9 +271,11 @@ impl<T: Message> Id<T, Owned> {
273271
///
274272
/// # Safety
275273
///
276-
/// The caller must ensure that there are no other pointers to the same
277-
/// object (which also means that the given [`Id`] should have a retain
278-
/// count of exactly 1 all cases, except when autoreleases are involved).
274+
/// The caller must ensure that there are no other pointers (including
275+
/// [`WeakId`][`super::WeakId`] pointers) to the same object.
276+
///
277+
/// This also means that the given [`Id`] should have a retain count of
278+
/// exactly 1 (except when autoreleases are involved).
279279
#[inline]
280280
pub unsafe fn from_shared(obj: Id<T, Shared>) -> Self {
281281
// Note: We can't debug_assert retainCount because of autoreleases
@@ -379,6 +379,8 @@ impl<T, O: Ownership> Deref for Id<T, O> {
379379
type Target = T;
380380

381381
/// Obtain an immutable reference to the object.
382+
// Box doesn't inline, but that's because it's a compiler built-in
383+
#[inline]
382384
fn deref(&self) -> &T {
383385
// SAFETY: The pointer's validity is verified when the type is created
384386
unsafe { self.ptr.as_ref() }
@@ -387,6 +389,7 @@ impl<T, O: Ownership> Deref for Id<T, O> {
387389

388390
impl<T> DerefMut for Id<T, Owned> {
389391
/// Obtain a mutable reference to the object.
392+
#[inline]
390393
fn deref_mut(&mut self) -> &mut T {
391394
// SAFETY: The pointer's validity is verified when the type is created
392395
// Additionally, the owned `Id` is the unique owner of the object, so
@@ -395,142 +398,25 @@ impl<T> DerefMut for Id<T, Owned> {
395398
}
396399
}
397400

398-
impl<T: PartialEq, O: Ownership> PartialEq for Id<T, O> {
399-
#[inline]
400-
fn eq(&self, other: &Self) -> bool {
401-
(**self).eq(&**other)
402-
}
403-
404-
#[inline]
405-
fn ne(&self, other: &Self) -> bool {
406-
(**self).ne(&**other)
407-
}
408-
}
409-
410-
impl<T: Eq, O: Ownership> Eq for Id<T, O> {}
411-
412-
impl<T: PartialOrd, O: Ownership> PartialOrd for Id<T, O> {
413-
#[inline]
414-
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
415-
(**self).partial_cmp(&**other)
416-
}
417-
#[inline]
418-
fn lt(&self, other: &Self) -> bool {
419-
(**self).lt(&**other)
420-
}
421-
#[inline]
422-
fn le(&self, other: &Self) -> bool {
423-
(**self).le(&**other)
424-
}
425-
#[inline]
426-
fn ge(&self, other: &Self) -> bool {
427-
(**self).ge(&**other)
428-
}
429-
#[inline]
430-
fn gt(&self, other: &Self) -> bool {
431-
(**self).gt(&**other)
432-
}
433-
}
434-
435-
impl<T: Ord, O: Ownership> Ord for Id<T, O> {
436-
#[inline]
437-
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
438-
(**self).cmp(&**other)
439-
}
440-
}
441-
442-
impl<T: hash::Hash, O: Ownership> hash::Hash for Id<T, O> {
443-
fn hash<H: hash::Hasher>(&self, state: &mut H) {
444-
(**self).hash(state)
445-
}
446-
}
447-
448-
impl<T: fmt::Display, O: Ownership> fmt::Display for Id<T, O> {
449-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
450-
(**self).fmt(f)
451-
}
452-
}
453-
454-
impl<T: fmt::Debug, O: Ownership> fmt::Debug for Id<T, O> {
455-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
456-
(**self).fmt(f)
457-
}
458-
}
459-
460401
impl<T, O: Ownership> fmt::Pointer for Id<T, O> {
461402
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
462403
fmt::Pointer::fmt(&self.ptr.as_ptr(), f)
463404
}
464405
}
465406

466-
impl<I: Iterator> Iterator for Id<I, Owned> {
467-
type Item = I::Item;
468-
fn next(&mut self) -> Option<I::Item> {
469-
(**self).next()
470-
}
471-
fn size_hint(&self) -> (usize, Option<usize>) {
472-
(**self).size_hint()
473-
}
474-
fn nth(&mut self, n: usize) -> Option<I::Item> {
475-
(**self).nth(n)
476-
}
477-
}
478-
479-
impl<I: DoubleEndedIterator> DoubleEndedIterator for Id<I, Owned> {
480-
fn next_back(&mut self) -> Option<I::Item> {
481-
(**self).next_back()
482-
}
483-
fn nth_back(&mut self, n: usize) -> Option<I::Item> {
484-
(**self).nth_back(n)
485-
}
486-
}
487-
488-
impl<I: ExactSizeIterator> ExactSizeIterator for Id<I, Owned> {
489-
fn len(&self) -> usize {
490-
(**self).len()
491-
}
492-
}
493-
494-
impl<I: FusedIterator> FusedIterator for Id<I, Owned> {}
495-
496-
impl<T, O: Ownership> borrow::Borrow<T> for Id<T, O> {
497-
fn borrow(&self) -> &T {
498-
&**self
499-
}
500-
}
501-
502-
impl<T> borrow::BorrowMut<T> for Id<T, Owned> {
503-
fn borrow_mut(&mut self) -> &mut T {
504-
&mut **self
505-
}
506-
}
507-
508-
impl<T, O: Ownership> AsRef<T> for Id<T, O> {
509-
fn as_ref(&self) -> &T {
510-
&**self
511-
}
512-
}
513-
514-
impl<T> AsMut<T> for Id<T, Owned> {
515-
fn as_mut(&mut self) -> &mut T {
516-
&mut **self
517-
}
518-
}
519-
520-
// impl<F: Future + Unpin> Future for Id<F, Owned> {
521-
// type Output = F::Output;
522-
// fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
523-
// F::poll(Pin::new(&mut *self), cx)
524-
// }
525-
// }
526-
527407
// This is valid without `T: Unpin` because we don't implement any projection.
528408
//
529409
// See https://doc.rust-lang.org/1.54.0/src/alloc/boxed.rs.html#1652-1675
530410
// and the `Arc` implementation.
531411
impl<T, O: Ownership> Unpin for Id<T, O> {}
532412

533-
// TODO: When stabilized impl Fn traits & CoerceUnsized
413+
impl<T: RefUnwindSafe, O: Ownership> RefUnwindSafe for Id<T, O> {}
414+
415+
// Same as `Arc<T>`.
416+
impl<T: RefUnwindSafe> UnwindSafe for Id<T, Shared> {}
417+
418+
// Same as `Box<T>`.
419+
impl<T: UnwindSafe> UnwindSafe for Id<T, Owned> {}
534420

535421
#[cfg(test)]
536422
mod tests {

0 commit comments

Comments
 (0)