Skip to content

Commit a970cc3

Browse files
committed
Doctest README examples as well
1 parent 2b34564 commit a970cc3

File tree

8 files changed

+67
-11
lines changed

8 files changed

+67
-11
lines changed

objc/README.md

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,18 @@ Objective-C Runtime bindings and wrapper for Rust.
1111

1212
Objective-C objects can be messaged using the `msg_send!` macro:
1313

14-
```rust
14+
```rust , no_run
15+
use objc::{class, msg_send};
16+
use objc::runtime::{BOOL, Object};
17+
1518
let cls = class!(NSObject);
16-
let obj: *mut Object = msg_send![cls, new];
17-
let hash: usize = msg_send![obj, hash];
18-
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
19-
// Even void methods must have their return type annotated
20-
let _: () = msg_send![obj, release];
19+
unsafe {
20+
let obj: *mut Object = msg_send![cls, new];
21+
let hash: usize = msg_send![obj, hash];
22+
let is_kind: BOOL = msg_send![obj, isKindOfClass:cls];
23+
// Even void methods must have their return type annotated
24+
let _: () = msg_send![obj, release];
25+
}
2126
```
2227

2328
## Reference counting
@@ -28,7 +33,10 @@ A `StrongPtr` retains an object and releases the object when dropped.
2833
A `WeakPtr` will not retain the object, but can be upgraded to a `StrongPtr`
2934
and safely fails if the object has been deallocated.
3035

31-
```rust
36+
```rust , no_run
37+
use objc::{class, msg_send};
38+
use objc::rc::{autoreleasepool, StrongPtr};
39+
3240
// StrongPtr will release the object when dropped
3341
let obj = unsafe {
3442
StrongPtr::new(msg_send![class!(NSObject), new])
@@ -56,7 +64,11 @@ methods can then be added before the class is ultimately registered.
5664
The following example demonstrates declaring a class named `MyNumber` that has
5765
one ivar, a `u32` named `_number` and a `number` method that returns it:
5866

59-
```rust
67+
```rust , no_run
68+
use objc::{class, sel};
69+
use objc::declare::ClassDecl;
70+
use objc::runtime::{Object, Sel};
71+
6072
let superclass = class!(NSObject);
6173
let mut decl = ClassDecl::new("MyNumber", superclass).unwrap();
6274

objc/src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,14 @@ The bindings can be used on Linux or *BSD utilizing the
6969
extern crate alloc;
7070
extern crate std;
7171

72+
#[cfg(doctest)]
73+
#[doc = include_str!("../README.md")]
74+
extern "C" {}
75+
76+
#[cfg(doctest)]
77+
#[doc = include_str!("../../README.md")]
78+
extern "C" {}
79+
7280
pub use objc_encode::{Encode, Encoding};
7381

7482
pub use crate::encode::EncodeArguments;

objc_encode/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,6 @@ readme = "README.md"
1616
repository = "https://github.com/madsmtm/objc"
1717
documentation = "https://docs.rs/objc-encode/"
1818
license = "MIT"
19+
20+
[dev-dependencies]
21+
objc = { path = "../objc", version = "0.2.7" }

objc_encode/README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,23 @@ This crate declares an `Encode` trait that can be implemented for types that
1818
the Objective-C compiler can encode. Implementing this trait looks like:
1919

2020
```rust
21+
use objc::{Encode, Encoding};
22+
23+
#[cfg(target_pointer_width = "32")]
24+
type CGFloat = f32;
25+
26+
#[cfg(target_pointer_width = "64")]
27+
type CGFloat = f64;
28+
29+
#[repr(C)]
30+
struct CGPoint {
31+
x: CGFloat,
32+
y: CGFloat,
33+
}
34+
2135
unsafe impl Encode for CGPoint {
2236
const ENCODING: Encoding<'static> =
23-
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFLOAT::ENCODING]);
37+
Encoding::Struct("CGPoint", &[CGFloat::ENCODING, CGFloat::ENCODING]);
2438
}
2539
```
2640

@@ -33,6 +47,8 @@ An `Encoding` can be compared with an encoding string from the Objective-C
3347
runtime:
3448

3549
```rust
50+
use objc::Encode;
51+
3652
assert!(&i32::ENCODING == "i");
3753
```
3854

@@ -42,5 +58,7 @@ Every `Encoding` implements `Display` as its string representation.
4258
This can be generated conveniently through the `to_string` method:
4359

4460
```rust
61+
use objc::Encode;
62+
4563
assert_eq!(i32::ENCODING.to_string(), "i");
4664
```

objc_encode/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ assert_eq!(i32::ENCODING.to_string(), "i");
4747
// Update in Cargo.toml as well.
4848
#![doc(html_root_url = "https://docs.rs/objc-encode/1.1.0")]
4949

50+
#[cfg(doctest)]
51+
#[doc = include_str!("../README.md")]
52+
extern "C" {}
53+
5054
#[cfg(any(test, doc))]
5155
extern crate alloc;
5256

objc_exception/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@
66
//! - <https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Exceptions/Exceptions.html>
77
//! - <https://llvm.org/docs/ExceptionHandling.html>
88
9+
#![no_std]
910
#![warn(missing_docs)]
1011
// Update in Cargo.toml as well.
1112
#![doc(html_root_url = "https://docs.rs/objc_exception/0.1.2")]
1213

13-
#![no_std]
14-
1514
#[cfg(test)]
1615
extern crate alloc;
1716

17+
#[cfg(doctest)]
18+
#[doc = include_str!("../README.md")]
19+
extern "C" {}
20+
1821
use core::ffi::c_void;
1922
use core::mem;
2023
use core::ptr;

objc_foundation/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
extern crate alloc;
77
extern crate std;
88

9+
#[cfg(doctest)]
10+
#[doc = include_str!("../README.md")]
11+
extern "C" {}
12+
913
pub use self::array::{
1014
INSArray, INSMutableArray, NSArray, NSComparisonResult, NSMutableArray, NSMutableSharedArray,
1115
NSRange, NSSharedArray,

objc_foundation_derive/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Update in Cargo.toml as well.
22
#![doc(html_root_url = "https://docs.rs/objc-foundation-derive/0.0.1")]
33

4+
#[cfg(doctest)]
5+
#[doc = include_str!("../README.md")]
6+
extern "C" {}
7+
48
extern crate proc_macro;
59
#[macro_use]
610
extern crate quote;

0 commit comments

Comments
 (0)