Skip to content

Unify ProtocolObject and AnyObject? #843

Description

@madsmtm

In Objective-C, &AnyObject is written as id and &ProtocolObject<dyn NSCoding> is written as id<NSCoding>; this is not a coincidence, the protocol restricts what types are allowed, but otherwise it behaves in the same way (it's essentially a bound).

Maybe it would make sense to merge these? Something like:

pub struct AnyObject<T: ?Sized = dyn Any> { ... }

#[deprecated]
pub type ProtocolObject<T> = AnyObject<T>;

(And at this point, possibly rename AnyObject back to Object?)

This also feels more natural when considering multi-protocol bounds once we can support that. E.g. Object -> no bound, Object<dyn NSCoding> -> bound to NSCoding, Object<dyn NSObjectProtocol + NSCoding> -> bound to both NSObjectProtocol and NSCoding.

This would also allow expressing "any thread-safe object" with AnyObject<dyn Any + Send + Sync>, which is kinda nice. Any is also actually the right tool for the job, since its requirements are 'static, which is exactly also the requirement for converting &T to &AnyObject.

Probably replaces #686.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-objc2Affects the `objc2`, `objc2-exception-helper` and/or `objc2-encode` cratesenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions