Skip to content

Commit

Permalink
Require the Ownership bound on Id
Browse files Browse the repository at this point in the history
Will allow us to do some nice things when/if GATs stabilize
  • Loading branch information
madsmtm committed Sep 9, 2021
1 parent 5dee21a commit e36b8de
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
8 changes: 4 additions & 4 deletions objc2_foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ pub trait INSArray: INSObject {
}
}

pub struct NSArray<T, O = Owned> {
pub struct NSArray<T, O: Ownership = Owned> {
item: PhantomData<Id<T, O>>,
}

object_impl!(NSArray<T, O>);
object_impl!(NSArray<T, O: Ownership>);

impl<T, O> INSObject for NSArray<T, O>
where
Expand Down Expand Up @@ -340,11 +340,11 @@ pub trait INSMutableArray: INSArray {
}
}

pub struct NSMutableArray<T, O = Owned> {
pub struct NSMutableArray<T, O: Ownership = Owned> {
item: PhantomData<Id<T, O>>,
}

object_impl!(NSMutableArray<T, O>);
object_impl!(NSMutableArray<T, O: Ownership>);

impl<T, O> INSObject for NSMutableArray<T, O>
where
Expand Down
10 changes: 5 additions & 5 deletions objc2_foundation/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ macro_rules! object_impl {
($name:ident) => (
object_impl!($name,);
);
($name:ident<$($t:ident),+>) => (
object_impl!($name, $($t),+);
($name:ident<$($t:ident$(: $b:ident)?),+>) => (
object_impl!($name, $($t$(: $b)?),+);
);
($name:ident, $($t:ident),*) => (
unsafe impl<$($t),*> ::objc2::Message for $name<$($t),*> { }
($name:ident, $($t:ident$(: $b:ident)?),*) => (
unsafe impl<$($t$(:($b))?),*> ::objc2::Message for $name<$($t),*> { }

unsafe impl<$($t),*> ::objc2::RefEncode for $name<$($t),*> {
unsafe impl<$($t$(: $b)?),*> ::objc2::RefEncode for $name<$($t),*> {
const ENCODING_REF: ::objc2::Encoding<'static> = ::objc2::Encoding::Object;
}
);
Expand Down
28 changes: 14 additions & 14 deletions objc2_id/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl Ownership for super::Shared {}
/// ```
#[repr(transparent)]
// TODO: Figure out if `Message` bound on `T` would be better here?
pub struct Id<T, O = Owned> {
pub struct Id<T, O: Ownership = Owned> {
/// A pointer to the contained object. The pointer is always retained.
///
/// It is important that this is `NonNull`, since we want to dereference
Expand Down Expand Up @@ -311,7 +311,7 @@ impl<T: Message> Clone for Id<T, Shared> {
/// borrowed data.
///
/// [dropck_eyepatch]: https://doc.rust-lang.org/nightly/nomicon/dropck.html#an-escape-hatch
impl<T, O> Drop for Id<T, O> {
impl<T, O: Ownership> Drop for Id<T, O> {
/// Releases the retained object.
///
/// The contained object's destructor (if it has one) is never run!
Expand Down Expand Up @@ -354,7 +354,7 @@ unsafe impl<T: Send> Send for Id<T, Owned> {}
/// access as having a `T` directly.
unsafe impl<T: Sync> Sync for Id<T, Owned> {}

impl<T, O> Deref for Id<T, O> {
impl<T, O: Ownership> Deref for Id<T, O> {
type Target = T;

/// Obtain an immutable reference to the object.
Expand All @@ -374,7 +374,7 @@ impl<T> DerefMut for Id<T, Owned> {
}
}

impl<T: PartialEq, O> PartialEq for Id<T, O> {
impl<T: PartialEq, O: Ownership> PartialEq for Id<T, O> {
#[inline]
fn eq(&self, other: &Self) -> bool {
(**self).eq(&**other)
Expand All @@ -386,9 +386,9 @@ impl<T: PartialEq, O> PartialEq for Id<T, O> {
}
}

impl<T: Eq, O> Eq for Id<T, O> {}
impl<T: Eq, O: Ownership> Eq for Id<T, O> {}

impl<T: PartialOrd, O> PartialOrd for Id<T, O> {
impl<T: PartialOrd, O: Ownership> PartialOrd for Id<T, O> {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
(**self).partial_cmp(&**other)
Expand All @@ -411,32 +411,32 @@ impl<T: PartialOrd, O> PartialOrd for Id<T, O> {
}
}

impl<T: Ord, O> Ord for Id<T, O> {
impl<T: Ord, O: Ownership> Ord for Id<T, O> {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
(**self).cmp(&**other)
}
}

impl<T: hash::Hash, O> hash::Hash for Id<T, O> {
impl<T: hash::Hash, O: Ownership> hash::Hash for Id<T, O> {
fn hash<H: hash::Hasher>(&self, state: &mut H) {
(**self).hash(state)
}
}

impl<T: fmt::Display, O> fmt::Display for Id<T, O> {
impl<T: fmt::Display, O: Ownership> fmt::Display for Id<T, O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}

impl<T: fmt::Debug, O> fmt::Debug for Id<T, O> {
impl<T: fmt::Debug, O: Ownership> fmt::Debug for Id<T, O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}

impl<T, O> fmt::Pointer for Id<T, O> {
impl<T, O: Ownership> fmt::Pointer for Id<T, O> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.ptr.as_ptr(), f)
}
Expand Down Expand Up @@ -472,7 +472,7 @@ impl<I: ExactSizeIterator> ExactSizeIterator for Id<I, Owned> {

impl<I: FusedIterator> FusedIterator for Id<I, Owned> {}

impl<T, O> borrow::Borrow<T> for Id<T, O> {
impl<T, O: Ownership> borrow::Borrow<T> for Id<T, O> {
fn borrow(&self) -> &T {
&**self
}
Expand All @@ -484,7 +484,7 @@ impl<T> borrow::BorrowMut<T> for Id<T, Owned> {
}
}

impl<T, O> AsRef<T> for Id<T, O> {
impl<T, O: Ownership> AsRef<T> for Id<T, O> {
fn as_ref(&self) -> &T {
&**self
}
Expand All @@ -507,7 +507,7 @@ impl<T> AsMut<T> for Id<T, Owned> {
//
// See https://doc.rust-lang.org/1.54.0/src/alloc/boxed.rs.html#1652-1675
// and the `Arc` implementation.
impl<T, O> Unpin for Id<T, O> {}
impl<T, O: Ownership> Unpin for Id<T, O> {}

// TODO: When stabilized impl Fn traits & CoerceUnsized

Expand Down

0 comments on commit e36b8de

Please sign in to comment.