Skip to content

Commit

Permalink
Merge pull request #3 from madsmtm/chores
Browse files Browse the repository at this point in the history
Fix warnings, and other chores
  • Loading branch information
madsmtm authored Aug 29, 2021
2 parents 2a67179 + 30d83ea commit 32a9de8
Show file tree
Hide file tree
Showing 35 changed files with 165 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
extern crate objc_foundation;

use objc_foundation::{
INSArray, INSCopying, INSDictionary, INSObject, INSString, NSArray, NSDictionary, NSObject,
NSString,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#[macro_use]
extern crate objc;
extern crate objc_foundation;

use std::sync::{Once, ONCE_INIT};

use objc::declare::ClassDecl;
use objc::msg_send;
use objc::runtime::{Class, Object, Sel};
use objc::Message;
use objc_foundation::{INSObject, NSObject};
Expand Down
5 changes: 1 addition & 4 deletions objc/examples/example.rs → examples/objc.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#[macro_use]
extern crate objc;

use objc::rc::StrongPtr;
use objc::runtime::{Class, Object};
use objc::Encode;
use objc::{class, msg_send, Encode};

fn main() {
// Get a class
Expand Down
22 changes: 14 additions & 8 deletions objc/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ The following example demonstrates declaring a class named `MyNumber` that has
one ivar, a `u32` named `_number` and a `number` method that returns it:
``` no_run
# #[macro_use] extern crate objc;
# use objc::class;
# use objc::declare::ClassDecl;
# use objc::runtime::{Class, Object, Sel};
# fn main() {
Expand Down Expand Up @@ -91,7 +91,7 @@ fn count_args(sel: Sel) -> usize {
sel.name().chars().filter(|&c| c == ':').count()
}

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

Expand Down Expand Up @@ -159,8 +159,11 @@ impl ClassDecl {
/// Adds a method with the given name and implementation to self.
/// Panics if the method wasn't sucessfully added
/// or if the selector and function take different numbers of arguments.
/// Unsafe because the caller must ensure that the types match those that
/// are expected when the method is invoked from Objective-C.
///
/// # Safety
///
/// The caller must ensure that the types match those that are expected
/// when the method is invoked from Objective-C.
pub unsafe fn add_method<F>(&mut self, sel: Sel, func: F)
where
F: MethodImplementation<Callee = Object>,
Expand All @@ -182,8 +185,11 @@ impl ClassDecl {
/// Adds a class method with the given name and implementation to self.
/// Panics if the method wasn't sucessfully added
/// or if the selector and function take different numbers of arguments.
/// Unsafe because the caller must ensure that the types match those that
/// are expected when the method is invoked from Objective-C.
///
/// # Safety
///
/// The caller must ensure that the types match those that are expected
/// when the method is invoked from Objective-C.
pub unsafe fn add_class_method<F>(&mut self, sel: Sel, func: F)
where
F: MethodImplementation<Callee = Class>,
Expand Down Expand Up @@ -262,7 +268,7 @@ impl ProtocolDecl {
if proto.is_null() {
None
} else {
Some(ProtocolDecl { proto: proto })
Some(ProtocolDecl { proto })
}
}

Expand Down
13 changes: 13 additions & 0 deletions objc/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@ use objc_exception;
use crate::rc::StrongPtr;
use crate::runtime::Object;

// Comment copied from `objc_exception`

/// Tries to execute the given closure and catches an Objective-C exception
/// if one is thrown.
///
/// Returns a `Result` that is either `Ok` if the closure succeeded without an
/// exception being thrown, or an `Err` with a pointer to an exception if one
/// was thrown. The exception is retained and so must be released.
///
/// # Safety
///
/// This encourages unwinding through the closure from Objective-C, which is
/// not safe.
pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
where
F: FnOnce() -> R,
Expand Down
8 changes: 2 additions & 6 deletions objc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Objective-C Runtime bindings and wrapper for Rust.
Objective-C objects can be messaged using the [`msg_send!`](macro.msg_send!.html) macro:
``` no_run
# #[macro_use] extern crate objc;
# use objc::{class, msg_send};
# use objc::runtime::{BOOL, Class, Object};
# fn main() {
# unsafe {
Expand Down Expand Up @@ -63,11 +63,7 @@ The bindings can be used on Linux or *BSD utilizing the
#![crate_name = "objc"]
#![crate_type = "lib"]
#![warn(missing_docs)]

extern crate malloc_buf;
extern crate objc_encode;
#[cfg(feature = "exception")]
extern crate objc_exception;
#![allow(clippy::missing_safety_doc)]

pub use objc_encode::{Encode, Encoding};

Expand Down
3 changes: 0 additions & 3 deletions objc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ To check for a class that may not exist, use `Class::get`.
# Example
``` no_run
# #[macro_use] extern crate objc;
# fn main() {
let cls = class!(NSObject);
# }
Expand All @@ -31,7 +30,6 @@ Registers a selector, returning a `Sel`.
# Example
```
# #[macro_use] extern crate objc;
# fn main() {
let sel = sel!(description);
let sel = sel!(setObject:forKey:);
Expand Down Expand Up @@ -64,7 +62,6 @@ Variadic arguments are not currently supported.
# Example
``` no_run
# #[macro_use] extern crate objc;
# use objc::runtime::Object;
# fn main() {
# unsafe {
Expand Down
2 changes: 1 addition & 1 deletion objc/src/message/apple/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ where
{
let sup = Super {
receiver: obj as *mut T as *mut Object,
superclass: superclass,
superclass,
};
let receiver = &sup as *const Super as *mut Object;
let msg_send_fn = msg_send_super_fn::<R>();
Expand Down
10 changes: 5 additions & 5 deletions objc/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub unsafe trait Message {
send_message(self, sel, args)
}

#[allow(missing_docs)]
#[cfg(feature = "verify_message")]
unsafe fn send_message<A, R>(&self, sel: Sel, args: A) -> Result<R, MessageError>
where
Expand All @@ -90,7 +91,7 @@ pub unsafe trait Message {
# Example
``` no_run
# #[macro_use] extern crate objc;
# use objc::{class, msg_send};
# use objc::runtime::{BOOL, Class, Object};
# use objc::Message;
# fn main() {
Expand Down Expand Up @@ -195,7 +196,7 @@ Currently, an error may be returned in two cases:
pub struct MessageError(String);

impl fmt::Display for MessageError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
Expand All @@ -207,7 +208,7 @@ impl Error for MessageError {
}

impl<'a> From<VerificationError<'a>> for MessageError {
fn from(err: VerificationError) -> MessageError {
fn from(err: VerificationError<'_>) -> MessageError {
MessageError(err.to_string())
}
}
Expand Down Expand Up @@ -284,8 +285,7 @@ where

#[cfg(test)]
mod tests {
use super::Message;
use crate::runtime::Object;
use super::*;
use crate::test_utils;

#[test]
Expand Down
4 changes: 2 additions & 2 deletions objc/src/message/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum VerificationError<'a> {
}

impl<'a> fmt::Display for VerificationError<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
VerificationError::NilReceiver(sel) => {
write!(f, "Messsaging {:?} to nil", sel)
Expand Down Expand Up @@ -55,7 +55,7 @@ impl<'a> fmt::Display for VerificationError<'a> {
}
}

pub fn verify_message_signature<A, R>(cls: &Class, sel: Sel) -> Result<(), VerificationError>
pub fn verify_message_signature<A, R>(cls: &Class, sel: Sel) -> Result<(), VerificationError<'_>>
where
A: EncodeArguments,
R: Encode,
Expand Down
7 changes: 5 additions & 2 deletions objc/src/rc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ For more information on Objective-C's reference counting, see Apple's documentat
# Example
``` no_run
# #[macro_use] extern crate objc;
# use objc::{class, msg_send};
# use objc::rc::{autoreleasepool, StrongPtr};
# fn main() {
// StrongPtr will release the object when dropped
Expand Down Expand Up @@ -91,8 +91,11 @@ mod tests {
let weak = obj.weak();

let weak2 = weak.clone();
let strong = weak2.load();

let strong = weak.load();
let strong2 = weak2.load();
assert!(*strong == *obj);
assert!(*strong2 == *obj);
}

#[test]
Expand Down
12 changes: 9 additions & 3 deletions objc/src/rc/strong.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ impl StrongPtr {
/// Constructs a `StrongPtr` to a newly created object that already has a
/// +1 retain count. This will not retain the object.
/// When dropped, the object will be released.
/// Unsafe because the caller must ensure the given object pointer is valid.
///
/// # Safety
///
/// The caller must ensure the given object pointer is valid.
pub unsafe fn new(ptr: *mut Object) -> Self {
StrongPtr(ptr)
}

/// Retains the given object and constructs a `StrongPtr` to it.
/// When dropped, the object will be released.
/// Unsafe because the caller must ensure the given object pointer is valid.
///
/// # Safety
///
/// The caller must ensure the given object pointer is valid.
pub unsafe fn retain(ptr: *mut Object) -> Self {
StrongPtr(runtime::objc_retain(ptr))
}
Expand Down Expand Up @@ -65,7 +71,7 @@ impl Deref for StrongPtr {
}

impl fmt::Pointer for StrongPtr {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Pointer::fmt(&self.0, f)
}
}
5 changes: 4 additions & 1 deletion objc/src/rc/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ pub struct WeakPtr(Box<UnsafeCell<*mut Object>>);

impl WeakPtr {
/// Constructs a `WeakPtr` to the given object.
/// Unsafe because the caller must ensure the given object pointer is valid.
///
/// # Safety
///
/// The caller must ensure the given object pointer is valid.
pub unsafe fn new(obj: *mut Object) -> Self {
let ptr = Box::new(UnsafeCell::new(ptr::null_mut()));
runtime::objc_initWeak(ptr.get(), obj);
Expand Down
33 changes: 22 additions & 11 deletions objc/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct Object {
/// A pointer to the start of a method implementation.
pub type Imp = unsafe extern "C" fn();

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

/// Wraps a raw pointer to a selector into a `Sel` object.
///
/// # Safety
///
/// The pointer must a valid, registered selector.
///
/// This is almost never what you want; use `Sel::register()` instead.
#[inline]
pub unsafe fn from_ptr(ptr: *const c_void) -> Sel {
Sel { ptr: ptr }
Sel { ptr }
}

/// Returns a pointer to the raw selector.
Expand Down Expand Up @@ -210,7 +215,7 @@ impl Clone for Sel {
}

impl fmt::Debug for Sel {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
Expand Down Expand Up @@ -405,7 +410,7 @@ impl PartialEq for Class {
impl Eq for Class {}

impl fmt::Debug for Class {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
Expand Down Expand Up @@ -464,7 +469,7 @@ impl PartialEq for Protocol {
impl Eq for Protocol {}

impl fmt::Debug for Protocol {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
Expand All @@ -477,8 +482,10 @@ impl Object {

/// Returns a reference to the ivar of self with the given name.
/// Panics if self has no ivar with the given name.
/// Unsafe because the caller must ensure that the ivar is actually
/// of type `T`.
///
/// # Safety
///
/// The caller must ensure that the ivar is actually of type `T`.
pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
where
T: Encode,
Expand All @@ -502,8 +509,10 @@ impl Object {

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

/// Sets the value of the ivar of self with the given name.
/// Panics if self has no ivar with the given name.
/// Unsafe because the caller must ensure that the ivar is actually
/// of type `T`.
///
/// # Safety
///
/// The caller must ensure that the ivar is actually of type `T`.
pub unsafe fn set_ivar<T>(&mut self, name: &str, value: T)
where
T: Encode,
Expand All @@ -538,7 +549,7 @@ impl Object {
}

impl fmt::Debug for Object {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<{:?}: {:p}>", self.class(), self)
}
}
Expand Down
Loading

0 comments on commit 32a9de8

Please sign in to comment.