99//! what Objective-C type-encodings are.
1010//!
1111//!
12- //! ## Example
12+ //! ## Examples
1313//!
1414//! Implementing [`Encode`] and [`RefEncode`] for a custom type:
1515//!
4343//! assert!(MyStruct::ENCODING_REF.equivalent_to_str("^{MyStruct=fs}"));
4444//! ```
4545//!
46- //! See the [`examples `] folder for more complex usage .
46+ //! Implementing [`Encode `] for a few core-graphics types .
4747//!
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.
4950//!
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+ //! ```
5060//!
51- //! ## Caveats
61+ //! Implementing [`RefEncode`] for an object, in this case `NSString`.
5262//!
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+ //! ```
5766//!
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+ //! ```
6173
6274use core:: cell:: { Cell , UnsafeCell } ;
6375use core:: ffi:: c_void;
@@ -82,6 +94,7 @@ pub use objc2_encode::{Encoding, EncodingBox, ParseError};
8294/// If your type is an opaque type you should not need to implement this;
8395/// there you will only need [`RefEncode`].
8496///
97+ ///
8598/// # Safety
8699///
87100/// The type must be FFI-safe, meaning a C-compatible `repr` (`repr(C)`,
@@ -99,6 +112,7 @@ pub use objc2_encode::{Encoding, EncodingBox, ParseError};
99112/// passed to Objective-C via. `objc2::msg_send!` their destructor will not be
100113/// called!
101114///
115+ ///
102116/// # Examples
103117///
104118/// Implementing for a struct:
@@ -149,6 +163,7 @@ pub unsafe trait Encode {
149163/// - `Option<&mut T>`
150164/// - `Option<NonNull<T>>`
151165///
166+ ///
152167/// # Reasoning behind this trait's existence
153168///
154169/// External crates cannot implement [`Encode`] for pointers or [`Option`]s
@@ -159,6 +174,7 @@ pub unsafe trait Encode {
159174/// Finally, having this trait allows for much cleaner generic code that need
160175/// to represent types that can be encoded as pointers.
161176///
177+ ///
162178/// # Safety
163179///
164180/// References to the object must be FFI-safe.
0 commit comments