Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run cargo fmt #2

Merged
merged 2 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion objc/.travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: rust
rust:
- stable
- stable
- nightly
os:
- osx
Expand Down
4 changes: 2 additions & 2 deletions objc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
* C types are now used from `std::os::raw` rather than `libc`. This means
`Encode` may not be implemented for `libc` types; switch them to the
`std::os::raw` equivalents instead. This avoids an issue that would arise
from simultaneously using different versions of the libc crate.
from simultaneously using different versions of the libc crate.

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

* Corrected alignment of ivars in `ClassDecl`; declared classes may now have a
smaller size.
smaller size.

* With the `"exception"` or `"verify_message"` feature enabled, panics from
`msg_send!` will now be triggered from the line and file where the macro is
Expand Down
14 changes: 7 additions & 7 deletions objc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ documentation = "http://ssheldon.github.io/rust-objc/objc/"
license = "MIT"

exclude = [
".gitignore",
".travis.yml",
"doc.sh",
"travis_install.sh",
"travis_test.sh",
"tests-ios/**",
]
".gitignore",
".travis.yml",
"doc.sh",
"travis_install.sh",
"travis_test.sh",
"tests-ios/**",
]

[features]
exception = ["objc_exception"]
Expand Down
10 changes: 3 additions & 7 deletions objc/examples/example.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#[macro_use]
extern crate objc;

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

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

// Access an ivar of the object
let isa: *const Class = unsafe {
*(**obj).get_ivar("isa")
};
let isa: *const Class = unsafe { *(**obj).get_ivar("isa") };
println!("NSObject isa: {:?}", isa);

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

// Invoke a method on the object
let hash: usize = unsafe {
msg_send![*obj, hash]
};
let hash: usize = unsafe { msg_send![*obj, hash] };
println!("NSObject hash: {}", hash);
}
10 changes: 5 additions & 5 deletions objc/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ use std::os::raw::c_void;
use std::ptr;
use std::sync::atomic::{AtomicPtr, Ordering};

use crate::runtime::{Class, Sel, self};
use crate::runtime::{self, Class, Sel};

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

impl CachedSel {
/// Constructs a new `CachedSel`.
pub const fn new() -> CachedSel {
CachedSel {
ptr: AtomicPtr::new(ptr::null_mut())
ptr: AtomicPtr::new(ptr::null_mut()),
}
}

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

impl CachedClass {
/// Constructs a new `CachedClass`.
pub const fn new() -> CachedClass {
CachedClass {
ptr: AtomicPtr::new(ptr::null_mut())
ptr: AtomicPtr::new(ptr::null_mut()),
}
}

Expand Down
93 changes: 55 additions & 38 deletions objc/src/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use std::ffi::CString;
use std::mem;
use std::ptr;

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

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

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);
let mut types = format!("{}{}{}", ret, <*mut Object>::ENCODING, Sel::ENCODING);
for enc in args {
use std::fmt::Write;
write!(&mut types, "{}", enc).unwrap();
Expand All @@ -117,13 +116,10 @@ pub struct ClassDecl {
}

impl ClassDecl {
fn with_superclass(name: &str, superclass: Option<&Class>)
-> Option<ClassDecl> {
fn with_superclass(name: &str, superclass: Option<&Class>) -> Option<ClassDecl> {
let name = CString::new(name).unwrap();
let super_ptr = superclass.map_or(ptr::null(), |c| c);
let cls = unsafe {
runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0)
};
let cls = unsafe { runtime::objc_allocateClassPair(super_ptr, name.as_ptr(), 0) };
if cls.is_null() {
None
} else {
Expand All @@ -150,8 +146,7 @@ impl ClassDecl {
Functionality it expects, like implementations of `-retain` and `-release`
used by ARC, will not be present otherwise.
*/
pub fn root(name: &str, intitialize_fn: extern fn(&Class, Sel))
-> Option<ClassDecl> {
pub fn root(name: &str, intitialize_fn: extern "C" fn(&Class, Sel)) -> Option<ClassDecl> {
let mut decl = ClassDecl::with_superclass(name, None);
if let Some(ref mut decl) = decl {
unsafe {
Expand All @@ -167,17 +162,20 @@ impl ClassDecl {
/// Unsafe because 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> {
where
F: MethodImplementation<Callee = Object>,
{
let encs = F::Args::ENCODINGS;
let sel_args = count_args(sel);
assert!(sel_args == encs.len(),
assert!(
sel_args == encs.len(),
"Selector accepts {} arguments, but function accepts {}",
sel_args, encs.len(),
sel_args,
encs.len(),
);

let types = method_type_encoding(&F::Ret::ENCODING, encs);
let success = runtime::class_addMethod(self.cls, sel, func.imp(),
types.as_ptr());
let success = runtime::class_addMethod(self.cls, sel, func.imp(), types.as_ptr());
assert!(success != NO, "Failed to add method {:?}", sel);
}

Expand All @@ -187,31 +185,36 @@ impl ClassDecl {
/// Unsafe because 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> {
where
F: MethodImplementation<Callee = Class>,
{
let encs = F::Args::ENCODINGS;
let sel_args = count_args(sel);
assert!(sel_args == encs.len(),
assert!(
sel_args == encs.len(),
"Selector accepts {} arguments, but function accepts {}",
sel_args, encs.len(),
sel_args,
encs.len(),
);

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

/// Adds an ivar with type `T` and the provided name to self.
/// Panics if the ivar wasn't successfully added.
pub fn add_ivar<T>(&mut self, name: &str) where T: Encode {
pub fn add_ivar<T>(&mut self, name: &str)
where
T: Encode,
{
let c_name = CString::new(name).unwrap();
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
let size = mem::size_of::<T>();
let align = log2_align_of::<T>();
let success = unsafe {
runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align,
encoding.as_ptr())
runtime::class_addIvar(self.cls, c_name.as_ptr(), size, align, encoding.as_ptr())
};
assert!(success != NO, "Failed to add ivar {}", name);
}
Expand Down Expand Up @@ -247,52 +250,66 @@ impl Drop for ClassDecl {
/// A type for declaring a new protocol and adding new methods to it
/// before registering it.
pub struct ProtocolDecl {
proto: *mut Protocol
proto: *mut Protocol,
}

impl ProtocolDecl {
/// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
/// protocol couldn't be allocated.
pub fn new(name: &str) -> Option<ProtocolDecl> {
let c_name = CString::new(name).unwrap();
let proto = unsafe {
runtime::objc_allocateProtocol(c_name.as_ptr())
};
let proto = unsafe { runtime::objc_allocateProtocol(c_name.as_ptr()) };
if proto.is_null() {
None
} else {
Some(ProtocolDecl { proto: proto })
}
}

fn add_method_description_common<Args, Ret>(&mut self, sel: Sel, is_required: bool,
is_instance_method: bool)
where Args: EncodeArguments,
Ret: Encode {
fn add_method_description_common<Args, Ret>(
&mut self,
sel: Sel,
is_required: bool,
is_instance_method: bool,
) where
Args: EncodeArguments,
Ret: Encode,
{
let encs = Args::ENCODINGS;
let sel_args = count_args(sel);
assert!(sel_args == encs.len(),
assert!(
sel_args == encs.len(),
"Selector accepts {} arguments, but function accepts {}",
sel_args, encs.len(),
sel_args,
encs.len(),
);
let types = method_type_encoding(&Ret::ENCODING, encs);
unsafe {
runtime::protocol_addMethodDescription(
self.proto, sel, types.as_ptr(), is_required as BOOL, is_instance_method as BOOL);
self.proto,
sel,
types.as_ptr(),
is_required as BOOL,
is_instance_method as BOOL,
);
}
}

/// Adds an instance method declaration with a given description to self.
pub fn add_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
where Args: EncodeArguments,
Ret: Encode {
where
Args: EncodeArguments,
Ret: Encode,
{
self.add_method_description_common::<Args, Ret>(sel, is_required, true)
}

/// Adds a class method declaration with a given description to self.
pub fn add_class_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
where Args: EncodeArguments,
Ret: Encode {
where
Args: EncodeArguments,
Ret: Encode,
{
self.add_method_description_common::<Args, Ret>(sel, is_required, false)
}

Expand Down
2 changes: 1 addition & 1 deletion objc/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ encode_args_impl!(A, B, C, D, E, F, G, H, I, J, K, L);

#[cfg(test)]
mod tests {
use objc_encode::Encode;
use crate::runtime::{Class, Object, Sel};
use objc_encode::Encode;

#[test]
fn test_encode() {
Expand Down
8 changes: 4 additions & 4 deletions objc/src/exception.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::rc::StrongPtr;
use crate::runtime::Object;

pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
where F: FnOnce() -> R {
objc_exception::r#try(closure).map_err(|exception| {
StrongPtr::new(exception as *mut Object)
})
where
F: FnOnce() -> R,
{
objc_exception::r#try(closure).map_err(|exception| StrongPtr::new(exception as *mut Object))
}
7 changes: 3 additions & 4 deletions objc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ The bindings can be used on Linux or *BSD utilizing the

#![crate_name = "objc"]
#![crate_type = "lib"]

#![warn(missing_docs)]

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

pub mod runtime;
pub mod declare;
pub mod rc;
mod cache;
pub mod declare;
mod encode;
#[cfg(feature = "exception")]
mod exception;
mod message;
pub mod rc;
pub mod runtime;

#[cfg(test)]
mod test_utils;
4 changes: 2 additions & 2 deletions objc/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let cls = class!(NSObject);
*/
#[macro_export]
macro_rules! class {
($name:ident) => ({
($name:ident) => {{
static CLASS: $crate::__CachedClass = $crate::__CachedClass::new();
let name = concat!(stringify!($name), '\0');
#[allow(unused_unsafe)]
Expand All @@ -23,7 +23,7 @@ macro_rules! class {
Some(cls) => cls,
None => panic!("Class with name {} could not be found", stringify!($name)),
}
})
}};
}

/**
Expand Down
Loading