Skip to content

Commit 32a9de8

Browse files
authored
Merge pull request #3 from madsmtm/chores
Fix warnings, and other chores
2 parents 2a67179 + 30d83ea commit 32a9de8

35 files changed

+165
-136
lines changed

objc_foundation/examples/example.rs renamed to examples/foundation.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
extern crate objc_foundation;
2-
31
use objc_foundation::{
42
INSArray, INSCopying, INSDictionary, INSObject, INSString, NSArray, NSDictionary, NSObject,
53
NSString,

objc_foundation/examples/custom_class.rs renamed to examples/foundation_custom_class.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
#[macro_use]
2-
extern crate objc;
3-
extern crate objc_foundation;
4-
51
use std::sync::{Once, ONCE_INIT};
62

73
use objc::declare::ClassDecl;
4+
use objc::msg_send;
85
use objc::runtime::{Class, Object, Sel};
96
use objc::Message;
107
use objc_foundation::{INSObject, NSObject};

objc/examples/example.rs renamed to examples/objc.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
#[macro_use]
2-
extern crate objc;
3-
41
use objc::rc::StrongPtr;
52
use objc::runtime::{Class, Object};
6-
use objc::Encode;
3+
use objc::{class, msg_send, Encode};
74

85
fn main() {
96
// Get a class

objc/src/declare.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ The following example demonstrates declaring a class named `MyNumber` that has
1010
one ivar, a `u32` named `_number` and a `number` method that returns it:
1111
1212
``` no_run
13-
# #[macro_use] extern crate objc;
13+
# use objc::class;
1414
# use objc::declare::ClassDecl;
1515
# use objc::runtime::{Class, Object, Sel};
1616
# fn main() {
@@ -91,7 +91,7 @@ fn count_args(sel: Sel) -> usize {
9191
sel.name().chars().filter(|&c| c == ':').count()
9292
}
9393

94-
fn method_type_encoding(ret: &Encoding, args: &[Encoding]) -> CString {
94+
fn method_type_encoding(ret: &Encoding<'_>, args: &[Encoding<'_>]) -> CString {
9595
// First two arguments are always self and the selector
9696
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING);
9797
for enc in args {
@@ -123,7 +123,7 @@ impl ClassDecl {
123123
if cls.is_null() {
124124
None
125125
} else {
126-
Some(ClassDecl { cls: cls })
126+
Some(ClassDecl { cls })
127127
}
128128
}
129129

@@ -159,8 +159,11 @@ impl ClassDecl {
159159
/// Adds a method with the given name and implementation to self.
160160
/// Panics if the method wasn't sucessfully added
161161
/// or if the selector and function take different numbers of arguments.
162-
/// Unsafe because the caller must ensure that the types match those that
163-
/// are expected when the method is invoked from Objective-C.
162+
///
163+
/// # Safety
164+
///
165+
/// The caller must ensure that the types match those that are expected
166+
/// when the method is invoked from Objective-C.
164167
pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
165168
where
166169
F: MethodImplementation<Callee = Object>,
@@ -182,8 +185,11 @@ impl ClassDecl {
182185
/// Adds a class method with the given name and implementation to self.
183186
/// Panics if the method wasn't sucessfully added
184187
/// or if the selector and function take different numbers of arguments.
185-
/// Unsafe because the caller must ensure that the types match those that
186-
/// are expected when the method is invoked from Objective-C.
188+
///
189+
/// # Safety
190+
///
191+
/// The caller must ensure that the types match those that are expected
192+
/// when the method is invoked from Objective-C.
187193
pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
188194
where
189195
F: MethodImplementation<Callee = Class>,
@@ -262,7 +268,7 @@ impl ProtocolDecl {
262268
if proto.is_null() {
263269
None
264270
} else {
265-
Some(ProtocolDecl { proto: proto })
271+
Some(ProtocolDecl { proto })
266272
}
267273
}
268274

objc/src/exception.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,19 @@ use objc_exception;
33
use crate::rc::StrongPtr;
44
use crate::runtime::Object;
55

6+
// Comment copied from `objc_exception`
7+
8+
/// Tries to execute the given closure and catches an Objective-C exception
9+
/// if one is thrown.
10+
///
11+
/// Returns a `Result` that is either `Ok` if the closure succeeded without an
12+
/// exception being thrown, or an `Err` with a pointer to an exception if one
13+
/// was thrown. The exception is retained and so must be released.
14+
///
15+
/// # Safety
16+
///
17+
/// This encourages unwinding through the closure from Objective-C, which is
18+
/// not safe.
619
pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
720
where
821
F: FnOnce() -> R,

objc/src/lib.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Objective-C Runtime bindings and wrapper for Rust.
66
Objective-C objects can be messaged using the [`msg_send!`](macro.msg_send!.html) macro:
77
88
``` no_run
9-
# #[macro_use] extern crate objc;
9+
# use objc::{class, msg_send};
1010
# use objc::runtime::{BOOL, Class, Object};
1111
# fn main() {
1212
# unsafe {
@@ -63,11 +63,7 @@ The bindings can be used on Linux or *BSD utilizing the
6363
#![crate_name = "objc"]
6464
#![crate_type = "lib"]
6565
#![warn(missing_docs)]
66-
67-
extern crate malloc_buf;
68-
extern crate objc_encode;
69-
#[cfg(feature = "exception")]
70-
extern crate objc_exception;
66+
#![allow(clippy::missing_safety_doc)]
7167

7268
pub use objc_encode::{Encode, Encoding};
7369

objc/src/macros.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ To check for a class that may not exist, use `Class::get`.
66
77
# Example
88
``` no_run
9-
# #[macro_use] extern crate objc;
109
# fn main() {
1110
let cls = class!(NSObject);
1211
# }
@@ -31,7 +30,6 @@ Registers a selector, returning a `Sel`.
3130
3231
# Example
3332
```
34-
# #[macro_use] extern crate objc;
3533
# fn main() {
3634
let sel = sel!(description);
3735
let sel = sel!(setObject:forKey:);
@@ -64,7 +62,6 @@ Variadic arguments are not currently supported.
6462
6563
# Example
6664
``` no_run
67-
# #[macro_use] extern crate objc;
6865
# use objc::runtime::Object;
6966
# fn main() {
7067
# unsafe {

objc/src/message/apple/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ where
4242
{
4343
let sup = Super {
4444
receiver: obj as *mut T as *mut Object,
45-
superclass: superclass,
45+
superclass,
4646
};
4747
let receiver = &sup as *const Super as *mut Object;
4848
let msg_send_fn = msg_send_super_fn::<R>();

objc/src/message/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub unsafe trait Message {
7070
send_message(self, sel, args)
7171
}
7272

73+
#[allow(missing_docs)]
7374
#[cfg(feature = "verify_message")]
7475
unsafe fn send_message<A, R>(&self, sel: Sel, args: A) -> Result<R, MessageError>
7576
where
@@ -90,7 +91,7 @@ pub unsafe trait Message {
9091
9192
# Example
9293
``` no_run
93-
# #[macro_use] extern crate objc;
94+
# use objc::{class, msg_send};
9495
# use objc::runtime::{BOOL, Class, Object};
9596
# use objc::Message;
9697
# fn main() {
@@ -195,7 +196,7 @@ Currently, an error may be returned in two cases:
195196
pub struct MessageError(String);
196197

197198
impl fmt::Display for MessageError {
198-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
199+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199200
fmt::Display::fmt(&self.0, f)
200201
}
201202
}
@@ -207,7 +208,7 @@ impl Error for MessageError {
207208
}
208209

209210
impl<'a> From<VerificationError<'a>> for MessageError {
210-
fn from(err: VerificationError) -> MessageError {
211+
fn from(err: VerificationError<'_>) -> MessageError {
211212
MessageError(err.to_string())
212213
}
213214
}
@@ -284,8 +285,7 @@ where
284285

285286
#[cfg(test)]
286287
mod tests {
287-
use super::Message;
288-
use crate::runtime::Object;
288+
use super::*;
289289
use crate::test_utils;
290290

291291
#[test]

objc/src/message/verify.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub enum VerificationError<'a> {
1212
}
1313

1414
impl<'a> fmt::Display for VerificationError<'a> {
15-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1616
match *self {
1717
VerificationError::NilReceiver(sel) => {
1818
write!(f, "Messsaging {:?} to nil", sel)
@@ -55,7 +55,7 @@ impl<'a> fmt::Display for VerificationError<'a> {
5555
}
5656
}
5757

58-
pub fn verify_message_signature<A, R>(cls: &Class, sel: Sel) -> Result<(), VerificationError>
58+
pub fn verify_message_signature<A, R>(cls: &Class, sel: Sel) -> Result<(), VerificationError<'_>>
5959
where
6060
A: EncodeArguments,
6161
R: Encode,

objc/src/rc/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ For more information on Objective-C's reference counting, see Apple's documentat
1616
# Example
1717
1818
``` no_run
19-
# #[macro_use] extern crate objc;
19+
# use objc::{class, msg_send};
2020
# use objc::rc::{autoreleasepool, StrongPtr};
2121
# fn main() {
2222
// StrongPtr will release the object when dropped
@@ -91,8 +91,11 @@ mod tests {
9191
let weak = obj.weak();
9292

9393
let weak2 = weak.clone();
94-
let strong = weak2.load();
94+
95+
let strong = weak.load();
96+
let strong2 = weak2.load();
9597
assert!(*strong == *obj);
98+
assert!(*strong2 == *obj);
9699
}
97100

98101
#[test]

objc/src/rc/strong.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,20 @@ impl StrongPtr {
1212
/// Constructs a `StrongPtr` to a newly created object that already has a
1313
/// +1 retain count. This will not retain the object.
1414
/// When dropped, the object will be released.
15-
/// Unsafe because the caller must ensure the given object pointer is valid.
15+
///
16+
/// # Safety
17+
///
18+
/// The caller must ensure the given object pointer is valid.
1619
pub unsafe fn new(ptr: *mut Object) -> Self {
1720
StrongPtr(ptr)
1821
}
1922

2023
/// Retains the given object and constructs a `StrongPtr` to it.
2124
/// When dropped, the object will be released.
22-
/// Unsafe because the caller must ensure the given object pointer is valid.
25+
///
26+
/// # Safety
27+
///
28+
/// The caller must ensure the given object pointer is valid.
2329
pub unsafe fn retain(ptr: *mut Object) -> Self {
2430
StrongPtr(runtime::objc_retain(ptr))
2531
}
@@ -65,7 +71,7 @@ impl Deref for StrongPtr {
6571
}
6672

6773
impl fmt::Pointer for StrongPtr {
68-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
74+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6975
fmt::Pointer::fmt(&self.0, f)
7076
}
7177
}

objc/src/rc/weak.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ pub struct WeakPtr(Box<UnsafeCell<*mut Object>>);
1414

1515
impl WeakPtr {
1616
/// Constructs a `WeakPtr` to the given object.
17-
/// Unsafe because the caller must ensure the given object pointer is valid.
17+
///
18+
/// # Safety
19+
///
20+
/// The caller must ensure the given object pointer is valid.
1821
pub unsafe fn new(obj: *mut Object) -> Self {
1922
let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
2023
runtime::objc_initWeak(ptr.get(), obj);

objc/src/runtime.rs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub struct Object {
7474
/// A pointer to the start of a method implementation.
7575
pub type Imp = unsafe extern "C" fn();
7676

77+
#[allow(missing_docs)]
7778
#[link(name = "objc", kind = "dylib")]
7879
extern "C" {
7980
pub fn sel_registerName(name: *const c_char) -> Sel;
@@ -176,10 +177,14 @@ impl Sel {
176177

177178
/// Wraps a raw pointer to a selector into a `Sel` object.
178179
///
180+
/// # Safety
181+
///
182+
/// The pointer must a valid, registered selector.
183+
///
179184
/// This is almost never what you want; use `Sel::register()` instead.
180185
#[inline]
181186
pub unsafe fn from_ptr(ptr: *const c_void) -> Sel {
182-
Sel { ptr: ptr }
187+
Sel { ptr }
183188
}
184189

185190
/// Returns a pointer to the raw selector.
@@ -210,7 +215,7 @@ impl Clone for Sel {
210215
}
211216

212217
impl fmt::Debug for Sel {
213-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
214219
write!(f, "{}", self.name())
215220
}
216221
}
@@ -405,7 +410,7 @@ impl PartialEq for Class {
405410
impl Eq for Class {}
406411

407412
impl fmt::Debug for Class {
408-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
413+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
409414
write!(f, "{}", self.name())
410415
}
411416
}
@@ -464,7 +469,7 @@ impl PartialEq for Protocol {
464469
impl Eq for Protocol {}
465470

466471
impl fmt::Debug for Protocol {
467-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
472+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
468473
write!(f, "{}", self.name())
469474
}
470475
}
@@ -477,8 +482,10 @@ impl Object {
477482

478483
/// Returns a reference to the ivar of self with the given name.
479484
/// Panics if self has no ivar with the given name.
480-
/// Unsafe because the caller must ensure that the ivar is actually
481-
/// of type `T`.
485+
///
486+
/// # Safety
487+
///
488+
/// The caller must ensure that the ivar is actually of type `T`.
482489
pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
483490
where
484491
T: Encode,
@@ -502,8 +509,10 @@ impl Object {
502509

503510
/// Returns a mutable reference to the ivar of self with the given name.
504511
/// Panics if self has no ivar with the given name.
505-
/// Unsafe because the caller must ensure that the ivar is actually
506-
/// of type `T`.
512+
///
513+
/// # Safety
514+
///
515+
/// The caller must ensure that the ivar is actually of type `T`.
507516
pub unsafe fn get_mut_ivar<T>(&mut self, name: &str) -> &mut T
508517
where
509518
T: Encode,
@@ -527,8 +536,10 @@ impl Object {
527536

528537
/// Sets the value of the ivar of self with the given name.
529538
/// Panics if self has no ivar with the given name.
530-
/// Unsafe because the caller must ensure that the ivar is actually
531-
/// of type `T`.
539+
///
540+
/// # Safety
541+
///
542+
/// The caller must ensure that the ivar is actually of type `T`.
532543
pub unsafe fn set_ivar<T>(&mut self, name: &str, value: T)
533544
where
534545
T: Encode,
@@ -538,7 +549,7 @@ impl Object {
538549
}
539550

540551
impl fmt::Debug for Object {
541-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
552+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
542553
write!(f, "<{:?}: {:p}>", self.class(), self)
543554
}
544555
}

0 commit comments

Comments
 (0)