Skip to content

Commit 8d3517f

Browse files
committed
Fix unstable_autoreleasesafe in objc2-foundation
1 parent d16d42b commit 8d3517f

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

objc2-foundation/src/macros.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,17 @@ macro_rules! object {
8080
// TODO: Consider T: Debug bound
8181
impl<$($t $(: $b)?),*> ::core::fmt::Debug for $name<$($t),*> {
8282
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
83-
use $crate::{INSObject, INSString};
84-
::objc2::rc::autoreleasepool(|pool| {
85-
::core::fmt::Debug::fmt(self.description().as_str(pool), f)
86-
})
83+
use ::objc2::MessageReceiver;
84+
use ::alloc::borrow::ToOwned;
85+
use $crate::{INSObject, INSString, NSObject};
86+
// "downgrading" to NSObject and calling `to_owned` to work
87+
// around `f` and Self not being AutoreleaseSafe.
88+
// TODO: Fix this!
89+
let this: &NSObject = unsafe { &*self.as_raw_receiver().cast() };
90+
let s = ::objc2::rc::autoreleasepool(|pool| {
91+
this.description().as_str(pool).to_owned()
92+
});
93+
::core::fmt::Debug::fmt(&s, f)
8794
}
8895
}
8996
};

objc2-foundation/src/string.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use core::slice;
55
use core::str;
66
use std::os::raw::c_char;
77

8+
use alloc::borrow::ToOwned;
89
use objc2::ffi;
910
use objc2::msg_send;
1011
use objc2::rc::{autoreleasepool, AutoreleasePool};
@@ -119,7 +120,11 @@ unsafe impl INSCopying for NSString {
119120

120121
impl fmt::Display for NSString {
121122
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
122-
autoreleasepool(|pool| fmt::Display::fmt(self.as_str(pool), f))
123+
// The call to `to_owned` is unfortunate, but is required to work
124+
// around `f` not being AutoreleaseSafe.
125+
// TODO: Fix this!
126+
let s = autoreleasepool(|pool| self.as_str(pool).to_owned());
127+
fmt::Display::fmt(&s, f)
123128
}
124129
}
125130

0 commit comments

Comments
 (0)