|
| 1 | +# `arbitrary_self_types` |
| 2 | + |
| 3 | +The tracking issue for this feature is: [#44874] |
| 4 | + |
| 5 | +[#38788]: https://github.com/rust-lang/rust/issues/44874 |
| 6 | + |
| 7 | +------------------------ |
| 8 | + |
| 9 | +Allows any type implementing `core::ops::Receiver<Target=T>` to be used as the type |
| 10 | +of `self` in a method belonging to `T`. |
| 11 | + |
| 12 | +For example, |
| 13 | + |
| 14 | +```rust |
| 15 | +#![feature(arbitrary_self_types)] |
| 16 | + |
| 17 | +struct A; |
| 18 | + |
| 19 | +impl A { |
| 20 | + fn f(self: SmartPtr<Self>) -> i32 { 1 } // note self type |
| 21 | +} |
| 22 | + |
| 23 | +struct SmartPtr<T>(T); |
| 24 | + |
| 25 | +impl<T> core::ops::Receiver for SmartPtr<T> { |
| 26 | + type Target = T; |
| 27 | +} |
| 28 | + |
| 29 | +fn main() { |
| 30 | + let smart_ptr = SmartPtr(A); |
| 31 | + assert_eq!(a.f(), 1); |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +The `Receiver` trait has a blanket implementation for all `T: Deref`, so in fact |
| 36 | +things like this work too: |
| 37 | + |
| 38 | +```rust |
| 39 | +#![feature(arbitrary_self_types)] |
| 40 | + |
| 41 | +use std::rc::Rc; |
| 42 | + |
| 43 | +struct A; |
| 44 | + |
| 45 | +impl A { |
| 46 | + fn f(self: Rc<Self>) -> i32 { 1 } // Rc implements Deref |
| 47 | +} |
| 48 | + |
| 49 | +fn main() { |
| 50 | + let smart_ptr = Rc::new(A); |
| 51 | + assert_eq!(a.f(), 1); |
| 52 | +} |
| 53 | +``` |
| 54 | + |
| 55 | +Interestingly, that works even without the `arbitrary_self_types` feature |
| 56 | +- but that's because certain types are _effectively_ hard coded, including |
| 57 | +`Rc`. ("Hard coding" isn't quite true; they use a lang-item called |
| 58 | +`LegacyReceiver` to denote their special-ness in this way). |
| 59 | + |
| 60 | +## Changes to method lookup |
| 61 | + |
| 62 | +Method lookup previously used to work by stepping through the `Deref` |
| 63 | +chain then using the resulting list of types in two different ways: |
| 64 | + |
| 65 | +* To identify types that might contribute methods via their `impl` |
| 66 | + blocks (inherent methods) or via traits |
| 67 | +* To identify the types that the method receiver (`a` in the above |
| 68 | + examples) can be converted to. |
| 69 | + |
| 70 | +With this feature, these lists are created by instead stepping through |
| 71 | +the `Receiver` chain. However, a note is kept about whether the type |
| 72 | +can be reached also via the `Deref` chain. |
| 73 | + |
| 74 | +The full chain (via `Receiver` hops) is used for the first purpose |
| 75 | +(identifying relevant `impl` blocks and traits); whereas the shorter |
| 76 | +list (reachable via `Deref`) is used for the second purpose. |
| 77 | + |
| 78 | +## Types suitable for use as smart pointers |
| 79 | + |
| 80 | +This feature allows the creation of customised smart pointers - for example |
| 81 | +your own equivalent to `Rc` or `Box` with whatever capabilities you like. |
| 82 | +Those smart pointers can either implement `Deref` (if it's safe to |
| 83 | +create a reference to the referent) or `Receiver` (if it isn't). |
| 84 | + |
| 85 | +Either way, smart pointer types should mostly _avoid having methods_. |
| 86 | +Calling methods on a smart pointer leads to ambiguity about whether you're |
| 87 | +aiming for a method on the pointer, or on the referent. |
| 88 | + |
| 89 | +Best practice is therefore to put smart pointer functionality into |
| 90 | +associated functions instead - that's what's done in all the smart pointer |
| 91 | +types within Rust's standard library which implement `Receiver`. |
| 92 | + |
| 93 | +If you choose to add any methods to your smart pointer type, your users |
| 94 | +may run into errors from deshadowing, as described in the next section. |
| 95 | + |
| 96 | +## Avoiding shadowing |
| 97 | + |
| 98 | +With or without this feature, Rust emits an error if it finds two method |
| 99 | +candidates, like this: |
| 100 | + |
| 101 | + |
| 102 | +```rust,errors |
| 103 | +use std::pin::Pin; |
| 104 | +use std::pin::pin; |
| 105 | +
|
| 106 | +struct A; |
| 107 | +
|
| 108 | +impl A { |
| 109 | + fn get_ref(self: Pin<&A>) {} |
| 110 | +} |
| 111 | +
|
| 112 | +fn main() { |
| 113 | + let pinned_a: Pin<&A> = pin!(A).as_ref(); |
| 114 | + let pinned_a: Pin<&A> = pinned_a.as_ref(); |
| 115 | + pinned_a.get_ref(); // error[E0034]: multiple applicable items in scope |
| 116 | +} |
| 117 | +``` |
| 118 | + |
| 119 | +(this is why Rust's smart pointers are mostly carefully designed to avoid |
| 120 | +having methods at all, and shouldn't add new methods in future.) |
| 121 | + |
| 122 | +With `arbitrary_self_types`, we take care to spot some other kinds of |
| 123 | +conflict: |
| 124 | + |
| 125 | +```rust,errors |
| 126 | +#![feature(arbitrary_self_types)] |
| 127 | +
|
| 128 | +use std::pin::Pin; |
| 129 | +use std::pin::pin; |
| 130 | +
|
| 131 | +struct A; |
| 132 | +
|
| 133 | +impl A { |
| 134 | + fn get_ref(self: &Pin<&A>) {} // note &Pin |
| 135 | +} |
| 136 | +
|
| 137 | +fn main() { |
| 138 | + let pinned_a: Pin<&mut A> = pin!(A); |
| 139 | + let pinned_a: Pin<&A> = pinned_a.as_ref(); |
| 140 | + pinned_a.get_ref(); |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +This is to guard against the case where an inner (referent) type has a |
| 145 | +method of a given name, taking the smart pointer by reference, and then |
| 146 | +the smart pointer implementer adds a similar method taking self by value. |
| 147 | +As noted in the previous section, the safe option is simply |
| 148 | +not to add methods to smart pointers, and then these errors can't occur. |
0 commit comments