Skip to content

Commit 9aab415

Browse files
committed
Add DefaultId helper trait
1 parent 183331a commit 9aab415

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

objc2/src/rc/id_traits.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,24 @@ impl<T: Message> SliceIdMut for [Id<T, Owned>] {
5454
unsafe { &mut *ptr }
5555
}
5656
}
57+
58+
/// Helper trait to implement [`Default`] on types whoose default value is an
59+
/// [`Id`].
60+
// TODO: Remove `Sized` bound.
61+
// TODO: Maybe make this `unsafe` and provide a default implementation?
62+
pub trait DefaultId: Sized {
63+
/// Indicates whether the default value is mutable or immutable.
64+
type Ownership: Ownership;
65+
66+
/// The default [`Id`] for a type.
67+
///
68+
/// On most objects the implementation would just be sending a message to
69+
/// the `new` selector.
70+
fn default_id() -> Id<Self, Self::Ownership>;
71+
}
72+
73+
impl<T: DefaultId> Default for Id<T, T::Ownership> {
74+
fn default() -> Self {
75+
T::default_id()
76+
}
77+
}

objc2/src/rc/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod weak_id;
3535

3636
pub use self::autorelease::{autoreleasepool, AutoreleasePool, AutoreleaseSafe};
3737
pub use self::id::Id;
38-
pub use self::id_traits::{SliceId, SliceIdMut};
38+
pub use self::id_traits::{DefaultId, SliceId, SliceIdMut};
3939
pub use self::ownership::{Owned, Ownership, Shared};
4040
pub use self::weak_id::WeakId;
4141

0 commit comments

Comments
 (0)