Skip to content

Commit 045c53f

Browse files
authored
Merge pull request #520 from madsmtm/option-allocated
Change `Allocated<T>` in preparation for arbitrary self types
2 parents f0591af + 5480de5 commit 045c53f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+762
-1050
lines changed

crates/header-translator/src/method.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ impl fmt::Display for Method {
686686

687687
// Receiver
688688
if let MemoryManagement::IdInit = self.memory_management {
689-
write!(f, "this: Option<Allocated<Self>>, ")?;
689+
write!(f, "this: Allocated<Self>, ")?;
690690
} else if self.is_class {
691691
// Insert nothing; a class method is assumed
692692
} else if self.mutating {

crates/icrate/src/additions/Foundation/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,7 @@ impl IdFromIterator<u8> for NSMutableData {
267267
}
268268

269269
#[cfg(feature = "block")]
270-
unsafe fn with_vec<T: Message>(obj: Option<Allocated<T>>, bytes: Vec<u8>) -> Id<T> {
270+
unsafe fn with_vec<T: Message>(obj: Allocated<T>, bytes: Vec<u8>) -> Id<T> {
271271
use core::mem::ManuallyDrop;
272272

273273
use block2::{Block, ConcreteBlock};

crates/icrate/src/additions/Foundation/string.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ impl NSMutableString {
144144
}
145145
}
146146

147-
unsafe fn init_with_str<T: Message>(obj: Option<Allocated<T>>, string: &str) -> Id<T> {
147+
unsafe fn init_with_str<T: Message>(obj: Allocated<T>, string: &str) -> Id<T> {
148148
let bytes: *const c_void = string.as_ptr().cast();
149149
// We use `msg_send_id` instead of the generated method from `icrate`,
150150
// since that assumes the encoding is `usize`, whereas GNUStep assumes

crates/icrate/src/additions/Foundation/thread.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ impl MainThreadMarker {
157157
/// let obj: Id<SomeClass> = unsafe { msg_send_id![obj, init] };
158158
/// ```
159159
#[inline]
160-
pub fn alloc<T: ClassType>(self) -> Option<Allocated<T>> {
160+
pub fn alloc<T: ClassType>(self) -> Allocated<T> {
161161
// SAFETY: Same as `ClassType::alloc`, with the addition that since we
162162
// take `self: MainThreadMarker`, the `IsAllocableAnyThread` bound is
163163
// not required.

crates/icrate/src/additions/Metal/private.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ extern_methods!(
2727
#[cfg(feature = "Metal_MTLDevice")]
2828
#[method_id(initWithVertexData:fragmentData:serializedVertexDescriptor:device:options:flags:)]
2929
pub unsafe fn initWithVertexData(
30-
this: Option<Allocated<Self>>,
30+
this: Allocated<Self>,
3131
vertex_data: *mut c_void,
3232
fragment_data: *mut c_void,
3333
vertex_desc: *mut c_void,

crates/icrate/src/fixes/Foundation/NSUUID.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,7 @@ unsafe impl RefEncode for UuidBytes {
1919
extern_methods!(
2020
unsafe impl Foundation::NSUUID {
2121
#[method_id(initWithUUIDBytes:)]
22-
pub(crate) fn initWithUUIDBytes(
23-
this: Option<Allocated<Self>>,
24-
bytes: &UuidBytes,
25-
) -> Id<Self>;
22+
pub(crate) fn initWithUUIDBytes(this: Allocated<Self>, bytes: &UuidBytes) -> Id<Self>;
2623

2724
#[method(getUUIDBytes:)]
2825
pub(crate) fn getUUIDBytes(&self, bytes: &mut UuidBytes);

crates/icrate/src/generated

crates/icrate/tests/mutable_array.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![cfg(feature = "Foundation_NSMutableArray")]
2-
use objc2::rc::{__RcTestObject, __ThreadTestData, autoreleasepool};
2+
use objc2::rc::{__RcTestObject, __ThreadTestData, autoreleasepool, Allocated};
33
use objc2::{msg_send, ClassType};
44

55
#[cfg(feature = "Foundation_NSNumber")]
@@ -123,7 +123,8 @@ fn test_threaded() {
123123

124124
s.spawn(|| {
125125
let array = <NSMutableArray<NSObject>>::alloc();
126-
assert!(array.is_some());
126+
let ptr = Allocated::as_ptr(&array);
127+
assert!(!ptr.is_null());
127128
});
128129
});
129130
}

crates/objc2/CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
1515
- `CounterpartOrSelf`.
1616
* Added new `encode` traits `EncodeReturn`, `EncodeArgument` and
1717
`EncodeArguments`.
18+
* Added methods `as_ptr` and `as_mut_ptr` to `Allocated`.
1819

1920
### Changed
2021
* **BREAKING**: `AnyClass::verify_sel` now take more well-defined types
@@ -47,6 +48,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
4748
a method.
4849
* **BREAKING**: Renamed the associated types `Ret` and `Args` on
4950
`MethodImplementation` to `Return` and `Arguments`.
51+
* **BREAKING**: Make `rc::Allocated` allowed to be `NULL` internally, such
52+
that uses of `Option<Allocated<T>>` is now simply `Allocated<T>`.
5053

5154
### Deprecated
5255
* Soft deprecated using `msg_send!` without a comma between arguments (i.e.

crates/objc2/src/__macro_helpers/declare_class.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ where
4646
}
4747

4848
// Explicitly left unimplemented for now!
49-
// impl MessageRecieveId<impl MessageReceiver, Option<Allocated<T>>> for Alloc {}
49+
// impl MessageRecieveId<impl MessageReceiver, Allocated<T>> for Alloc {}
5050

5151
// Note: `MethodImplementation` allows for `Allocated` as the receiver, so we
5252
// restrict it here to only be when the selector is `init`.
@@ -56,7 +56,7 @@ where
5656
impl<Ret, T> MessageRecieveId<Allocated<T>, Ret> for Init
5757
where
5858
T: Message,
59-
Ret: MaybeOptionId<Input = Id<T>>,
59+
Ret: MaybeOptionId<Input = Option<Id<T>>>,
6060
{
6161
#[inline]
6262
fn into_return(obj: Ret) -> IdReturnValue {

0 commit comments

Comments
 (0)