There's a lot of places in objc2_foundation that would benefit from being able to implement From for specific Ids:
NSArray::from_vec
NSArray::into_vec
NSData::from_vec
NSDictionary::from_keys_and_objects
NSString::from_str
- And so on...
Same goes for traits like Default.
But unfortunately, we can't create these implementations because Id is defined in another crate, see RFC 2451. I tried creating a helper trait FromId for this purpose, but that doesn't work either:
pub trait FromId<T, O: Ownership> {
fn from_id(obj: Id<T, O>) -> Self;
}
impl<T, O: Ownership, X: FromId<T, O>> From<Id<T, O>> for X {
fn from(obj: Id<T, O>) -> Self {
Self::from_id(obj)
}
}
// Errors with:
// note: conflicting implementation in crate `core`:
// - impl<T> From<T> for T;
// note: downstream crates may implement trait `FromId<_, _>` for type `Id<_, _>`
Though doing it for Default would:
// In objc2:
pub trait IdDefault: Sized {
type Ownership: Ownership;
fn id_default() -> Id<Self, Self::Ownership>;
}
impl<T: IdDefault> Default for Id<T, T::Ownership> {
fn default() -> Self {
T::id_default()
}
}
// In objc2_foundation:
impl IdDefault for NSString {
type Ownership = <NSString as INSObject>::Ownership;
fn id_default() -> Id<Self, Self::Ownership> {
<NSString as INSObject>::new()
}
}
So possible solutions:
- Move
objc2_foundation to objc2::foundation (under a feature flag)
- Move
Id to objc2_foundation?
There's a lot of places in
objc2_foundationthat would benefit from being able to implementFromfor specificIds:NSArray::from_vecNSArray::into_vecNSData::from_vecNSDictionary::from_keys_and_objectsNSString::from_strSame goes for traits like
Default.But unfortunately, we can't create these implementations because
Idis defined in another crate, see RFC 2451. I tried creating a helper traitFromIdfor this purpose, but that doesn't work either:Though doing it for
Defaultwould:So possible solutions:
objc2_foundationtoobjc2::foundation(under a feature flag)Idtoobjc2_foundation?