diff options
| author | Clar Charr <clar@charr.xyz> | 2017-06-07 22:13:31 -0400 |
|---|---|---|
| committer | Clar Charr <clar@charr.xyz> | 2017-06-09 19:07:27 -0400 |
| commit | fefa521e5082fa610b7e5eebcdd98b56dc6b523b (patch) | |
| tree | 990f3762de7e64427b447a9a5136fa5183209c8a /src/libcore | |
| parent | bc9dc0ab3e26171da27fccf5775538e55c2a8e06 (diff) | |
| download | rust-fefa521e5082fa610b7e5eebcdd98b56dc6b523b.tar.gz rust-fefa521e5082fa610b7e5eebcdd98b56dc6b523b.zip | |
Move Deref to module.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/ops/deref.rs | 119 | ||||
| -rw-r--r-- | src/libcore/ops/mod.rs | 114 |
2 files changed, 123 insertions, 110 deletions
diff --git a/src/libcore/ops/deref.rs b/src/libcore/ops/deref.rs new file mode 100644 index 00000000000..3f4dabbbc0c --- /dev/null +++ b/src/libcore/ops/deref.rs @@ -0,0 +1,119 @@ +// Copyright 2012 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +/// The `Deref` trait is used to specify the functionality of dereferencing +/// operations, like `*v`. +/// +/// `Deref` also enables ['`Deref` coercions'][coercions]. +/// +/// [coercions]: ../../book/deref-coercions.html +/// +/// # Examples +/// +/// A struct with a single field which is accessible via dereferencing the +/// struct. +/// +/// ``` +/// use std::ops::Deref; +/// +/// struct DerefExample<T> { +/// value: T +/// } +/// +/// impl<T> Deref for DerefExample<T> { +/// type Target = T; +/// +/// fn deref(&self) -> &T { +/// &self.value +/// } +/// } +/// +/// fn main() { +/// let x = DerefExample { value: 'a' }; +/// assert_eq!('a', *x); +/// } +/// ``` +#[lang = "deref"] +#[stable(feature = "rust1", since = "1.0.0")] +pub trait Deref { + /// The resulting type after dereferencing + #[stable(feature = "rust1", since = "1.0.0")] + type Target: ?Sized; + + /// The method called to dereference a value + #[stable(feature = "rust1", since = "1.0.0")] + fn deref(&self) -> &Self::Target; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, T: ?Sized> Deref for &'a T { + type Target = T; + + fn deref(&self) -> &T { *self } +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, T: ?Sized> Deref for &'a mut T { + type Target = T; + + fn deref(&self) -> &T { *self } +} + +/// The `DerefMut` trait is used to specify the functionality of dereferencing +/// mutably like `*v = 1;` +/// +/// `DerefMut` also enables ['`Deref` coercions'][coercions]. +/// +/// [coercions]: ../../book/deref-coercions.html +/// +/// # Examples +/// +/// A struct with a single field which is modifiable via dereferencing the +/// struct. +/// +/// ``` +/// use std::ops::{Deref, DerefMut}; +/// +/// struct DerefMutExample<T> { +/// value: T +/// } +/// +/// impl<T> Deref for DerefMutExample<T> { +/// type Target = T; +/// +/// fn deref(&self) -> &T { +/// &self.value +/// } +/// } +/// +/// impl<T> DerefMut for DerefMutExample<T> { +/// fn deref_mut(&mut self) -> &mut T { +/// &mut self.value +/// } +/// } +/// +/// fn main() { +/// let mut x = DerefMutExample { value: 'a' }; +/// *x = 'b'; +/// assert_eq!('b', *x); +/// } +/// ``` +#[lang = "deref_mut"] +#[stable(feature = "rust1", since = "1.0.0")] +pub trait DerefMut: Deref { + /// The method called to mutably dereference a value + #[stable(feature = "rust1", since = "1.0.0")] + fn deref_mut(&mut self) -> &mut Self::Target; +} + +#[stable(feature = "rust1", since = "1.0.0")] +impl<'a, T: ?Sized> DerefMut for &'a mut T { + fn deref_mut(&mut self) -> &mut T { *self } +} diff --git a/src/libcore/ops/mod.rs b/src/libcore/ops/mod.rs index ae6fcf52cd7..22e5695a878 100644 --- a/src/libcore/ops/mod.rs +++ b/src/libcore/ops/mod.rs @@ -149,6 +149,7 @@ mod arith; mod bit; +mod deref; mod function; mod place; mod range; @@ -165,6 +166,9 @@ pub use self::bit::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign}; #[stable(feature = "rust1", since = "1.0.0")] +pub use self::deref::{Deref, DerefMut}; + +#[stable(feature = "rust1", since = "1.0.0")] pub use self::function::{Fn, FnMut, FnOnce}; #[stable(feature = "rust1", since = "1.0.0")] @@ -423,116 +427,6 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> { fn index_mut(&mut self, index: Idx) -> &mut Self::Output; } -/// The `Deref` trait is used to specify the functionality of dereferencing -/// operations, like `*v`. -/// -/// `Deref` also enables ['`Deref` coercions'][coercions]. -/// -/// [coercions]: ../../book/deref-coercions.html -/// -/// # Examples -/// -/// A struct with a single field which is accessible via dereferencing the -/// struct. -/// -/// ``` -/// use std::ops::Deref; -/// -/// struct DerefExample<T> { -/// value: T -/// } -/// -/// impl<T> Deref for DerefExample<T> { -/// type Target = T; -/// -/// fn deref(&self) -> &T { -/// &self.value -/// } -/// } -/// -/// fn main() { -/// let x = DerefExample { value: 'a' }; -/// assert_eq!('a', *x); -/// } -/// ``` -#[lang = "deref"] -#[stable(feature = "rust1", since = "1.0.0")] -pub trait Deref { - /// The resulting type after dereferencing - #[stable(feature = "rust1", since = "1.0.0")] - type Target: ?Sized; - - /// The method called to dereference a value - #[stable(feature = "rust1", since = "1.0.0")] - fn deref(&self) -> &Self::Target; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> Deref for &'a T { - type Target = T; - - fn deref(&self) -> &T { *self } -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> Deref for &'a mut T { - type Target = T; - - fn deref(&self) -> &T { *self } -} - -/// The `DerefMut` trait is used to specify the functionality of dereferencing -/// mutably like `*v = 1;` -/// -/// `DerefMut` also enables ['`Deref` coercions'][coercions]. -/// -/// [coercions]: ../../book/deref-coercions.html -/// -/// # Examples -/// -/// A struct with a single field which is modifiable via dereferencing the -/// struct. -/// -/// ``` -/// use std::ops::{Deref, DerefMut}; -/// -/// struct DerefMutExample<T> { -/// value: T -/// } -/// -/// impl<T> Deref for DerefMutExample<T> { -/// type Target = T; -/// -/// fn deref(&self) -> &T { -/// &self.value -/// } -/// } -/// -/// impl<T> DerefMut for DerefMutExample<T> { -/// fn deref_mut(&mut self) -> &mut T { -/// &mut self.value -/// } -/// } -/// -/// fn main() { -/// let mut x = DerefMutExample { value: 'a' }; -/// *x = 'b'; -/// assert_eq!('b', *x); -/// } -/// ``` -#[lang = "deref_mut"] -#[stable(feature = "rust1", since = "1.0.0")] -pub trait DerefMut: Deref { - /// The method called to mutably dereference a value - #[stable(feature = "rust1", since = "1.0.0")] - fn deref_mut(&mut self) -> &mut Self::Target; -} - -#[stable(feature = "rust1", since = "1.0.0")] -impl<'a, T: ?Sized> DerefMut for &'a mut T { - fn deref_mut(&mut self) -> &mut T { *self } -} - /// Trait that indicates that this is a pointer or a wrapper for one, /// where unsizing can be performed on the pointee. /// |
