Skip to content

Commit

Permalink
Add Id::cast
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Dec 21, 2021
1 parent 3d0e46b commit b276169
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 1 addition & 2 deletions objc2-foundation/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ object!(unsafe pub struct NSObject);

impl NSObject {
pub fn new() -> Id<Self, Owned> {
unsafe { ffi::NSObject::new().unwrap() };
todo!("unsafe Id::cast")
unsafe { ffi::NSObject::new().unwrap().cast().into_owned() }
}
}

Expand Down
10 changes: 5 additions & 5 deletions objc2-foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,17 @@ pub unsafe trait INSString: INSObject {

fn from_str(string: &str) -> Id<Self, Shared> {
let bytes = string.as_ptr() as *const c_void;
unsafe {
let obj = ffi::NSString::alloc().unwrap();
let obj = unsafe { ffi::NSString::alloc().unwrap() };
let obj = unsafe {
ffi::NSString::initWithBytes_length_encoding_(
obj,
bytes,
string.len(),
ffi::NSUTF8StringEncoding,
)
.unwrap();
todo!("unsafe Id::cast")
}
.unwrap()
};
unsafe { obj.cast().into_shared() }
}
}

Expand Down
7 changes: 7 additions & 0 deletions objc2/src/rc/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,13 @@ impl<T: Message> Id<T, Unknown> {
pool.__verify_is_inner();
self.autorelease_inner()
}

#[inline]
// TODO: Can we make this safe?
pub unsafe fn cast<U: Message>(self) -> Id<U, Unknown> {
let ptr = ManuallyDrop::new(self).ptr.cast();
unsafe { Id::new(ptr) }
}
}

impl<T: Message> From<Id<T, Owned>> for Id<T, Shared> {
Expand Down

0 comments on commit b276169

Please sign in to comment.