Skip to content

Commit

Permalink
Rename methods on Retained traits from id to retained
Browse files Browse the repository at this point in the history
Part of #617
  • Loading branch information
madsmtm committed Sep 17, 2024
1 parent f8b11ac commit adf2c52
Show file tree
Hide file tree
Showing 12 changed files with 26 additions and 55 deletions.
2 changes: 1 addition & 1 deletion crates/header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1815,7 +1815,7 @@ impl Stmt {
GenericTyHelper(cls_generics),
)?;
writeln!(f, " #[inline]")?;
writeln!(f, " fn default_id() -> Retained<Self> {{")?;
writeln!(f, " fn default_retained() -> Retained<Self> {{")?;
writeln!(f, " Self::{}()", method.fn_name)?;
writeln!(f, " }}")?;
writeln!(f, "}}")?;
Expand Down
2 changes: 2 additions & 0 deletions crates/objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: Mark `Retained::autorelease` as `unsafe`, since we cannot
ensure that the given pool is actually the innermost pool.
* **BREAKING**: Removed the deprecated `malloc` feature and `malloc_buf` dependency.
* **BREAKING**: Removed aliases `DefaultId`, `IdFromIterator` and
`IdIntoIterator`, as well as their methods. Use the renamed traits instead.

### Fixed
* Remove an incorrect assertion when adding protocols to classes in an unexpected
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/__framework_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub use core::ptr::NonNull;

pub use crate::encode::{Encode, Encoding, RefEncode};
pub use crate::ffi::{NSInteger, NSIntegerMax, NSUInteger, NSUIntegerMax};
pub use crate::rc::{Allocated, DefaultId, DefaultRetained, Id, Retained};
pub use crate::rc::{Allocated, DefaultRetained, Retained};
pub use crate::runtime::{
AnyClass, AnyObject, Bool, NSObject, NSObjectProtocol, ProtocolObject, Sel,
};
Expand Down
6 changes: 0 additions & 6 deletions crates/objc2/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,3 @@ pub use self::retained_traits::{DefaultRetained, RetainedFromIterator, RetainedI
#[cfg(test)]
pub(crate) use self::test_object::{RcTestObject, ThreadTestData};
pub use self::weak::{Weak, WeakId};

// Soft-deprecated aliases
pub use self::retained_traits::{
DefaultRetained as DefaultId, RetainedFromIterator as IdFromIterator,
RetainedIntoIterator as IdIntoIterator,
};
41 changes: 8 additions & 33 deletions crates/objc2/src/rc/retained_traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,11 @@ use super::Retained;
/// Helper trait to implement [`Default`] on [`Retained`].
#[doc(alias = "DefaultId")]
pub trait DefaultRetained {
/// The default [`Retained`] for a type.
///
/// Soft-deprecated alias of [`DefaultRetained::default_retained`].
fn default_id() -> Retained<Self>;

/// The default [`Retained`] for a type.
///
/// On most objects the implementation would be sending a message to the
/// `new` selector.
#[inline]
fn default_retained() -> Retained<Self> {
Self::default_id()
}
fn default_retained() -> Retained<Self>;
}

impl<T: ?Sized + DefaultRetained> Default for Retained<T> {
Expand Down Expand Up @@ -45,19 +37,11 @@ pub trait RetainedIntoIterator {
/// Which kind of iterator are we turning this into?
type IntoIter: Iterator<Item = Self::Item>;

/// Creates an iterator from an [`Retained`].
///
/// Soft-deprecated alias of [`RetainedIntoIterator::retained_into_iter`].
fn id_into_iter(this: Retained<Self>) -> Self::IntoIter;

/// Creates an iterator from an [`Retained`].
///
/// You would normally not call this function directly; instead, you'd
/// call [`into_iter`](IntoIterator::into_iter) on an [`Retained`].
#[inline]
fn retained_into_iter(this: Retained<Self>) -> Self::IntoIter {
Self::id_into_iter(this)
}
fn retained_into_iter(this: Retained<Self>) -> Self::IntoIter;
}

// Note: These `IntoIterator` implementations conflict with an `Iterator`
Expand Down Expand Up @@ -118,20 +102,9 @@ where
#[doc(alias = "IdFromIterator")]
pub trait RetainedFromIterator<T>: Sized {
/// Creates an `Retained` from an iterator.
///
/// Soft-deprecated alias of [`RetainedFromIterator::retained_from_iter`].
fn id_from_iter<I>(iter: I) -> Retained<Self>
where
I: IntoIterator<Item = T>;

/// Creates an `Retained` from an iterator.
#[inline]
fn retained_from_iter<I>(iter: I) -> Retained<Self>
where
I: IntoIterator<Item = T>,
{
Self::id_from_iter(iter)
}
I: IntoIterator<Item = T>;
}

impl<T, U: RetainedFromIterator<T>> FromIterator<T> for Retained<U> {
Expand Down Expand Up @@ -160,7 +133,7 @@ mod tests {
);

impl DefaultRetained for Collection {
fn default_id() -> Retained<Self> {
fn default_retained() -> Retained<Self> {
unsafe { msg_send_id![Collection::class(), new] }
}
}
Expand Down Expand Up @@ -200,13 +173,15 @@ mod tests {
type Item = Retained<NSObject>;
type IntoIter = IntoIter;

fn id_into_iter(this: Retained<Self>) -> Self::IntoIter {
fn retained_into_iter(this: Retained<Self>) -> Self::IntoIter {
IntoIter { _inner: this }
}
}

impl RetainedFromIterator<Retained<NSObject>> for Collection {
fn id_from_iter<I: IntoIterator<Item = Retained<NSObject>>>(_iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = Retained<NSObject>>>(
_iter: I,
) -> Retained<Self> {
Collection::default_retained()
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/rc/test_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ impl Drop for RcTestObject {
}

impl DefaultRetained for RcTestObject {
fn default_id() -> Retained<Self> {
fn default_retained() -> Retained<Self> {
Self::new()
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ impl fmt::Debug for NSObject {

impl DefaultRetained for NSObject {
#[inline]
fn default_id() -> Retained<Self> {
fn default_retained() -> Retained<Self> {
Self::new()
}
}
Expand Down
8 changes: 4 additions & 4 deletions framework-crates/objc2-foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,29 +429,29 @@ impl<'a, T: Message> Extend<&'a T> for &NSMutableArray<T> {
}

impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSArray<T> {
fn id_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_slice(&vec)
}
}

impl<T: Message> RetainedFromIterator<Retained<T>> for NSArray<T> {
fn id_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_retained_slice(&vec)
}
}

impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSMutableArray<T> {
fn id_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
// TODO: Is this, or is using `initWithCapacity` the most optimal?
let vec = Vec::from_iter(iter);
Self::from_slice(&vec)
}
}

impl<T: Message> RetainedFromIterator<Retained<T>> for NSMutableArray<T> {
fn id_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
// TODO: Is this, or is using `initWithCapacity` the most optimal?
let vec = Vec::from_iter(iter);
Self::from_retained_slice(&vec)
Expand Down
4 changes: 2 additions & 2 deletions framework-crates/objc2-foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,15 +314,15 @@ impl std::io::Write for &NSMutableData {

#[cfg(feature = "block2")]
impl RetainedFromIterator<u8> for NSData {
fn id_from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_vec(vec)
}
}

#[cfg(feature = "block2")]
impl RetainedFromIterator<u8> for NSMutableData {
fn id_from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = u8>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_vec(vec)
}
Expand Down
2 changes: 1 addition & 1 deletion framework-crates/objc2-foundation/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ macro_rules! __impl_into_iter {
type IntoIter = $into_iter<T>;

#[inline]
fn id_into_iter(this: Retained<Self>) -> Self::IntoIter {
fn retained_into_iter(this: Retained<Self>) -> Self::IntoIter {
$into_iter($crate::iter::IntoIter::$new_fn(this))
}
}
Expand Down
8 changes: 4 additions & 4 deletions framework-crates/objc2-foundation/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,29 +311,29 @@ impl<'a, T: Message> Extend<&'a T> for &NSMutableSet<T> {
}

impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSSet<T> {
fn id_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_slice(&vec)
}
}

impl<T: Message> RetainedFromIterator<Retained<T>> for NSSet<T> {
fn id_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
let vec = Vec::from_iter(iter);
Self::from_retained_slice(&vec)
}
}

impl<'a, T: Message + 'a> RetainedFromIterator<&'a T> for NSMutableSet<T> {
fn id_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = &'a T>>(iter: I) -> Retained<Self> {
// TODO: Is this, or is using `initWithCapacity` the most optimal?
let vec = Vec::from_iter(iter);
Self::from_slice(&vec)
}
}

impl<T: Message> RetainedFromIterator<Retained<T>> for NSMutableSet<T> {
fn id_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
fn retained_from_iter<I: IntoIterator<Item = Retained<T>>>(iter: I) -> Retained<Self> {
// TODO: Is this, or is using `initWithCapacity` the most optimal?
let vec = Vec::from_iter(iter);
Self::from_retained_slice(&vec)
Expand Down

0 comments on commit adf2c52

Please sign in to comment.