Skip to content

Commit fa2f1f9

Browse files
committed
Various documentation fixes and cleanups
- Make a few `where` clauses simpler - Remove unnecessary `fn main` from doctests - Fix a bunch of item links - Document `objc::Message` better - Refactor `Object` impl a bit
1 parent a970cc3 commit fa2f1f9

File tree

15 files changed

+195
-176
lines changed

15 files changed

+195
-176
lines changed

objc/src/cache.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ use core::sync::atomic::{AtomicPtr, Ordering};
44

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

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

1313
impl CachedSel {
14-
/// Constructs a new `CachedSel`.
14+
/// Constructs a new [`CachedSel`].
1515
pub const fn new() -> CachedSel {
1616
CachedSel {
1717
ptr: AtomicPtr::new(ptr::null_mut()),
@@ -35,14 +35,14 @@ impl CachedSel {
3535
}
3636
}
3737

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

4444
impl CachedClass {
45-
/// Constructs a new `CachedClass`.
45+
/// Constructs a new [`CachedClass`].
4646
pub const fn new() -> CachedClass {
4747
CachedClass {
4848
ptr: AtomicPtr::new(ptr::null_mut()),

objc/src/declare.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*!
22
Functionality for declaring Objective-C classes.
33
4-
Classes can be declared using the `ClassDecl` struct. Instance variables and
4+
Classes can be declared using the [`ClassDecl`] struct. Instance variables and
55
methods can then be added before the class is ultimately registered.
66
77
# Example
@@ -13,7 +13,6 @@ one ivar, a `u32` named `_number` and a `number` method that returns it:
1313
# use objc::{class, sel};
1414
# use objc::declare::ClassDecl;
1515
# use objc::runtime::{Class, Object, Sel};
16-
# fn main() {
1716
let superclass = class!(NSObject);
1817
let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();
1918
@@ -30,7 +29,6 @@ unsafe {
3029
}
3130
3231
decl.register();
33-
# }
3432
```
3533
*/
3634

@@ -52,7 +50,7 @@ pub trait MethodImplementation {
5250
/// The argument types of the method.
5351
type Args: EncodeArguments;
5452

55-
/// Returns self as an `Imp` of a method.
53+
/// Returns self as an [`Imp`] of a method.
5654
fn imp(self) -> Imp;
5755
}
5856

@@ -129,20 +127,21 @@ impl ClassDecl {
129127
}
130128
}
131129

132-
/// Constructs a `ClassDecl` with the given name and superclass.
133-
/// Returns `None` if the class couldn't be allocated.
130+
/// Constructs a [`ClassDecl`] with the given name and superclass.
131+
///
132+
/// Returns [`None`] if the class couldn't be allocated.
134133
pub fn new(name: &str, superclass: &Class) -> Option<ClassDecl> {
135134
ClassDecl::with_superclass(name, Some(superclass))
136135
}
137136

138137
/**
139-
Constructs a `ClassDecl` declaring a new root class with the given name.
140-
Returns `None` if the class couldn't be allocated.
138+
Constructs a [`ClassDecl`] declaring a new root class with the given name.
139+
Returns [`None`] if the class couldn't be allocated.
141140
142141
An implementation for `+initialize` must also be given; the runtime calls
143142
this method for all classes, so it must be defined on root classes.
144143
145-
Note that implementing a root class is not a simple endeavor.
144+
Note that implementing a root class is not a simple endeavor!
146145
For example, your class probably cannot be passed to Cocoa code unless
147146
the entire `NSObject` protocol is implemented.
148147
Functionality it expects, like implementations of `-retain` and `-release`
@@ -158,9 +157,12 @@ impl ClassDecl {
158157
decl
159158
}
160159

161-
/// Adds a method with the given name and implementation to self.
162-
/// Panics if the method wasn't sucessfully added
163-
/// or if the selector and function take different numbers of arguments.
160+
/// Adds a method with the given name and implementation.
161+
///
162+
/// # Panics
163+
///
164+
/// Panics if the method wasn't sucessfully added or if the selector and
165+
/// function take different numbers of arguments.
164166
///
165167
/// # Safety
166168
///
@@ -184,9 +186,12 @@ impl ClassDecl {
184186
assert!(success != NO, "Failed to add method {:?}", sel);
185187
}
186188

187-
/// Adds a class method with the given name and implementation to self.
188-
/// Panics if the method wasn't sucessfully added
189-
/// or if the selector and function take different numbers of arguments.
189+
/// Adds a class method with the given name and implementation.
190+
///
191+
/// # Panics
192+
///
193+
/// Panics if the method wasn't sucessfully added or if the selector and
194+
/// function take different numbers of arguments.
190195
///
191196
/// # Safety
192197
///
@@ -211,12 +216,12 @@ impl ClassDecl {
211216
assert!(success != NO, "Failed to add class method {:?}", sel);
212217
}
213218

214-
/// Adds an ivar with type `T` and the provided name to self.
215-
/// Panics if the ivar wasn't successfully added.
216-
pub fn add_ivar<T>(&mut self, name: &str)
217-
where
218-
T: Encode,
219-
{
219+
/// Adds an ivar with type `T` and the provided name.
220+
///
221+
/// # Panics
222+
///
223+
/// If the ivar wasn't successfully added.
224+
pub fn add_ivar<T: Encode>(&mut self, name: &str) {
220225
let c_name = CString::new(name).unwrap();
221226
let encoding = CString::new(T::ENCODING.to_string()).unwrap();
222227
let size = mem::size_of::<T>();
@@ -227,15 +232,18 @@ impl ClassDecl {
227232
assert!(success != NO, "Failed to add ivar {}", name);
228233
}
229234

230-
/// Adds a protocol to self. Panics if the protocol wasn't successfully
231-
/// added
235+
/// Adds the given protocol to self.
236+
///
237+
/// # Panics
238+
///
239+
/// If the protocol wasn't successfully added.
232240
pub fn add_protocol(&mut self, proto: &Protocol) {
233241
let success = unsafe { runtime::class_addProtocol(self.cls, proto) };
234242
assert!(success != NO, "Failed to add protocol {:?}", proto);
235243
}
236244

237-
/// Registers self, consuming it and returning a reference to the
238-
/// newly registered `Class`.
245+
/// Registers the [`ClassDecl`], consuming it, and returns a reference to
246+
/// the newly registered [`Class`].
239247
pub fn register(self) -> &'static Class {
240248
unsafe {
241249
let cls = self.cls;
@@ -262,8 +270,9 @@ pub struct ProtocolDecl {
262270
}
263271

264272
impl ProtocolDecl {
265-
/// Constructs a `ProtocolDecl` with the given name. Returns `None` if the
266-
/// protocol couldn't be allocated.
273+
/// Constructs a [`ProtocolDecl`] with the given name.
274+
///
275+
/// Returns [`None`] if the protocol couldn't be allocated.
267276
pub fn new(name: &str) -> Option<ProtocolDecl> {
268277
let c_name = CString::new(name).unwrap();
269278
let proto = unsafe { runtime::objc_allocateProtocol(c_name.as_ptr()) };
@@ -303,7 +312,7 @@ impl ProtocolDecl {
303312
}
304313
}
305314

306-
/// Adds an instance method declaration with a given description to self.
315+
/// Adds an instance method declaration with a given description.
307316
pub fn add_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
308317
where
309318
Args: EncodeArguments,
@@ -312,7 +321,7 @@ impl ProtocolDecl {
312321
self.add_method_description_common::<Args, Ret>(sel, is_required, true)
313322
}
314323

315-
/// Adds a class method declaration with a given description to self.
324+
/// Adds a class method declaration with a given description.
316325
pub fn add_class_method_description<Args, Ret>(&mut self, sel: Sel, is_required: bool)
317326
where
318327
Args: EncodeArguments,
@@ -328,8 +337,8 @@ impl ProtocolDecl {
328337
}
329338
}
330339

331-
/// Registers self, consuming it and returning a reference to the
332-
/// newly registered `Protocol`.
340+
/// Registers the [`ProtocolDecl`], consuming it and returning a reference
341+
/// to the newly registered [`Protocol`].
333342
pub fn register(self) -> &'static Protocol {
334343
unsafe {
335344
runtime::objc_registerProtocol(self.proto);

objc/src/encode.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ unsafe impl<'a> Encode for &'a mut Class {
2222
}
2323

2424
/// Types that represent a group of arguments, where each has an Objective-C
25-
/// type encoding.
25+
/// type-encoding.
2626
pub trait EncodeArguments {
2727
/// The type as which the encodings for Self will be returned.
2828
const ENCODINGS: &'static [Encoding<'static>];

objc/src/exception.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use objc_exception;
2-
31
use crate::rc::StrongPtr;
42
use crate::runtime::Object;
53

@@ -20,9 +18,6 @@ use crate::runtime::Object;
2018
/// undefined behaviour until `C-unwind` is stabilized, see [RFC-2945].
2119
///
2220
/// [RFC-2945]: https://rust-lang.github.io/rfcs/2945-c-unwind-abi.html
23-
pub unsafe fn catch_exception<F, R>(closure: F) -> Result<R, StrongPtr>
24-
where
25-
F: FnOnce() -> R,
26-
{
21+
pub unsafe fn catch_exception<R>(closure: impl FnOnce() -> R) -> Result<R, StrongPtr> {
2722
objc_exception::r#try(closure).map_err(|exception| StrongPtr::new(exception as *mut Object))
2823
}

objc/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Objective-C objects can be messaged using the [`msg_send!`](macro.msg_send!.html
88
``` no_run
99
# use objc::{class, msg_send};
1010
# use objc::runtime::{BOOL, Class, Object};
11-
# fn main() {
1211
# unsafe {
1312
let cls = class!(NSObject);
1413
let obj: *mut Object = msg_send![cls, new];
@@ -17,7 +16,6 @@ let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
1716
// Even void methods must have their return type annotated
1817
let _: () = msg_send![obj, release];
1918
# }
20-
# }
2119
```
2220
2321
# Reference counting

objc/src/macros.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
/**
2-
Gets a reference to a `Class`.
2+
Gets a reference to a [`Class`] from the given name.
3+
4+
# Panics
35
46
Panics if no class with the given name can be found.
5-
To check for a class that may not exist, use `Class::get`.
67
7-
# Example
8+
To check for a class that may not exist, use [`Class::get`].
9+
10+
[`Class`]: crate::runtime::Class
11+
[`Class::get`]: crate::runtime::Class::get
12+
13+
# Examples
14+
815
``` no_run
916
# use objc::class;
10-
# fn main() {
1117
let cls = class!(NSObject);
12-
# }
1318
```
1419
*/
1520
#[macro_export]
@@ -27,15 +32,18 @@ macro_rules! class {
2732
}
2833

2934
/**
30-
Registers a selector, returning a `Sel`.
35+
Registers a selector with the Objective-C runtime.
36+
37+
Returns a [`Sel`].
38+
39+
[`Sel`]: crate::runtime::Sel
40+
41+
# Examples
3142
32-
# Example
3343
```
3444
# use objc::sel;
35-
# fn main() {
3645
let sel = sel!(description);
3746
let sel = sel!(setObject:forKey:);
38-
# }
3947
```
4048
*/
4149
#[macro_export]
@@ -58,22 +66,33 @@ macro_rules! sel {
5866
Sends a message to an object.
5967
6068
The first argument can be any type that dereferences to a type that implements
61-
`Message`, like a reference, pointer, or an `Id`.
69+
[`Message`], like a reference, a pointer, or an `objc_id::Id` to an object.
70+
6271
The syntax is similar to the message syntax in Objective-C.
72+
6373
Variadic arguments are not currently supported.
6474
65-
# Example
75+
[`Message`]: crate::Message
76+
77+
# Panics
78+
79+
Panics if the `exception` feature is enabled and the Objective-C method throws
80+
an exception.
81+
82+
And panics if the `verify_message` feature is enabled and the Objective-C
83+
method's argument's encoding does not match the encoding of the given arguments.
84+
85+
# Examples
86+
6687
``` no_run
6788
# use objc::msg_send;
6889
# use objc::runtime::Object;
69-
# fn main() {
7090
# unsafe {
7191
let obj: *mut Object;
7292
# let obj: *mut Object = 0 as *mut Object;
7393
let description: *const Object = msg_send![obj, description];
7494
let _: () = msg_send![obj, setArg1:1 arg2:2];
7595
# }
76-
# }
7796
```
7897
*/
7998
#[macro_export]

0 commit comments

Comments
 (0)