|
| 1 | +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +/// The `Deref` trait is used to specify the functionality of dereferencing |
| 12 | +/// operations, like `*v`. |
| 13 | +/// |
| 14 | +/// `Deref` also enables ['`Deref` coercions'][coercions]. |
| 15 | +/// |
| 16 | +/// [coercions]: ../../book/deref-coercions.html |
| 17 | +/// |
| 18 | +/// # Examples |
| 19 | +/// |
| 20 | +/// A struct with a single field which is accessible via dereferencing the |
| 21 | +/// struct. |
| 22 | +/// |
| 23 | +/// ``` |
| 24 | +/// use std::ops::Deref; |
| 25 | +/// |
| 26 | +/// struct DerefExample<T> { |
| 27 | +/// value: T |
| 28 | +/// } |
| 29 | +/// |
| 30 | +/// impl<T> Deref for DerefExample<T> { |
| 31 | +/// type Target = T; |
| 32 | +/// |
| 33 | +/// fn deref(&self) -> &T { |
| 34 | +/// &self.value |
| 35 | +/// } |
| 36 | +/// } |
| 37 | +/// |
| 38 | +/// fn main() { |
| 39 | +/// let x = DerefExample { value: 'a' }; |
| 40 | +/// assert_eq!('a', *x); |
| 41 | +/// } |
| 42 | +/// ``` |
| 43 | +#[lang = "deref"] |
| 44 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 45 | +pub trait Deref { |
| 46 | + /// The resulting type after dereferencing |
| 47 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 48 | + type Target: ?Sized; |
| 49 | + |
| 50 | + /// The method called to dereference a value |
| 51 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 52 | + fn deref(&self) -> &Self::Target; |
| 53 | +} |
| 54 | + |
| 55 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 56 | +impl<'a, T: ?Sized> Deref for &'a T { |
| 57 | + type Target = T; |
| 58 | + |
| 59 | + fn deref(&self) -> &T { *self } |
| 60 | +} |
| 61 | + |
| 62 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 63 | +impl<'a, T: ?Sized> Deref for &'a mut T { |
| 64 | + type Target = T; |
| 65 | + |
| 66 | + fn deref(&self) -> &T { *self } |
| 67 | +} |
| 68 | + |
| 69 | +/// The `DerefMut` trait is used to specify the functionality of dereferencing |
| 70 | +/// mutably like `*v = 1;` |
| 71 | +/// |
| 72 | +/// `DerefMut` also enables ['`Deref` coercions'][coercions]. |
| 73 | +/// |
| 74 | +/// [coercions]: ../../book/deref-coercions.html |
| 75 | +/// |
| 76 | +/// # Examples |
| 77 | +/// |
| 78 | +/// A struct with a single field which is modifiable via dereferencing the |
| 79 | +/// struct. |
| 80 | +/// |
| 81 | +/// ``` |
| 82 | +/// use std::ops::{Deref, DerefMut}; |
| 83 | +/// |
| 84 | +/// struct DerefMutExample<T> { |
| 85 | +/// value: T |
| 86 | +/// } |
| 87 | +/// |
| 88 | +/// impl<T> Deref for DerefMutExample<T> { |
| 89 | +/// type Target = T; |
| 90 | +/// |
| 91 | +/// fn deref(&self) -> &T { |
| 92 | +/// &self.value |
| 93 | +/// } |
| 94 | +/// } |
| 95 | +/// |
| 96 | +/// impl<T> DerefMut for DerefMutExample<T> { |
| 97 | +/// fn deref_mut(&mut self) -> &mut T { |
| 98 | +/// &mut self.value |
| 99 | +/// } |
| 100 | +/// } |
| 101 | +/// |
| 102 | +/// fn main() { |
| 103 | +/// let mut x = DerefMutExample { value: 'a' }; |
| 104 | +/// *x = 'b'; |
| 105 | +/// assert_eq!('b', *x); |
| 106 | +/// } |
| 107 | +/// ``` |
| 108 | +#[lang = "deref_mut"] |
| 109 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 110 | +pub trait DerefMut: Deref { |
| 111 | + /// The method called to mutably dereference a value |
| 112 | + #[stable(feature = "rust1", since = "1.0.0")] |
| 113 | + fn deref_mut(&mut self) -> &mut Self::Target; |
| 114 | +} |
| 115 | + |
| 116 | +#[stable(feature = "rust1", since = "1.0.0")] |
| 117 | +impl<'a, T: ?Sized> DerefMut for &'a mut T { |
| 118 | + fn deref_mut(&mut self) -> &mut T { *self } |
| 119 | +} |
0 commit comments