@@ -294,14 +294,100 @@ unsafe impl<T: ?Sized> DerefPure for &T {}
294
294
#[ unstable( feature = "deref_pure_trait" , issue = "87121" ) ]
295
295
unsafe impl < T : ?Sized > DerefPure for & mut T { }
296
296
297
+ /// Indicates that a struct can be used as a method receiver.
298
+ /// That is, a type can use this type as a type of `self`, like this:
299
+ /// ```
300
+ /// use std::ops::Receiver;
301
+ ///
302
+ /// struct SmartPointer<T>(T);
303
+ ///
304
+ /// impl<T> Receiver for SmartPointer<T> {
305
+ /// type Target = T;
306
+ /// }
307
+ ///
308
+ /// struct MyContainedType;
309
+ ///
310
+ /// impl MyContainedType {
311
+ /// fn method(self: MySmartPointer<Self>) {
312
+ /// // ...
313
+ /// }
314
+ /// }
315
+ ///
316
+ /// fn main() {
317
+ /// let ptr = SmartPointer(MyContainedType);
318
+ /// ptr.method();
319
+ /// }
320
+ /// ```
321
+ /// This trait is blanket implemented for any type which implements
322
+ /// [`Deref`], which includes stdlib pointer types like `Box<T>`,`Rc<T>`, `&T`,
323
+ /// and `Pin<P>`. For that reason, it's relatively rare to need to
324
+ /// implement this directly. You'll typically do this only if you need
325
+ /// to implement a smart pointer type which can't implement [`Deref`]; perhaps
326
+ /// because you're interfacing with another programming language and can't
327
+ /// guarantee that references comply with Rust's aliasing rules.
328
+ ///
329
+ /// When looking for method candidates, Rust will explore a chain of possible
330
+ /// `Receiver`s, so for example each of the following methods work:
331
+ /// ```
332
+ /// use std::boxed::Box;
333
+ /// use std::rc::Rc;
334
+ ///
335
+ /// // Both `Box` and `Rc`` (indirectly) implement Receiver
336
+ ///
337
+ /// struct MyContainedType;
338
+ ///
339
+ /// fn main() {
340
+ /// let t = Rc::new(Box::new(MyContainedType));
341
+ /// t.method_a();
342
+ /// t.method_b();
343
+ /// t.method_c();
344
+ /// t.method_d();
345
+ /// }
346
+ ///
347
+ /// impl MyContainedType {
348
+ /// fn method_a(self) {
349
+ ///
350
+ /// }
351
+ /// fn method_b(&self) {
352
+ ///
353
+ /// }
354
+ /// fn method_c(self: Box<Self>) {
355
+ ///
356
+ /// }
357
+ /// fn method_d(self: Rc<Box<Self>>) {
358
+ ///
359
+ /// }
360
+ /// }
361
+ /// ```
362
+ #[ lang = "receiver" ]
363
+ #[ cfg( not( bootstrap) ) ]
364
+ #[ unstable( feature = "arbitrary_self_types" , issue = "44874" ) ]
365
+ pub trait Receiver {
366
+ /// The target type on which the method may be called.
367
+ #[ cfg( not( bootstrap) ) ]
368
+ #[ rustc_diagnostic_item = "receiver_target" ]
369
+ #[ lang = "receiver_target" ]
370
+ #[ unstable( feature = "arbitrary_self_types" , issue = "44874" ) ]
371
+ type Target : ?Sized ;
372
+ }
373
+
374
+ #[ cfg( not( bootstrap) ) ]
375
+ #[ unstable( feature = "arbitrary_self_types" , issue = "44874" ) ]
376
+ impl < P : ?Sized , T : ?Sized > Receiver for P
377
+ where
378
+ P : Deref < Target = T > ,
379
+ {
380
+ type Target = T ;
381
+ }
382
+
297
383
/// Indicates that a struct can be used as a method receiver, without the
298
384
/// `arbitrary_self_types` feature. This is implemented by stdlib pointer types like `Box<T>`,
299
385
/// `Rc<T>`, `&T`, and `Pin<P>`.
300
386
///
301
387
/// This trait will shortly be removed and replaced with a more generic
302
388
/// facility based around the current "arbitrary self types" unstable feature.
303
- /// That new facility will use a replacement trait called `Receiver` which is
304
- /// why this is now named `LegacyReceiver`.
389
+ /// That new facility will use the replacement trait above called `Receiver`
390
+ /// which is why this is now named `LegacyReceiver`.
305
391
#[ cfg_attr( bootstrap, lang = "receiver" ) ]
306
392
#[ cfg_attr( not( bootstrap) , lang = "legacy_receiver" ) ]
307
393
#[ unstable( feature = "legacy_receiver_trait" , issue = "none" ) ]
0 commit comments