Skip to content

Commit 5f5ff6c

Browse files
committed
Implement ToOwned as an easy way of generically cloning types
1 parent 5d3790b commit 5f5ff6c

File tree

9 files changed

+72
-1
lines changed

9 files changed

+72
-1
lines changed

objc2-foundation/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
2929
* Added `NSSize`.
3030
* Added `NSRect`.
3131
* Implement `Borrow` and `BorrowMut` for all objects.
32+
* Implement `ToOwned` for copyable types.
3233

3334
### Changed
3435
* **BREAKING**: Removed the following helper traits in favor of inherent

objc2-foundation/src/array.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ unsafe impl<T: Message> NSMutableCopying for NSArray<T, Shared> {
185185
type Output = NSMutableArray<T, Shared>;
186186
}
187187

188+
impl<T: Message> alloc::borrow::ToOwned for NSArray<T, Shared> {
189+
type Owned = Id<NSArray<T, Shared>, Shared>;
190+
fn to_owned(&self) -> Self::Owned {
191+
self.copy()
192+
}
193+
}
194+
188195
unsafe impl<T: Message, O: Ownership> NSFastEnumeration for NSArray<T, O> {
189196
type Item = T;
190197
}
@@ -334,6 +341,13 @@ unsafe impl<T: Message> NSMutableCopying for NSMutableArray<T, Shared> {
334341
type Output = NSMutableArray<T, Shared>;
335342
}
336343

344+
impl<T: Message> alloc::borrow::ToOwned for NSMutableArray<T, Shared> {
345+
type Owned = Id<NSMutableArray<T, Shared>, Owned>;
346+
fn to_owned(&self) -> Self::Owned {
347+
self.mutable_copy()
348+
}
349+
}
350+
337351
unsafe impl<T: Message, O: Ownership> NSFastEnumeration for NSMutableArray<T, O> {
338352
type Item = T;
339353
}

objc2-foundation/src/attributed_string.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ unsafe impl NSMutableCopying for NSAttributedString {
118118
type Output = NSMutableAttributedString;
119119
}
120120

121+
impl alloc::borrow::ToOwned for NSAttributedString {
122+
type Owned = Id<NSAttributedString, Shared>;
123+
fn to_owned(&self) -> Self::Owned {
124+
self.copy()
125+
}
126+
}
127+
121128
#[cfg(test)]
122129
mod tests {
123130
use alloc::string::ToString;

objc2-foundation/src/data.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ unsafe impl NSMutableCopying for NSData {
9292
type Output = NSMutableData;
9393
}
9494

95+
impl alloc::borrow::ToOwned for NSData {
96+
type Owned = Id<NSData, Shared>;
97+
fn to_owned(&self) -> Self::Owned {
98+
self.copy()
99+
}
100+
}
101+
95102
impl AsRef<[u8]> for NSData {
96103
fn as_ref(&self) -> &[u8] {
97104
self.bytes()
@@ -220,6 +227,13 @@ unsafe impl NSMutableCopying for NSMutableData {
220227
type Output = NSMutableData;
221228
}
222229

230+
impl alloc::borrow::ToOwned for NSMutableData {
231+
type Owned = Id<NSMutableData, Owned>;
232+
fn to_owned(&self) -> Self::Owned {
233+
self.mutable_copy()
234+
}
235+
}
236+
223237
impl AsRef<[u8]> for NSMutableData {
224238
fn as_ref(&self) -> &[u8] {
225239
self.bytes()

objc2-foundation/src/mutable_attributed_string.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ unsafe impl NSMutableCopying for NSMutableAttributedString {
7474
type Output = NSMutableAttributedString;
7575
}
7676

77+
impl alloc::borrow::ToOwned for NSMutableAttributedString {
78+
type Owned = Id<NSMutableAttributedString, Owned>;
79+
fn to_owned(&self) -> Self::Owned {
80+
self.mutable_copy()
81+
}
82+
}
83+
7784
#[cfg(test)]
7885
mod tests {
7986
use alloc::string::ToString;

objc2-foundation/src/mutable_string.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ unsafe impl NSMutableCopying for NSMutableString {
9696
type Output = NSMutableString;
9797
}
9898

99+
impl alloc::borrow::ToOwned for NSMutableString {
100+
type Owned = Id<NSMutableString, Owned>;
101+
fn to_owned(&self) -> Self::Owned {
102+
self.mutable_copy()
103+
}
104+
}
105+
99106
impl AddAssign<&NSString> for NSMutableString {
100107
#[inline]
101108
fn add_assign(&mut self, other: &NSString) {

objc2-foundation/src/string.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::borrow::ToOwned;
12
use core::cmp;
23
use core::ffi::c_void;
34
use core::fmt;
@@ -6,7 +7,6 @@ use core::slice;
67
use core::str;
78
use std::os::raw::c_char;
89

9-
use alloc::borrow::ToOwned;
1010
use objc2::ffi;
1111
use objc2::rc::DefaultId;
1212
use objc2::rc::{autoreleasepool, AutoreleasePool};
@@ -255,6 +255,13 @@ unsafe impl NSMutableCopying for NSString {
255255
type Output = NSMutableString;
256256
}
257257

258+
impl ToOwned for NSString {
259+
type Owned = Id<NSString, Shared>;
260+
fn to_owned(&self) -> Self::Owned {
261+
self.copy()
262+
}
263+
}
264+
258265
impl fmt::Display for NSString {
259266
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260267
// The call to `to_owned` is unfortunate, but is required to work

objc2-foundation/src/uuid.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,13 @@ unsafe impl NSCopying for NSUUID {
6868
type Output = NSUUID;
6969
}
7070

71+
impl alloc::borrow::ToOwned for NSUUID {
72+
type Owned = Id<NSUUID, Shared>;
73+
fn to_owned(&self) -> Self::Owned {
74+
self.copy()
75+
}
76+
}
77+
7178
#[cfg(test)]
7279
mod tests {
7380
use super::*;

objc2-foundation/src/value.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ unsafe impl<T: 'static> NSCopying for NSValue<T> {
8383
type Output = NSValue<T>;
8484
}
8585

86+
impl<T: 'static> alloc::borrow::ToOwned for NSValue<T> {
87+
type Owned = Id<NSValue<T>, Shared>;
88+
fn to_owned(&self) -> Self::Owned {
89+
self.copy()
90+
}
91+
}
92+
8693
impl<T: 'static + Copy + Encode + Ord> Ord for NSValue<T> {
8794
fn cmp(&self, other: &Self) -> Ordering {
8895
self.get().cmp(&other.get())

0 commit comments

Comments
 (0)