8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- // So we don't have to document the actual methods on the traits.
12
- #[ allow( missing_doc) ] ;
13
-
14
11
/*!
15
12
*
16
13
* Traits representing built-in operators, useful for overloading
83
80
*/
84
81
#[ lang="drop" ]
85
82
pub trait Drop {
83
+ /// The `drop` method, called when the value goes out of scope.
86
84
fn drop ( & mut self ) ;
87
85
}
88
86
@@ -112,6 +110,7 @@ pub trait Drop {
112
110
*/
113
111
#[ lang="add" ]
114
112
pub trait Add < RHS , Result > {
113
+ /// The method for the `+` operator
115
114
fn add ( & self , rhs : & RHS ) -> Result ;
116
115
}
117
116
@@ -141,6 +140,7 @@ pub trait Add<RHS,Result> {
141
140
*/
142
141
#[ lang="sub" ]
143
142
pub trait Sub < RHS , Result > {
143
+ /// The method for the `-` operator
144
144
fn sub ( & self , rhs : & RHS ) -> Result ;
145
145
}
146
146
@@ -170,6 +170,7 @@ pub trait Sub<RHS,Result> {
170
170
*/
171
171
#[ lang="mul" ]
172
172
pub trait Mul < RHS , Result > {
173
+ /// The method for the `*` operator
173
174
fn mul ( & self , rhs : & RHS ) -> Result ;
174
175
}
175
176
@@ -199,6 +200,7 @@ pub trait Mul<RHS,Result> {
199
200
*/
200
201
#[ lang="div" ]
201
202
pub trait Div < RHS , Result > {
203
+ /// The method for the `/` operator
202
204
fn div ( & self , rhs : & RHS ) -> Result ;
203
205
}
204
206
@@ -228,6 +230,7 @@ pub trait Div<RHS,Result> {
228
230
*/
229
231
#[ lang="rem" ]
230
232
pub trait Rem < RHS , Result > {
233
+ /// The method for the `%` operator
231
234
fn rem ( & self , rhs : & RHS ) -> Result ;
232
235
}
233
236
@@ -257,6 +260,7 @@ pub trait Rem<RHS,Result> {
257
260
*/
258
261
#[ lang="neg" ]
259
262
pub trait Neg < Result > {
263
+ /// The method for the unary `-` operator
260
264
fn neg ( & self ) -> Result ;
261
265
}
262
266
@@ -286,6 +290,7 @@ pub trait Neg<Result> {
286
290
*/
287
291
#[ lang="not" ]
288
292
pub trait Not < Result > {
293
+ /// The method for the unary `!` operator
289
294
fn not ( & self ) -> Result ;
290
295
}
291
296
@@ -315,6 +320,7 @@ pub trait Not<Result> {
315
320
*/
316
321
#[ lang="bitand" ]
317
322
pub trait BitAnd < RHS , Result > {
323
+ /// The method for the `&` operator
318
324
fn bitand ( & self , rhs : & RHS ) -> Result ;
319
325
}
320
326
@@ -344,6 +350,7 @@ pub trait BitAnd<RHS,Result> {
344
350
*/
345
351
#[ lang="bitor" ]
346
352
pub trait BitOr < RHS , Result > {
353
+ /// The method for the `|` operator
347
354
fn bitor ( & self , rhs : & RHS ) -> Result ;
348
355
}
349
356
@@ -373,6 +380,7 @@ pub trait BitOr<RHS,Result> {
373
380
*/
374
381
#[ lang="bitxor" ]
375
382
pub trait BitXor < RHS , Result > {
383
+ /// The method for the `^` operator
376
384
fn bitxor ( & self , rhs : & RHS ) -> Result ;
377
385
}
378
386
@@ -402,6 +410,7 @@ pub trait BitXor<RHS,Result> {
402
410
*/
403
411
#[ lang="shl" ]
404
412
pub trait Shl < RHS , Result > {
413
+ /// The method for the `<<` operator
405
414
fn shl ( & self , rhs : & RHS ) -> Result ;
406
415
}
407
416
@@ -431,6 +440,7 @@ pub trait Shl<RHS,Result> {
431
440
*/
432
441
#[ lang="shr" ]
433
442
pub trait Shr < RHS , Result > {
443
+ /// The method for the `>>` operator
434
444
fn shr ( & self , rhs : & RHS ) -> Result ;
435
445
}
436
446
@@ -461,6 +471,7 @@ pub trait Shr<RHS,Result> {
461
471
*/
462
472
#[ lang="index" ]
463
473
pub trait Index < Index , Result > {
474
+ /// The method for the indexing (`Foo[Bar]`) operation
464
475
fn index ( & self , index : & Index ) -> Result ;
465
476
}
466
477
@@ -469,9 +480,37 @@ pub trait Deref<Result> {
469
480
fn deref < ' a > ( & ' a self ) -> & ' a Result ;
470
481
}
471
482
483
+ /**
484
+ *
485
+ * The `Deref` trait is used to specify the functionality of dereferencing
486
+ * operations like `*v`.
487
+ *
488
+ * # Example
489
+ *
490
+ * A struct with a single field which is accessible via dereferencing the
491
+ * struct.
492
+ *
493
+ * ```
494
+ * struct DerefExample<T> {
495
+ * value: T
496
+ * }
497
+ *
498
+ * impl<T> Deref<T> for DerefExample<T> {
499
+ * fn deref<'a>(&'a self) -> &'a T {
500
+ * &self.value
501
+ * }
502
+ * }
503
+ *
504
+ * fn main() {
505
+ * let x = DerefExample { value: 'a' };
506
+ * assert_eq!('a', *x);
507
+ * }
508
+ * ```
509
+ */
472
510
#[ cfg( not( stage0) ) ]
473
511
#[ lang="deref" ]
474
512
pub trait Deref < Result > {
513
+ /// The method called to dereference a value
475
514
fn deref < ' a > ( & ' a self ) -> & ' a Result ;
476
515
}
477
516
@@ -480,9 +519,44 @@ pub trait DerefMut<Result>: Deref<Result> {
480
519
fn deref_mut < ' a > ( & ' a mut self ) -> & ' a mut Result ;
481
520
}
482
521
522
+ /**
523
+ *
524
+ * The `DerefMut` trait is used to specify the functionality of dereferencing
525
+ * mutably like `*v = 1;`
526
+ *
527
+ * # Example
528
+ *
529
+ * A struct with a single field which is modifiable via dereferencing the
530
+ * struct.
531
+ *
532
+ * ```
533
+ * struct DerefMutExample<T> {
534
+ * value: T
535
+ * }
536
+ *
537
+ * impl<T> Deref<T> for DerefMutExample<T> {
538
+ * fn deref<'a>(&'a self) -> &'a T {
539
+ * &self.value
540
+ * }
541
+ * }
542
+ *
543
+ * impl<T> DerefMut<T> for DerefMutExample<T> {
544
+ * fn deref_mut<'a>(&'a mut self) -> &'a mut T {
545
+ * &mut self.value
546
+ * }
547
+ * }
548
+ *
549
+ * fn main() {
550
+ * let mut x = DerefMutExample { value: 'a' };
551
+ * *x = 'b';
552
+ * assert_eq!('b', *x);
553
+ * }
554
+ * ```
555
+ */
483
556
#[ cfg( not( stage0) ) ]
484
557
#[ lang="deref_mut" ]
485
558
pub trait DerefMut < Result > : Deref < Result > {
559
+ /// The method called to mutably dereference a value
486
560
fn deref_mut < ' a > ( & ' a mut self ) -> & ' a mut Result ;
487
561
}
488
562
0 commit comments