Skip to content

Commit 577af32

Browse files
committed
Document our usage of Deref
1 parent 2b32ca7 commit 577af32

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

objc2/src/foundation/mod.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,31 @@
2121
//! [pull requests]: https://github.com/madsmtm/objc2/pulls
2222
//! [`Message`]: crate::Message
2323
//! [`msg_send!`]: crate::msg_send
24+
//!
25+
//!
26+
//! # Use of `Deref`
27+
//!
28+
//! `objc2::foundation` uses the [`Deref`] trait in a bit special way: All
29+
//! objects deref to their superclasses. For example, `NSMutableArray` derefs
30+
//! to `NSArray`, which in turn derefs to `NSObject`.
31+
//!
32+
//! Note that this is explicitly recommended against in [the
33+
//! documentation][`Deref`] and [the Rust Design patterns
34+
//! book][anti-pattern-deref] (see those links for details).
35+
//!
36+
//! Due to Objective-C objects only ever being accessible behind pointers in
37+
//! the first place, the problems stated there are less severe, and having the
38+
//! implementation just means that everything is much nicer when you actually
39+
//! want to use the objects!
40+
//!
41+
//! All objects also implement [`AsRef`] and [`AsMut`] to their superclass,
42+
//! and can be used in [`Id::into_superclass`], so if you favour explicit
43+
//! conversion, that is a possibility too.
44+
//!
45+
//! [`Deref`]: std::ops::Deref
46+
//! [`ClassType`]: crate::ClassType
47+
//! [anti-pattern-deref]: https://rust-unofficial.github.io/patterns/anti_patterns/deref.html
48+
//! [`Id::into_superclass`]: crate::rc::Id::into_superclass
2449
2550
// TODO: Remove these
2651
#![allow(missing_docs)]

0 commit comments

Comments
 (0)