Skip to content

Commit 2a67179

Browse files
authored
Merge pull request #2 from madsmtm/rustfmt
Run `cargo fmt`
2 parents a021c4c + d2afd4d commit 2a67179

Some content is hidden

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

45 files changed

+868
-571
lines changed

objc/.travis.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
language: rust
22
rust:
3-
- stable
3+
- stable
44
- nightly
55
os:
66
- osx

objc/CHANGELOG.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
* C types are now used from `std::os::raw` rather than `libc`. This means
8585
`Encode` may not be implemented for `libc` types; switch them to the
8686
`std::os::raw` equivalents instead. This avoids an issue that would arise
87-
from simultaneously using different versions of the libc crate.
87+
from simultaneously using different versions of the libc crate.
8888

8989
* Dynamic messaging was moved into the `Message` trait; instead of
9090
`().send(obj, sel!(description))`, use
@@ -110,7 +110,7 @@
110110
### Fixed
111111

112112
* Corrected alignment of ivars in `ClassDecl`; declared classes may now have a
113-
smaller size.
113+
smaller size.
114114

115115
* With the `"exception"` or `"verify_message"` feature enabled, panics from
116116
`msg_send!` will now be triggered from the line and file where the macro is

objc/Cargo.toml

+7-7
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ documentation = "http://ssheldon.github.io/rust-objc/objc/"
1212
license = "MIT"
1313

1414
exclude = [
15-
".gitignore",
16-
".travis.yml",
17-
"doc.sh",
18-
"travis_install.sh",
19-
"travis_test.sh",
20-
"tests-ios/**",
21-
]
15+
".gitignore",
16+
".travis.yml",
17+
"doc.sh",
18+
"travis_install.sh",
19+
"travis_test.sh",
20+
"tests-ios/**",
21+
]
2222

2323
[features]
2424
exception = ["objc_exception"]

objc/examples/example.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#[macro_use]
22
extern crate objc;
33

4-
use objc::Encode;
54
use objc::rc::StrongPtr;
65
use objc::runtime::{Class, Object};
6+
use objc::Encode;
77

88
fn main() {
99
// Get a class
@@ -25,9 +25,7 @@ fn main() {
2525
println!("NSObject address: {:p}", obj);
2626

2727
// Access an ivar of the object
28-
let isa: *const Class = unsafe {
29-
*(**obj).get_ivar("isa")
30-
};
28+
let isa: *const Class = unsafe { *(**obj).get_ivar("isa") };
3129
println!("NSObject isa: {:?}", isa);
3230

3331
// Inspect a method of the class
@@ -38,8 +36,6 @@ fn main() {
3836
assert!(*hash_return == usize::ENCODING);
3937

4038
// Invoke a method on the object
41-
let hash: usize = unsafe {
42-
msg_send![*obj, hash]
43-
};
39+
let hash: usize = unsafe { msg_send![*obj, hash] };
4440
println!("NSObject hash: {}", hash);
4541
}

objc/src/cache.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@ use std::os::raw::c_void;
22
use std::ptr;
33
use std::sync::atomic::{AtomicPtr, Ordering};
44

5-
use crate::runtime::{Class, Sel, self};
5+
use crate::runtime::{self, Class, Sel};
66

77
/// Allows storing a `Sel` in a static and lazily loading it.
88
#[doc(hidden)]
99
pub struct CachedSel {
10-
ptr: AtomicPtr<c_void>
10+
ptr: AtomicPtr<c_void>,
1111
}
1212

1313
impl CachedSel {
1414
/// Constructs a new `CachedSel`.
1515
pub const fn new() -> CachedSel {
1616
CachedSel {
17-
ptr: AtomicPtr::new(ptr::null_mut())
17+
ptr: AtomicPtr::new(ptr::null_mut()),
1818
}
1919
}
2020

@@ -38,14 +38,14 @@ impl CachedSel {
3838
/// Allows storing a `Class` reference in a static and lazily loading it.
3939
#[doc(hidden)]
4040
pub struct CachedClass {
41-
ptr: AtomicPtr<Class>
41+
ptr: AtomicPtr<Class>,
4242
}
4343

4444
impl CachedClass {
4545
/// Constructs a new `CachedClass`.
4646
pub const fn new() -> CachedClass {
4747
CachedClass {
48-
ptr: AtomicPtr::new(ptr::null_mut())
48+
ptr: AtomicPtr::new(ptr::null_mut()),
4949
}
5050
}
5151

objc/src/declare.rs

+55-38
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use std::ffi::CString;
3838
use std::mem;
3939
use std::ptr;
4040

41-
use crate::runtime::{BOOL, Class, Imp, NO, Object, Protocol, Sel, self};
41+
use crate::runtime::{self, Class, Imp, Object, Protocol, Sel, BOOL, NO};
4242
use crate::{Encode, EncodeArguments, Encoding, Message};
4343

4444
/// Types that can be used as the implementation of an Objective-C method.
@@ -93,8 +93,7 @@ fn count_args(sel: Sel) -> usize {
9393

9494
fn method_type_encoding(ret: &Encoding, args: &[Encoding]) -> CString {
9595
// First two arguments are always self and the selector
96-
let mut types = format!("{}{}{}",
97-
ret, <*mut Object>::ENCODING, Sel::ENCODING);
96+
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING);
9897
for enc in args {
9998
use std::fmt::Write;
10099
write!(&mut types, "{}", enc).unwrap();
@@ -117,13 +116,10 @@ pub struct ClassDecl {
117116
}
118117

119118
impl ClassDecl {
120-
fn with_superclass(name: &str, superclass: Option<&Class>)
121-
-> Option<ClassDecl> {
119+
fn with_superclass(name: &str, superclass: Option<&Class>) -> Option<ClassDecl> {
122120
let name = CString::new(name).unwrap();
123121
let super_ptr = superclass.map_or(ptr::null(), |c| c);
124-
let cls = unsafe {
125-
runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0)
126-
};
122+
let cls = unsafe { runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0) };
127123
if cls.is_null() {
128124
None
129125
} else {
@@ -150,8 +146,7 @@ impl ClassDecl {
150146
Functionality it expects, like implementations of `-retain` and `-release`
151147
used by ARC, will not be present otherwise.
152148
*/
153-
pub fn root(name: &str, intitialize_fn: extern fn(&Class, Sel))
154-
-> Option<ClassDecl> {
149+
pub fn root(name: &str, intitialize_fn: extern "C" fn(&Class, Sel)) -> Option<ClassDecl> {
155150
let mut decl = ClassDecl::with_superclass(name, None);
156151
if let Some(ref mut decl) = decl {
157152
unsafe {
@@ -167,17 +162,20 @@ impl ClassDecl {
167162
/// Unsafe because the caller must ensure that the types match those that
168163
/// are expected when the method is invoked from Objective-C.
169164
pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
170-
where F: MethodImplementation<Callee=Object> {
165+
where
166+
F: MethodImplementation<Callee = Object>,
167+
{
171168
let encs = F::Args::ENCODINGS;
172169
let sel_args = count_args(sel);
173-
assert!(sel_args == encs.len(),
170+
assert!(
171+
sel_args == encs.len(),
174172
"Selector accepts {} arguments, but function accepts {}",
175-
sel_args, encs.len(),
173+
sel_args,
174+
encs.len(),
176175
);
177176

178177
let types = method_type_encoding(&F::Ret::ENCODING, encs);
179-
let success = runtime::class_addMethod(self.cls, sel, func.imp(),
180-
types.as_ptr());
178+
let success = runtime::class_addMethod(self.cls, sel, func.imp(), types.as_ptr());
181179
assert!(success != NO, "Failed to add method {:?}", sel);
182180
}
183181

@@ -187,31 +185,36 @@ impl ClassDecl {
187185
/// Unsafe because the caller must ensure that the types match those that
188186
/// are expected when the method is invoked from Objective-C.
189187
pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
190-
where F: MethodImplementation<Callee=Class> {
188+
where
189+
F: MethodImplementation<Callee = Class>,
190+
{
191191
let encs = F::Args::ENCODINGS;
192192
let sel_args = count_args(sel);
193-
assert!(sel_args == encs.len(),
193+
assert!(
194+
sel_args == encs.len(),
194195
"Selector accepts {} arguments, but function accepts {}",
195-
sel_args, encs.len(),
196+
sel_args,
197+
encs.len(),
196198
);
197199

198200
let types = method_type_encoding(&F::Ret::ENCODING, encs);
199201
let metaclass = (*self.cls).metaclass() as *const _ as *mut _;
200-
let success = runtime::class_addMethod(metaclass, sel, func.imp(),
201-
types.as_ptr());
202+
let success = runtime::class_addMethod(metaclass, sel, func.imp(), types.as_ptr());
202203
assert!(success != NO, "Failed to add class method {:?}", sel);
203204
}
204205

205206
/// Adds an ivar with type `T` and the provided name to self.
206207
/// Panics if the ivar wasn't successfully added.
207-
pub fn add_ivar<T>(&mut self, name: &str) where T: Encode {
208+
pub fn add_ivar<T>(&mut self, name: &str)
209+
where
210+
T: Encode,
211+
{
208212
let c_name = CString::new(name).unwrap();
209213
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
210214
let size = mem::size_of::<T>();
211215
let align = log2_align_of::<T>();
212216
let success = unsafe {
213-
runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align,
214-
encoding.as_ptr())
217+
runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align, encoding.as_ptr())
215218
};
216219
assert!(success != NO, "Failed to add ivar {}", name);
217220
}
@@ -247,52 +250,66 @@ impl Drop for ClassDecl {
247250
/// A type for declaring a new protocol and adding new methods to it
248251
/// before registering it.
249252
pub struct ProtocolDecl {
250-
proto: *mut Protocol
253+
proto: *mut Protocol,
251254
}
252255

253256
impl ProtocolDecl {
254257
/// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
255258
/// protocol couldn't be allocated.
256259
pub fn new(name: &str) -> Option<ProtocolDecl> {
257260
let c_name = CString::new(name).unwrap();
258-
let proto = unsafe {
259-
runtime::objc_allocateProtocol(c_name.as_ptr())
260-
};
261+
let proto = unsafe { runtime::objc_allocateProtocol(c_name.as_ptr()) };
261262
if proto.is_null() {
262263
None
263264
} else {
264265
Some(ProtocolDecl { proto: proto })
265266
}
266267
}
267268

268-
fn add_method_description_common<Args, Ret>(&mut self, sel: Sel, is_required: bool,
269-
is_instance_method: bool)
270-
where Args: EncodeArguments,
271-
Ret: Encode {
269+
fn add_method_description_common<Args, Ret>(
270+
&mut self,
271+
sel: Sel,
272+
is_required: bool,
273+
is_instance_method: bool,
274+
) where
275+
Args: EncodeArguments,
276+
Ret: Encode,
277+
{
272278
let encs = Args::ENCODINGS;
273279
let sel_args = count_args(sel);
274-
assert!(sel_args == encs.len(),
280+
assert!(
281+
sel_args == encs.len(),
275282
"Selector accepts {} arguments, but function accepts {}",
276-
sel_args, encs.len(),
283+
sel_args,
284+
encs.len(),
277285
);
278286
let types = method_type_encoding(&Ret::ENCODING, encs);
279287
unsafe {
280288
runtime::protocol_addMethodDescription(
281-
self.proto, sel, types.as_ptr(), is_required as BOOL, is_instance_method as BOOL);
289+
self.proto,
290+
sel,
291+
types.as_ptr(),
292+
is_required as BOOL,
293+
is_instance_method as BOOL,
294+
);
282295
}
283296
}
284297

285298
/// Adds an instance method declaration with a given description to self.
286299
pub fn add_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
287-
where Args: EncodeArguments,
288-
Ret: Encode {
300+
where
301+
Args: EncodeArguments,
302+
Ret: Encode,
303+
{
289304
self.add_method_description_common::<Args, Ret>(sel, is_required, true)
290305
}
291306

292307
/// Adds a class method declaration with a given description to self.
293308
pub fn add_class_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
294-
where Args: EncodeArguments,
295-
Ret: Encode {
309+
where
310+
Args: EncodeArguments,
311+
Ret: Encode,
312+
{
296313
self.add_method_description_common::<Args, Ret>(sel, is_required, false)
297314
}
298315

objc/src/encode.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K, L);
5454

5555
#[cfg(test)]
5656
mod tests {
57-
use objc_encode::Encode;
5857
use crate::runtime::{Class, Object, Sel};
58+
use objc_encode::Encode;
5959

6060
#[test]
6161
fn test_encode() {

objc/src/exception.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ use crate::rc::StrongPtr;
44
use crate::runtime::Object;
55

66
pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
7-
where F: FnOnce() -> R {
8-
objc_exception::r#try(closure).map_err(|exception| {
9-
StrongPtr::new(exception as *mut Object)
10-
})
7+
where
8+
F: FnOnce() -> R,
9+
{
10+
objc_exception::r#try(closure).map_err(|exception| StrongPtr::new(exception as *mut Object))
1111
}

objc/src/lib.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ The bindings can be used on Linux or *BSD utilizing the
6262

6363
#![crate_name = "objc"]
6464
#![crate_type = "lib"]
65-
6665
#![warn(missing_docs)]
6766

6867
extern crate malloc_buf;
@@ -83,14 +82,14 @@ pub use crate::message::send_super_message as __send_super_message;
8382
#[macro_use]
8483
mod macros;
8584

86-
pub mod runtime;
87-
pub mod declare;
88-
pub mod rc;
8985
mod cache;
86+
pub mod declare;
9087
mod encode;
9188
#[cfg(feature = "exception")]
9289
mod exception;
9390
mod message;
91+
pub mod rc;
92+
pub mod runtime;
9493

9594
#[cfg(test)]
9695
mod test_utils;

objc/src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ let cls = class!(NSObject);
1414
*/
1515
#[macro_export]
1616
macro_rules! class {
17-
($name:ident) => ({
17+
($name:ident) => {{
1818
static CLASS: $crate::__CachedClass = $crate::__CachedClass::new();
1919
let name = concat!(stringify!($name), '\0');
2020
#[allow(unused_unsafe)]
@@ -23,7 +23,7 @@ macro_rules! class {
2323
Some(cls) => cls,
2424
None => panic!("Class with name {} could not be found", stringify!($name)),
2525
}
26-
})
26+
}};
2727
}
2828

2929
/**

0 commit comments

Comments
 (0)