use objc2::extern_protocol;
use objc2::rc::Retained;
use objc2::runtime::NSZone;
use objc2::runtime::ProtocolObject;
use objc2::Message;
pub unsafe trait CopyingHelper: Message {
type Result: Message;
}
pub unsafe trait MutableCopyingHelper: Message {
type Result: Message;
}
unsafe impl<P: ?Sized> CopyingHelper for ProtocolObject<P> {
type Result = Self;
}
unsafe impl<P: ?Sized> MutableCopyingHelper for ProtocolObject<P> {
type Result = Self;
}
extern_protocol!(
#[allow(clippy::missing_safety_doc)]
pub unsafe trait NSCopying {
#[unsafe(method(copy))]
#[unsafe(method_family = copy)]
#[optional]
fn copy(&self) -> Retained<Self::Result>
where
Self: CopyingHelper;
#[unsafe(method(copyWithZone:))]
#[unsafe(method_family = copy)]
unsafe fn copyWithZone(&self, zone: *mut NSZone) -> Retained<Self::Result>
where
Self: CopyingHelper;
}
);
extern_protocol!(
/// A protocol to provide mutable copies of objects.
///
/// Only classes that have an “immutable vs. mutable” distinction should
/// adopt this protocol. Use the [`MutableCopyingHelper`] trait to specify
/// the return type after copying.
///
/// See [Apple's documentation][apple-doc] for details.
///
/// [apple-doc]: https://developer.apple.com/documentation/foundation/nsmutablecopying
///
///
/// # Example
///
/// Implement [`NSCopying`] and [`NSMutableCopying`] for a class pair like
/// `NSString` and `NSMutableString`.
///
/// ```ignore
/// // Immutable copies return NSString
///
/// unsafe impl NSCopying for NSString {}
/// unsafe impl CopyingHelper for NSString {
/// type Result = NSString;
/// }
/// unsafe impl NSCopying for NSMutableString {}
/// unsafe impl CopyingHelper for NSMutableString {
/// type Result = NSString;
/// }
///
/// // Mutable copies return NSMutableString
///
/// unsafe impl NSMutableCopying for NSString {}
/// unsafe impl MutableCopyingHelper for NSString {
/// type Result = NSMutableString;
/// }
/// unsafe impl NSMutableCopying for NSMutableString {}
/// unsafe impl MutableCopyingHelper for NSMutableString {
/// type Result = NSMutableString;
/// }
/// ```
#[allow(clippy::missing_safety_doc)]
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`.
#[unsafe(method(mutableCopy))]
#[unsafe(method_family = mutableCopy)]
#[optional]
fn mutableCopy(&self) -> Retained<Self::Result>
where
Self: MutableCopyingHelper;
/// Returns a new instance that's a mutable copy of the receiver.
///
/// This is only used when implementing `NSMutableCopying`, call
/// [`mutableCopy`][NSMutableCopying::mutableCopy] instead.
///
///
/// # Safety
///
/// The zone pointer must be valid or NULL.
#[unsafe(method(mutableCopyWithZone:))]
#[unsafe(method_family = mutableCopy)]
unsafe fn mutableCopyWithZone(&self, zone: *mut NSZone) -> Retained<Self::Result>
where
Self: MutableCopyingHelper;
}
);