Skip to content

Commit

Permalink
Implement ToOwned as an easy way of generically cloning types
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Jun 11, 2022
1 parent 5d3790b commit 5f5ff6c
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 1 deletion.
1 change: 1 addition & 0 deletions objc2-foundation/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* Added `NSSize`.
* Added `NSRect`.
* Implement `Borrow` and `BorrowMut` for all objects.
* Implement `ToOwned` for copyable types.

### Changed
* **BREAKING**: Removed the following helper traits in favor of inherent
Expand Down
14 changes: 14 additions & 0 deletions objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,13 @@ unsafe impl<T: Message> NSMutableCopying for NSArray<T, Shared> {
type Output = NSMutableArray<T, Shared>;
}

impl<T: Message> alloc::borrow::ToOwned for NSArray<T, Shared> {
type Owned = Id<NSArray<T, Shared>, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

unsafe impl<T: Message, O: Ownership> NSFastEnumeration for NSArray<T, O> {
type Item = T;
}
Expand Down Expand Up @@ -334,6 +341,13 @@ unsafe impl<T: Message> NSMutableCopying for NSMutableArray<T, Shared> {
type Output = NSMutableArray<T, Shared>;
}

impl<T: Message> alloc::borrow::ToOwned for NSMutableArray<T, Shared> {
type Owned = Id<NSMutableArray<T, Shared>, Owned>;
fn to_owned(&self) -> Self::Owned {
self.mutable_copy()
}
}

unsafe impl<T: Message, O: Ownership> NSFastEnumeration for NSMutableArray<T, O> {
type Item = T;
}
Expand Down
7 changes: 7 additions & 0 deletions objc2-foundation/src/attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ unsafe impl NSMutableCopying for NSAttributedString {
type Output = NSMutableAttributedString;
}

impl alloc::borrow::ToOwned for NSAttributedString {
type Owned = Id<NSAttributedString, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

#[cfg(test)]
mod tests {
use alloc::string::ToString;
Expand Down
14 changes: 14 additions & 0 deletions objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ unsafe impl NSMutableCopying for NSData {
type Output = NSMutableData;
}

impl alloc::borrow::ToOwned for NSData {
type Owned = Id<NSData, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

impl AsRef<[u8]> for NSData {
fn as_ref(&self) -> &[u8] {
self.bytes()
Expand Down Expand Up @@ -220,6 +227,13 @@ unsafe impl NSMutableCopying for NSMutableData {
type Output = NSMutableData;
}

impl alloc::borrow::ToOwned for NSMutableData {
type Owned = Id<NSMutableData, Owned>;
fn to_owned(&self) -> Self::Owned {
self.mutable_copy()
}
}

impl AsRef<[u8]> for NSMutableData {
fn as_ref(&self) -> &[u8] {
self.bytes()
Expand Down
7 changes: 7 additions & 0 deletions objc2-foundation/src/mutable_attributed_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,13 @@ unsafe impl NSMutableCopying for NSMutableAttributedString {
type Output = NSMutableAttributedString;
}

impl alloc::borrow::ToOwned for NSMutableAttributedString {
type Owned = Id<NSMutableAttributedString, Owned>;
fn to_owned(&self) -> Self::Owned {
self.mutable_copy()
}
}

#[cfg(test)]
mod tests {
use alloc::string::ToString;
Expand Down
7 changes: 7 additions & 0 deletions objc2-foundation/src/mutable_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ unsafe impl NSMutableCopying for NSMutableString {
type Output = NSMutableString;
}

impl alloc::borrow::ToOwned for NSMutableString {
type Owned = Id<NSMutableString, Owned>;
fn to_owned(&self) -> Self::Owned {
self.mutable_copy()
}
}

impl AddAssign<&NSString> for NSMutableString {
#[inline]
fn add_assign(&mut self, other: &NSString) {
Expand Down
9 changes: 8 additions & 1 deletion objc2-foundation/src/string.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::borrow::ToOwned;
use core::cmp;
use core::ffi::c_void;
use core::fmt;
Expand All @@ -6,7 +7,6 @@ use core::slice;
use core::str;
use std::os::raw::c_char;

use alloc::borrow::ToOwned;
use objc2::ffi;
use objc2::rc::DefaultId;
use objc2::rc::{autoreleasepool, AutoreleasePool};
Expand Down Expand Up @@ -255,6 +255,13 @@ unsafe impl NSMutableCopying for NSString {
type Output = NSMutableString;
}

impl ToOwned for NSString {
type Owned = Id<NSString, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

impl fmt::Display for NSString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// The call to `to_owned` is unfortunate, but is required to work
Expand Down
7 changes: 7 additions & 0 deletions objc2-foundation/src/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,13 @@ unsafe impl NSCopying for NSUUID {
type Output = NSUUID;
}

impl alloc::borrow::ToOwned for NSUUID {
type Owned = Id<NSUUID, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 7 additions & 0 deletions objc2-foundation/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,13 @@ unsafe impl<T: 'static> NSCopying for NSValue<T> {
type Output = NSValue<T>;
}

impl<T: 'static> alloc::borrow::ToOwned for NSValue<T> {
type Owned = Id<NSValue<T>, Shared>;
fn to_owned(&self) -> Self::Owned {
self.copy()
}
}

impl<T: 'static + Copy + Encode + Ord> Ord for NSValue<T> {
fn cmp(&self, other: &Self) -> Ordering {
self.get().cmp(&other.get())
Expand Down

0 comments on commit 5f5ff6c

Please sign in to comment.