Skip to content

Commit fefa521

Browse files
author
Clar Charr
committed
Move Deref to module.
1 parent bc9dc0a commit fefa521

File tree

2 files changed

+123
-110
lines changed

2 files changed

+123
-110
lines changed

src/libcore/ops/deref.rs

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
}

src/libcore/ops/mod.rs

Lines changed: 4 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149

150150
mod arith;
151151
mod bit;
152+
mod deref;
152153
mod function;
153154
mod place;
154155
mod range;
@@ -164,6 +165,9 @@ pub use self::bit::{Not, BitAnd, BitOr, BitXor, Shl, Shr};
164165
#[stable(feature = "op_assign_traits", since = "1.8.0")]
165166
pub use self::bit::{BitAndAssign, BitOrAssign, BitXorAssign, ShlAssign, ShrAssign};
166167

168+
#[stable(feature = "rust1", since = "1.0.0")]
169+
pub use self::deref::{Deref, DerefMut};
170+
167171
#[stable(feature = "rust1", since = "1.0.0")]
168172
pub use self::function::{Fn, FnMut, FnOnce};
169173

@@ -423,116 +427,6 @@ pub trait IndexMut<Idx: ?Sized>: Index<Idx> {
423427
fn index_mut(&mut self, index: Idx) -> &mut Self::Output;
424428
}
425429

426-
/// The `Deref` trait is used to specify the functionality of dereferencing
427-
/// operations, like `*v`.
428-
///
429-
/// `Deref` also enables ['`Deref` coercions'][coercions].
430-
///
431-
/// [coercions]: ../../book/deref-coercions.html
432-
///
433-
/// # Examples
434-
///
435-
/// A struct with a single field which is accessible via dereferencing the
436-
/// struct.
437-
///
438-
/// ```
439-
/// use std::ops::Deref;
440-
///
441-
/// struct DerefExample<T> {
442-
/// value: T
443-
/// }
444-
///
445-
/// impl<T> Deref for DerefExample<T> {
446-
/// type Target = T;
447-
///
448-
/// fn deref(&self) -> &T {
449-
/// &self.value
450-
/// }
451-
/// }
452-
///
453-
/// fn main() {
454-
/// let x = DerefExample { value: 'a' };
455-
/// assert_eq!('a', *x);
456-
/// }
457-
/// ```
458-
#[lang = "deref"]
459-
#[stable(feature = "rust1", since = "1.0.0")]
460-
pub trait Deref {
461-
/// The resulting type after dereferencing
462-
#[stable(feature = "rust1", since = "1.0.0")]
463-
type Target: ?Sized;
464-
465-
/// The method called to dereference a value
466-
#[stable(feature = "rust1", since = "1.0.0")]
467-
fn deref(&self) -> &Self::Target;
468-
}
469-
470-
#[stable(feature = "rust1", since = "1.0.0")]
471-
impl<'a, T: ?Sized> Deref for &'a T {
472-
type Target = T;
473-
474-
fn deref(&self) -> &T { *self }
475-
}
476-
477-
#[stable(feature = "rust1", since = "1.0.0")]
478-
impl<'a, T: ?Sized> Deref for &'a mut T {
479-
type Target = T;
480-
481-
fn deref(&self) -> &T { *self }
482-
}
483-
484-
/// The `DerefMut` trait is used to specify the functionality of dereferencing
485-
/// mutably like `*v = 1;`
486-
///
487-
/// `DerefMut` also enables ['`Deref` coercions'][coercions].
488-
///
489-
/// [coercions]: ../../book/deref-coercions.html
490-
///
491-
/// # Examples
492-
///
493-
/// A struct with a single field which is modifiable via dereferencing the
494-
/// struct.
495-
///
496-
/// ```
497-
/// use std::ops::{Deref, DerefMut};
498-
///
499-
/// struct DerefMutExample<T> {
500-
/// value: T
501-
/// }
502-
///
503-
/// impl<T> Deref for DerefMutExample<T> {
504-
/// type Target = T;
505-
///
506-
/// fn deref(&self) -> &T {
507-
/// &self.value
508-
/// }
509-
/// }
510-
///
511-
/// impl<T> DerefMut for DerefMutExample<T> {
512-
/// fn deref_mut(&mut self) -> &mut T {
513-
/// &mut self.value
514-
/// }
515-
/// }
516-
///
517-
/// fn main() {
518-
/// let mut x = DerefMutExample { value: 'a' };
519-
/// *x = 'b';
520-
/// assert_eq!('b', *x);
521-
/// }
522-
/// ```
523-
#[lang = "deref_mut"]
524-
#[stable(feature = "rust1", since = "1.0.0")]
525-
pub trait DerefMut: Deref {
526-
/// The method called to mutably dereference a value
527-
#[stable(feature = "rust1", since = "1.0.0")]
528-
fn deref_mut(&mut self) -> &mut Self::Target;
529-
}
530-
531-
#[stable(feature = "rust1", since = "1.0.0")]
532-
impl<'a, T: ?Sized> DerefMut for &'a mut T {
533-
fn deref_mut(&mut self) -> &mut T { *self }
534-
}
535-
536430
/// Trait that indicates that this is a pointer or a wrapper for one,
537431
/// where unsizing can be performed on the pointee.
538432
///

0 commit comments

Comments
 (0)