Skip to content

Commit 554c685

Browse files
committed
Auto merge of #42523 - clarcharr:refactor_ops, r=brson
Refactor ops.rs This refactors ops.rs into several different modules internally, as the file has gotten quite big. None of these modules are actually exported, but this should make maintaining it much easier. I've avoided the ambition of exporting the modules because they can more easily be rearranged after this commit goes through, even though it'd be cool to potentially export the modules in the future. I've separated the creation of each file into a separate commit so that this is easier to read. Redone version of #42269 with the movement of `RangeArgument` moved.
2 parents dfa7e21 + f8d5f90 commit 554c685

File tree

14 files changed

+3171
-3029
lines changed

14 files changed

+3171
-3029
lines changed

src/libcore/ops.rs

-3,021
This file was deleted.

src/libcore/ops/arith.rs

+873
Large diffs are not rendered by default.

src/libcore/ops/bit.rs

+839
Large diffs are not rendered by default.

src/libcore/ops/deref.rs

+119
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/drop.rs

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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 `Drop` trait is used to run some code when a value goes out of scope.
12+
/// This is sometimes called a 'destructor'.
13+
///
14+
/// When a value goes out of scope, if it implements this trait, it will have
15+
/// its `drop` method called. Then any fields the value contains will also
16+
/// be dropped recursively.
17+
///
18+
/// Because of the recursive dropping, you do not need to implement this trait
19+
/// unless your type needs its own destructor logic.
20+
///
21+
/// # Examples
22+
///
23+
/// A trivial implementation of `Drop`. The `drop` method is called when `_x`
24+
/// goes out of scope, and therefore `main` prints `Dropping!`.
25+
///
26+
/// ```
27+
/// struct HasDrop;
28+
///
29+
/// impl Drop for HasDrop {
30+
/// fn drop(&mut self) {
31+
/// println!("Dropping!");
32+
/// }
33+
/// }
34+
///
35+
/// fn main() {
36+
/// let _x = HasDrop;
37+
/// }
38+
/// ```
39+
///
40+
/// Showing the recursive nature of `Drop`. When `outer` goes out of scope, the
41+
/// `drop` method will be called first for `Outer`, then for `Inner`. Therefore
42+
/// `main` prints `Dropping Outer!` and then `Dropping Inner!`.
43+
///
44+
/// ```
45+
/// struct Inner;
46+
/// struct Outer(Inner);
47+
///
48+
/// impl Drop for Inner {
49+
/// fn drop(&mut self) {
50+
/// println!("Dropping Inner!");
51+
/// }
52+
/// }
53+
///
54+
/// impl Drop for Outer {
55+
/// fn drop(&mut self) {
56+
/// println!("Dropping Outer!");
57+
/// }
58+
/// }
59+
///
60+
/// fn main() {
61+
/// let _x = Outer(Inner);
62+
/// }
63+
/// ```
64+
///
65+
/// Because variables are dropped in the reverse order they are declared,
66+
/// `main` will print `Declared second!` and then `Declared first!`.
67+
///
68+
/// ```
69+
/// struct PrintOnDrop(&'static str);
70+
///
71+
/// fn main() {
72+
/// let _first = PrintOnDrop("Declared first!");
73+
/// let _second = PrintOnDrop("Declared second!");
74+
/// }
75+
/// ```
76+
#[lang = "drop"]
77+
#[stable(feature = "rust1", since = "1.0.0")]
78+
pub trait Drop {
79+
/// A method called when the value goes out of scope.
80+
///
81+
/// When this method has been called, `self` has not yet been deallocated.
82+
/// If it were, `self` would be a dangling reference.
83+
///
84+
/// After this function is over, the memory of `self` will be deallocated.
85+
///
86+
/// This function cannot be called explicitly. This is compiler error
87+
/// [E0040]. However, the [`std::mem::drop`] function in the prelude can be
88+
/// used to call the argument's `Drop` implementation.
89+
///
90+
/// [E0040]: ../../error-index.html#E0040
91+
/// [`std::mem::drop`]: ../../std/mem/fn.drop.html
92+
///
93+
/// # Panics
94+
///
95+
/// Given that a `panic!` will call `drop()` as it unwinds, any `panic!` in
96+
/// a `drop()` implementation will likely abort.
97+
#[stable(feature = "rust1", since = "1.0.0")]
98+
fn drop(&mut self);
99+
}

0 commit comments

Comments
 (0)