Skip to content

Commit

Permalink
Manually format usage of msg_send!
Browse files Browse the repository at this point in the history
Rustfmt can't do this for us
  • Loading branch information
madsmtm committed Oct 1, 2021
1 parent 43b2699 commit 1860013
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 44 deletions.
2 changes: 1 addition & 1 deletion objc2/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let cls = class!(NSObject);
unsafe {
let obj: *mut Object = msg_send![cls, new];
let hash: usize = msg_send![obj, hash];
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
let is_kind: BOOL = msg_send![obj, isKindOfClass: cls];
// Even void methods must have their return type annotated
let _: () = msg_send![obj, release];
}
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ mod tests {
// Registering the custom class is in test_utils
let obj = test_utils::custom_object();
unsafe {
let _: () = msg_send![obj, setFoo:13u32];
let _: () = msg_send![obj, setFoo: 13u32];
let result: u32 = msg_send![obj, foo];
assert!(result == 13);
}
Expand Down
2 changes: 1 addition & 1 deletion objc2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Objective-C objects can be messaged using the [`msg_send!`](macro.msg_send!.html
let cls = class!(NSObject);
let obj: *mut Object = msg_send![cls, new];
let hash: usize = msg_send![obj, hash];
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
let is_kind: BOOL = msg_send![obj, isKindOfClass: cls];
// Even void methods must have their return type annotated
let _: () = msg_send![obj, release];
# }
Expand Down
4 changes: 2 additions & 2 deletions objc2/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ mod tests {
fn test_send_message() {
let obj = test_utils::custom_object();
let result: u32 = unsafe {
let _: () = msg_send![obj, setFoo:4u32];
let _: () = msg_send![obj, setFoo: 4u32];
msg_send![obj, foo]
};
assert!(result == 4);
Expand Down Expand Up @@ -421,7 +421,7 @@ mod tests {
let obj = test_utils::custom_subclass_object();
let superclass = test_utils::custom_class();
unsafe {
let _: () = msg_send![obj, setFoo:4u32];
let _: () = msg_send![obj, setFoo: 4u32];
let foo: u32 = msg_send![super(obj, superclass), foo];
assert!(foo == 4);

Expand Down
2 changes: 1 addition & 1 deletion objc2/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ mod tests {
#[test]
fn test_protocol_method() {
let class = test_utils::custom_class();
let result: i32 = unsafe { msg_send![class, addNumber:1 toNumber:2] };
let result: i32 = unsafe { msg_send![class, addNumber: 1, toNumber: 2] };
assert_eq!(result, 3);
}

Expand Down
2 changes: 1 addition & 1 deletion objc2_foundation/examples/custom_class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fn main() {
});

unsafe {
let _: () = msg_send![obj, setNumber:12u32];
let _: () = msg_send![obj, setNumber: 12u32];
}
println!("Number: {}", obj.number());
}
22 changes: 14 additions & 8 deletions objc2_foundation/src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,11 @@ where
{
let cls = A::class();
let obj: *mut A = msg_send![cls, alloc];
let obj: *mut A = msg_send![obj, initWithObjects:refs.as_ptr()
count:refs.len()];
let obj: *mut A = msg_send![
obj,
initWithObjects: refs.as_ptr(),
count: refs.len(),
];
Id::new(NonNull::new_unchecked(obj))
}

Expand Down Expand Up @@ -135,7 +138,7 @@ pub trait INSArray: INSObject {
let range = NSRange::from_range(range);
let mut vec = Vec::with_capacity(range.length);
unsafe {
let _: () = msg_send![self, getObjects:vec.as_ptr() range:range];
let _: () = msg_send![self, getObjects: vec.as_ptr(), range: range];
vec.set_len(range.length);
}
vec
Expand Down Expand Up @@ -254,13 +257,13 @@ pub type NSSharedArray<T> = NSArray<T, Shared>;
pub trait INSMutableArray: INSArray {
fn add_object(&mut self, obj: Id<Self::Item, Self::Own>) {
unsafe {
let _: () = msg_send![self, addObject:&*obj];
let _: () = msg_send![self, addObject: &*obj];
}
}

fn insert_object_at(&mut self, index: usize, obj: Id<Self::Item, Self::Own>) {
unsafe {
let _: () = msg_send![self, insertObject:&*obj atIndex:index];
let _: () = msg_send![self, insertObject: &*obj, atIndex: index];
}
}

Expand All @@ -274,8 +277,11 @@ pub trait INSMutableArray: INSArray {
Id::retain(obj.into())
};
unsafe {
let _: () = msg_send![self, replaceObjectAtIndex:index
withObject:&*obj];
let _: () = msg_send![
self,
replaceObjectAtIndex: index,
withObject: &*obj,
];
}
old_obj
}
Expand Down Expand Up @@ -333,7 +339,7 @@ pub trait INSMutableArray: INSArray {
let context = &mut closure as *mut F as *mut c_void;

unsafe {
let _: () = msg_send![self, sortUsingFunction:f context:context];
let _: () = msg_send![self, sortUsingFunction: f, context: context];
}
// Keep the closure alive until the function has run.
drop(closure);
Expand Down
32 changes: 22 additions & 10 deletions objc2_foundation/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ pub trait INSData: INSObject {
let bytes_ptr = bytes.as_ptr() as *const c_void;
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytes:bytes_ptr
length:bytes.len()];
let obj: *mut Self = msg_send![
obj,
initWithBytes: bytes_ptr,
length: bytes.len(),
];
Id::new(NonNull::new_unchecked(obj))
}
}
Expand All @@ -56,9 +59,12 @@ pub trait INSData: INSObject {
let cls = Self::class();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytesNoCopy:bytes_ptr
length:bytes.len()
deallocator:dealloc];
let obj: *mut Self = msg_send![
obj,
initWithBytesNoCopy: bytes_ptr,
length: bytes.len(),
deallocator: dealloc,
];
core::mem::forget(bytes);
Id::new(NonNull::new_unchecked(obj))
}
Expand Down Expand Up @@ -98,18 +104,24 @@ pub trait INSMutableData: INSData {
fn append(&mut self, bytes: &[u8]) {
let bytes_ptr = bytes.as_ptr() as *const c_void;
unsafe {
let _: () = msg_send![self, appendBytes:bytes_ptr
length:bytes.len()];
let _: () = msg_send![
self,
appendBytes: bytes_ptr,
length:bytes.len(),
];
}
}

fn replace_range(&mut self, range: Range<usize>, bytes: &[u8]) {
let range = NSRange::from_range(range);
let bytes_ptr = bytes.as_ptr() as *const c_void;
unsafe {
let _: () = msg_send![self, replaceBytesInRange:range
withBytes:bytes_ptr
length:bytes.len()];
let _: () = msg_send![
self,
replaceBytesInRange:range,
withBytes:bytes_ptr,
length:bytes.len(),
];
}
}

Expand Down
37 changes: 26 additions & 11 deletions objc2_foundation/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ where
let cls = D::class();
let count = min(keys.len(), vals.len());
let obj: *mut D = msg_send![cls, alloc];
let obj: *mut D = msg_send![obj, initWithObjects:vals.as_ptr()
forKeys:keys.as_ptr()
count:count];
let obj: *mut D = msg_send![
obj,
initWithObjects: vals.as_ptr(),
forKeys: keys.as_ptr(),
count: count,
];
Id::new(NonNull::new_unchecked(obj))
}

Expand Down Expand Up @@ -48,8 +51,11 @@ pub trait INSDictionary: INSObject {
let len = self.count();
let mut keys = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:ptr::null_mut::<&Self::Value>()
andKeys:keys.as_mut_ptr()];
let _: () = msg_send![
self,
getObjects: ptr::null_mut::<&Self::Value>(),
andKeys: keys.as_mut_ptr(),
];
keys.set_len(len);
}
keys
Expand All @@ -59,8 +65,11 @@ pub trait INSDictionary: INSObject {
let len = self.count();
let mut vals = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:vals.as_mut_ptr()
andKeys:ptr::null_mut::<&Self::Key>()];
let _: () = msg_send![
self,
getObjects: vals.as_mut_ptr(),
andKeys: ptr::null_mut::<&Self::Key>(),
];
vals.set_len(len);
}
vals
Expand All @@ -71,8 +80,11 @@ pub trait INSDictionary: INSObject {
let mut keys = Vec::with_capacity(len);
let mut objs = Vec::with_capacity(len);
unsafe {
let _: () = msg_send![self, getObjects:objs.as_mut_ptr()
andKeys:keys.as_mut_ptr()];
let _: () = msg_send![
self,
getObjects: objs.as_mut_ptr(),
andKeys: keys.as_mut_ptr(),
];
keys.set_len(len);
objs.set_len(len);
}
Expand Down Expand Up @@ -100,7 +112,10 @@ pub trait INSDictionary: INSObject {
}
}

fn from_keys_and_objects<T>(keys: &[&T], vals: Vec<Id<Self::Value, Self::Own>>) -> Id<Self, Owned>
fn from_keys_and_objects<T>(
keys: &[&T],
vals: Vec<Id<Self::Value, Self::Own>>,
) -> Id<Self, Owned>
where
T: INSCopying<Output = Self::Key>,
{
Expand Down Expand Up @@ -166,7 +181,7 @@ where
#[cfg(test)]
mod tests {
use alloc::vec;
use objc2::rc::{Owned, Id};
use objc2::rc::{Id, Owned};

use super::{INSDictionary, NSDictionary};
use crate::{INSArray, INSObject, INSString, NSObject, NSString};
Expand Down
9 changes: 6 additions & 3 deletions objc2_foundation/src/enumerator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,12 @@ fn enumerate<'a, 'b: 'a, C: INSFastEnumeration>(
let count: usize = unsafe {
// Reborrow state so that we don't move it
let state = &mut *state;
msg_send![object, countByEnumeratingWithState:state
objects:buf.as_mut_ptr()
count:buf.len()]
msg_send![
object,
countByEnumeratingWithState: state,
objects: buf.as_mut_ptr(),
count: buf.len(),
]
};

if count > 0 {
Expand Down
9 changes: 6 additions & 3 deletions objc2_foundation/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,12 @@ pub trait INSString: INSObject {
let bytes = string.as_ptr() as *const c_void;
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytes:bytes
length:string.len()
encoding:UTF8_ENCODING];
let obj: *mut Self = msg_send![
obj,
initWithBytes: bytes,
length: string.len(),
encoding: UTF8_ENCODING,
];
Id::new(NonNull::new_unchecked(obj))
}
}
Expand Down
7 changes: 5 additions & 2 deletions objc2_foundation/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@ pub trait INSValue: INSObject {
let encoding = CString::new(Self::Value::ENCODING.to_string()).unwrap();
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![obj, initWithBytes:bytes
objCType:encoding.as_ptr()];
let obj: *mut Self = msg_send![
obj,
initWithBytes: bytes,
objCType: encoding.as_ptr(),
];
Id::new(NonNull::new_unchecked(obj))
}
}
Expand Down

0 comments on commit 1860013

Please sign in to comment.