-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor CopyHelper and NS[Mutable]Copying setup
Add CounterpartOrSelf instead of CopyHelper, which lives in objc2 and is much nicer to use. This also allows us to move NSCopying and NSMutableCopying back to reside in Foundation (note: We might have to at some point implement a small hack for these traits acting as if they contain the `copy` method, while it's actually `NSObject` that does).
- Loading branch information
Showing
25 changed files
with
761 additions
and
685 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use objc2::mutability::CounterpartOrSelf; | ||
use objc2::rc::Id; | ||
use objc2::runtime::NSZone; | ||
use objc2::{extern_protocol, ProtocolType}; | ||
|
||
extern_protocol!( | ||
/// A protocol to provide functional copies of objects. | ||
/// | ||
/// This is similar to Rust's [`Clone`] trait, along with sharing a few | ||
/// similarities to the [`std::borrow::ToOwned`] trait with regards to the | ||
/// output type. | ||
/// | ||
/// See [Apple's documentation][apple-doc] for details. | ||
/// | ||
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nscopying | ||
pub unsafe trait NSCopying { | ||
/// Returns a new instance that's a copy of the receiver. | ||
/// | ||
/// The output type is the immutable counterpart of the object, which is | ||
/// usually `Self`, but e.g. `NSMutableString` returns `NSString`. | ||
#[method_id(copy)] | ||
#[optional] | ||
fn copy(&self) -> Id<Self::Immutable> | ||
where | ||
Self: Sized, | ||
Self: CounterpartOrSelf; | ||
|
||
/// Returns a new instance that's a copy of the receiver. | ||
/// | ||
/// This is only used when implementing `NSCopying`, use | ||
/// [`copy`][NSCopying::copy] instead. | ||
/// | ||
/// | ||
/// # Safety | ||
/// | ||
/// The zone pointer must be valid or NULL. | ||
#[method_id(copyWithZone:)] | ||
unsafe fn copyWithZone(&self, zone: *mut NSZone) -> Id<Self::Immutable> | ||
where | ||
Self: Sized, | ||
Self: CounterpartOrSelf; | ||
} | ||
|
||
unsafe impl ProtocolType for dyn NSCopying { | ||
const NAME: &'static str = "NSCopying"; | ||
} | ||
); | ||
|
||
extern_protocol!( | ||
/// A protocol to provide mutable copies of objects. | ||
/// | ||
/// Only classes that have an “immutable vs. mutable” distinction should adopt | ||
/// this protocol. | ||
/// | ||
/// See [Apple's documentation][apple-doc] for details. | ||
/// | ||
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nsmutablecopying | ||
pub unsafe trait NSMutableCopying { | ||
/// Returns a new instance that's a mutable copy of the receiver. | ||
/// | ||
/// The output type is the mutable counterpart of the object. E.g. both | ||
/// `NSString` and `NSMutableString` return `NSMutableString`. | ||
#[method_id(mutableCopy)] | ||
#[optional] | ||
fn mutableCopy(&self) -> Id<Self::Mutable> | ||
where | ||
Self: Sized, | ||
Self: CounterpartOrSelf; | ||
|
||
/// Returns a new instance that's a mutable copy of the receiver. | ||
/// | ||
/// This is only used when implementing `NSMutableCopying`, use | ||
/// [`mutableCopy`][NSMutableCopying::mutableCopy] instead. | ||
/// | ||
/// | ||
/// # Safety | ||
/// | ||
/// The zone pointer must be valid or NULL. | ||
#[method_id(mutableCopyWithZone:)] | ||
unsafe fn mutableCopyWithZone(&self, zone: *mut NSZone) -> Id<Self::Mutable> | ||
where | ||
Self: Sized, | ||
Self: CounterpartOrSelf; | ||
} | ||
|
||
unsafe impl ProtocolType for dyn NSMutableCopying { | ||
const NAME: &'static str = "NSMutableCopying"; | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.