Skip to content

Commit

Permalink
Remove AsMut and BorrowMut implementations
Browse files Browse the repository at this point in the history
Part of #563
  • Loading branch information
madsmtm committed Sep 6, 2024
1 parent c41fe84 commit 53ee5e3
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 63 deletions.
2 changes: 2 additions & 0 deletions crates/objc2/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
* **BREAKING**: Disallow `&mut` message receivers (except in the special case
when the object is `AnyObject`, for better backwards compatibility with
`objc`).
* **BREAKING**: Removed `AsMut` and `BorrowMut` implementations in
`extern_class!` and `declare_class!`.

### Fixed
* Remove an incorrect assertion when adding protocols to classes in an unexpected
Expand Down
4 changes: 2 additions & 2 deletions crates/objc2/src/__macro_helpers/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use core::borrow::{Borrow, BorrowMut};
pub use core::borrow::Borrow;
pub use core::cell::UnsafeCell;
pub use core::convert::{AsMut, AsRef};
pub use core::convert::AsRef;
pub use core::marker::{PhantomData, Sized};
pub use core::mem::{size_of, ManuallyDrop, MaybeUninit};
pub use core::ops::{Deref, DerefMut};
Expand Down
31 changes: 2 additions & 29 deletions crates/objc2/src/macros/extern_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
/// - [`Deref<Target = $superclass>`][core::ops::Deref]
/// - [`DerefMut`][core::ops::DerefMut]
/// - [`AsRef<$inheritance_chain>`][AsRef]
/// - [`AsMut<$inheritance_chain>`][AsMut]
/// - [`Borrow<$inheritance_chain>`][core::borrow::Borrow]
/// - [`BorrowMut<$inheritance_chain>`][core::borrow::BorrowMut]
///
/// The macro allows specifying zero-sized fields like [`PhantomData`] on the
/// struct.
Expand Down Expand Up @@ -262,15 +260,13 @@ macro_rules! __impl_as_ref_borrow {
{
impl ($($t:tt)*) for $for:ty {
fn as_ref($($self:tt)*) $ref:block
fn as_mut($($self_mut:tt)*) $mut:block
}

()
} => {};
{
impl ($($t:tt)*) for $for:ty {
fn as_ref($($self:tt)*) $ref:block
fn as_mut($($self_mut:tt)*) $mut:block
}

($item:ty, $($tail:ty,)*)
Expand All @@ -280,13 +276,8 @@ macro_rules! __impl_as_ref_borrow {
fn as_ref($($self)*) -> &$item $ref
}

impl<$($t)*> $crate::__macro_helpers::AsMut<$item> for $for {
#[inline]
fn as_mut($($self_mut)*) -> &mut $item $mut
}

// Borrow and BorrowMut are correct, since subclasses behaves
// identical to the class they inherit (message sending doesn't care).
// Borrow is correct, since subclasses behaves identical to the class
// they inherit (message sending doesn't care).
//
// In particular, `Eq`, `Ord` and `Hash` all give the same results
// after borrow.
Expand All @@ -296,15 +287,9 @@ macro_rules! __impl_as_ref_borrow {
fn borrow($($self)*) -> &$item $ref
}

impl<$($t)*> $crate::__macro_helpers::BorrowMut<$item> for $for {
#[inline]
fn borrow_mut($($self_mut)*) -> &mut $item $mut
}

$crate::__impl_as_ref_borrow! {
impl ($($t)*) for $for {
fn as_ref($($self)*) $ref
fn as_mut($($self_mut)*) $mut
}

($($tail,)*)
Expand Down Expand Up @@ -478,14 +463,6 @@ macro_rules! __extern_class_impl_traits {
}
}

$(#[$impl_m])*
impl<$($t)*> $crate::__macro_helpers::AsMut<Self> for $for {
#[inline]
fn as_mut(&mut self) -> &mut Self {
self
}
}

// Assume the meta attributes are all `cfg` attributes
$(#[$impl_m])*
$crate::__impl_as_ref_borrow! {
Expand All @@ -494,10 +471,6 @@ macro_rules! __extern_class_impl_traits {
// Triggers Deref coercion depending on return type
&*self
}
fn as_mut(&mut self) {
// Triggers Deref coercion depending on return type
&mut *self
}
}

($superclass, $($inheritance_rest,)*)
Expand Down
14 changes: 0 additions & 14 deletions crates/objc2/src/rc/id_forwarding_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,13 @@ impl<T: ?Sized> borrow::Borrow<T> for Retained<T> {
}
}

impl<T: ?Sized + IsMutable> borrow::BorrowMut<T> for Retained<T> {
fn borrow_mut(&mut self) -> &mut T {
// Auto-derefs
self
}
}

impl<T: ?Sized> AsRef<T> for Retained<T> {
fn as_ref(&self) -> &T {
// Auto-derefs
self
}
}

impl<T: ?Sized + IsMutable> AsMut<T> for Retained<T> {
fn as_mut(&mut self) -> &mut T {
// Auto-derefs
self
}
}

impl<T: Error + ?Sized> Error for Retained<T> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
(**self).source()
Expand Down
9 changes: 2 additions & 7 deletions crates/objc2/src/runtime/nsobject.rs
Original file line number Diff line number Diff line change
Expand Up @@ -506,20 +506,15 @@ mod tests {

#[test]
fn test_as_ref_borrow() {
use core::borrow::{Borrow, BorrowMut};
use core::borrow::Borrow;

fn impls_as_ref<T: AsRef<U> + Borrow<U> + ?Sized, U: ?Sized>(_: &T) {}
fn impls_as_mut<T: AsMut<U> + BorrowMut<U> + ?Sized, U: ?Sized>(_: &mut T) {}

let mut obj = NSObjectMutable::new();
let obj = NSObjectMutable::new();
impls_as_ref::<Retained<NSObjectMutable>, NSObjectMutable>(&obj);
impls_as_mut::<Retained<NSObjectMutable>, NSObjectMutable>(&mut obj);
impls_as_ref::<NSObjectMutable, NSObjectMutable>(&obj);
impls_as_mut::<NSObjectMutable, NSObjectMutable>(&mut obj);
impls_as_ref::<NSObject, NSObject>(&obj);
impls_as_mut::<NSObject, NSObject>(&mut obj);
impls_as_ref::<NSObject, AnyObject>(&obj);
impls_as_mut::<NSObject, AnyObject>(&mut obj);

let obj = NSObject::new();
impls_as_ref::<Retained<NSObject>, NSObject>(&obj);
Expand Down
12 changes: 1 addition & 11 deletions crates/objc2/src/runtime/protocol_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,17 +203,7 @@ where
}
}

impl<P: ?Sized, T> AsMut<ProtocolObject<T>> for ProtocolObject<P>
where
T: ?Sized + ImplementedBy<ProtocolObject<P>>,
{
#[inline]
fn as_mut(&mut self) -> &mut ProtocolObject<T> {
ProtocolObject::from_mut(self)
}
}

// TODO: Maybe implement Borrow + BorrowMut?
// TODO: Maybe implement Borrow?

#[cfg(test)]
#[allow(clippy::missing_safety_doc)]
Expand Down

0 comments on commit 53ee5e3

Please sign in to comment.