9
9
//! what Objective-C type-encodings are.
10
10
//!
11
11
//!
12
- //! ## Example
12
+ //! ## Examples
13
13
//!
14
14
//! Implementing [`Encode`] and [`RefEncode`] for a custom type:
15
15
//!
43
43
//! assert!(MyStruct::ENCODING_REF.equivalent_to_str("^{MyStruct=fs}"));
44
44
//! ```
45
45
//!
46
- //! See the [`examples `] folder for more complex usage .
46
+ //! Implementing [`Encode `] for a few core-graphics types .
47
47
//!
48
- //! [`examples`]: https://github.com/madsmtm/objc2/tree/master/crates/objc2/examples
48
+ //! Note that these are available in `icrate`, so the implementation here is
49
+ //! mostly for demonstration.
49
50
//!
51
+ //! ```
52
+ #![ doc = include_str ! ( "../../examples/encode_core_graphics.rs" ) ]
53
+ //! ```
54
+ //!
55
+ //! Implementing [`Encode`] and [`RefEncode`] for a transparent newtype.
56
+ //!
57
+ //! ```
58
+ #![ doc = include_str ! ( "../../examples/encode_nsuinteger.rs" ) ]
59
+ //! ```
50
60
//!
51
- //! ## Caveats
61
+ //! Implementing [`RefEncode`] for an object, in this case `NSString`.
52
62
//!
53
- //! We've taken the pragmatic approach with [`Encode`] and [`RefEncode`], and
54
- //! have implemented it for as many types as possible (instead of defining a
55
- //! bunch of subtraits for very specific purposes). However, that might
56
- //! sometimes be slightly surprising.
63
+ //! ```
64
+ #![ doc = include_str ! ( "../../examples/encode_nsstring.rs" ) ]
65
+ //! ```
57
66
//!
58
- //! The primary example is [`()`][`unit`], which doesn't make sense as a
59
- //! method argument, but is a very common return type, and hence implements
60
- //! [`Encode`].
67
+ //! Implementing [`RefEncode`] for a type where you don't necessarily know
68
+ //! about the exact internals / the internals are not representable in Rust.
69
+ //!
70
+ //! ```
71
+ #![ doc = include_str ! ( "../../examples/encode_opaque_type.rs" ) ]
72
+ //! ```
61
73
62
74
use core:: cell:: { Cell , UnsafeCell } ;
63
75
use core:: ffi:: c_void;
@@ -82,6 +94,7 @@ pub use objc2_encode::{Encoding, EncodingBox, ParseError};
82
94
/// If your type is an opaque type you should not need to implement this;
83
95
/// there you will only need [`RefEncode`].
84
96
///
97
+ ///
85
98
/// # Safety
86
99
///
87
100
/// The type must be FFI-safe, meaning a C-compatible `repr` (`repr(C)`,
@@ -99,6 +112,7 @@ pub use objc2_encode::{Encoding, EncodingBox, ParseError};
99
112
/// passed to Objective-C via. `objc2::msg_send!` their destructor will not be
100
113
/// called!
101
114
///
115
+ ///
102
116
/// # Examples
103
117
///
104
118
/// Implementing for a struct:
@@ -149,6 +163,7 @@ pub unsafe trait Encode {
149
163
/// - `Option<&mut T>`
150
164
/// - `Option<NonNull<T>>`
151
165
///
166
+ ///
152
167
/// # Reasoning behind this trait's existence
153
168
///
154
169
/// External crates cannot implement [`Encode`] for pointers or [`Option`]s
@@ -159,6 +174,7 @@ pub unsafe trait Encode {
159
174
/// Finally, having this trait allows for much cleaner generic code that need
160
175
/// to represent types that can be encoded as pointers.
161
176
///
177
+ ///
162
178
/// # Safety
163
179
///
164
180
/// References to the object must be FFI-safe.
0 commit comments