Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc9dc0a
commit fefa521
Showing
2 changed files
with
123 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters